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