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.Objects;
023import java.util.function.BinaryOperator;
024
025/**
026 * Like {@link BinaryOperator} but throws {@link IOException}.
027 *
028 * @param <T> the type of the operands and result of the operator.
029 * @see IOBiFunction
030 * @see BinaryOperator
031 * @since 2.12.0
032 */
033@FunctionalInterface
034public interface IOBinaryOperator<T> extends IOBiFunction<T, T, T> {
035
036    /**
037     * Creates a {@link IOBinaryOperator} which returns the greater of two elements according to the specified
038     * {@code Comparator}.
039     *
040     * @param <T> the type of the input arguments of the comparator
041     * @param comparator a {@code Comparator} for comparing the two values
042     * @return a {@code BinaryOperator} which returns the greater of its operands, according to the supplied
043     *         {@code Comparator}
044     * @throws NullPointerException if the argument is null
045     */
046    static <T> IOBinaryOperator<T> maxBy(final IOComparator<? super T> comparator) {
047        Objects.requireNonNull(comparator);
048        return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
049    }
050
051    /**
052     * Creates a {@link IOBinaryOperator} which returns the lesser of two elements according to the specified
053     * {@code Comparator}.
054     *
055     * @param <T> the type of the input arguments of the comparator
056     * @param comparator a {@code Comparator} for comparing the two values
057     * @return a {@code BinaryOperator} which returns the lesser of its operands, according to the supplied
058     *         {@code Comparator}
059     * @throws NullPointerException if the argument is null
060     */
061    static <T> IOBinaryOperator<T> minBy(final IOComparator<? super T> comparator) {
062        Objects.requireNonNull(comparator);
063        return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
064    }
065
066    /**
067     * Creates a {@link BinaryOperator} for this instance that throws {@link UncheckedIOException} instead of
068     * {@link IOException}.
069     *
070     * @return an unchecked BiFunction.
071     */
072    default BinaryOperator<T> asBinaryOperator() {
073        return (t, u) -> Uncheck.apply(this, t, u);
074    }
075}