com.annimon.stream.function.BinaryOperator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stream Show documentation
Show all versions of stream Show documentation
Stream API from Java 8 rewritten on iterators for Java 7 and below
package com.annimon.stream.function;
import com.annimon.stream.Objects;
import java.util.Comparator;
/**
* Represents an operation on two operands that produces a result of the
* same type as its operand.
*
* @param the type of the operands and result of the operator
*/
public interface BinaryOperator extends BiFunction {
class Util {
private Util() { }
/**
* Returns a {@code BinaryOperator} which returns 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
*/
public static BinaryOperator minBy(final Comparator super T> comparator) {
Objects.requireNonNull(comparator);
return new BinaryOperator() {
@Override
public T apply(T a, T b) {
return comparator.compare(a, b) <= 0 ? a : b;
}
};
}
/**
* Returns a {@code BinaryOperator} which returns 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
*/
public static BinaryOperator maxBy(final Comparator super T> comparator) {
Objects.requireNonNull(comparator);
return new BinaryOperator() {
@Override
public T apply(T a, T b) {
return comparator.compare(a, b) >= 0 ? a : b;
}
};
}
}
}