001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.io.function;
019
020import java.io.IOException;
021import java.io.UncheckedIOException;
022import java.util.Comparator;
023
024/**
025 * Like {@link Comparator} but throws {@link IOException}.
026 *
027 * @param <T> the type of objects that may be compared by this comparator
028 * @see Comparator
029 * @since 2.12.0
030 */
031@FunctionalInterface
032public interface IOComparator<T> {
033
034    /**
035     * Creates a {@link Comparator} for this instance that throws {@link UncheckedIOException} instead of
036     * {@link IOException}.
037     *
038     * @return an UncheckedIOException BiFunction.
039     */
040    default Comparator<T> asComparator() {
041        return (t, u) -> Uncheck.compare(this, t, u);
042    }
043
044    /**
045     * Like {@link Comparator#compare(Object, Object)} but throws {@link IOException}.
046     *
047     * @param o1 the first object to be compared.
048     * @param o2 the second object to be compared.
049     * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than
050     *         the second.
051     * @throws NullPointerException if an argument is null and this comparator does not permit null arguments
052     * @throws ClassCastException if the arguments' types prevent them from being compared by this comparator.
053     * @throws IOException if an I/O error occurs.
054     */
055    int compare(T o1, T o2) throws IOException;
056}