io.github.suppierk.java.util.function.ThrowableBinaryOperator Maven / Gradle / Ivy
Show all versions of java-throwable-utils Show documentation
/*
* MIT License
*
* Copyright (c) 2020 Roman Khlebnov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.github.suppierk.java.util.function;
import java.util.Comparator;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
/**
* Represents an operation upon two operands of the same type, producing a result of the same type
* as the operands. This is a specialization of {@link BiFunction} for the case where the operands
* and the result are all of the same type.
*
* Permits checked exceptions unlike {@link java.util.function.BinaryOperator}
*
*
This is a functional interface whose functional method is
* {@link #apply(Object, Object)}.
*
* @param the type of the operands and result of the operator
* @see BiFunction
* @see UnaryOperator
*/
@FunctionalInterface
@SuppressWarnings("squid:S112")
public interface ThrowableBinaryOperator extends ThrowableBiFunction {
/**
* Returns a {@link ThrowableBinaryOperator} which returns the lesser of two elements according to
* the specified {@code Comparator}.
*
* @param the type of the input arguments of the comparator
* @param comparator a {@code Comparator} for comparing the two values
* @return a {@code BinaryOperator} which returns the lesser of its operands, according to the
* supplied {@code Comparator}
* @throws NullPointerException if the argument is null
*/
static ThrowableBinaryOperator minBy(Comparator super T> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
}
/**
* Returns a {@link ThrowableBinaryOperator} which returns the greater of two elements according
* to the specified {@code Comparator}.
*
* @param the type of the input arguments of the comparator
* @param comparator a {@code Comparator} for comparing the two values
* @return a {@code BinaryOperator} which returns the greater of its operands, according to the
* supplied {@code Comparator}
* @throws NullPointerException if the argument is null
*/
static ThrowableBinaryOperator maxBy(Comparator super T> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
}
}