All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.annimon.stream.function.BinaryOperator Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
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 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 comparator) {
            Objects.requireNonNull(comparator);
            return new BinaryOperator() {
                @Override
                public T apply(T a, T b) {
                    return comparator.compare(a, b) >= 0 ? a : b;
                }
            };
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy