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

io.github.oliviercailloux.jaris.throwing.TBinaryOperator Maven / Gradle / Ivy

The newest version!
package io.github.oliviercailloux.jaris.throwing;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Generalization of {@link java.util.function.BinaryOperator} that may throw instances of type
 * {@code X}, not just {@code RuntimeException} instances.
 *
 * @param  the type of the operands and result of the operator
 * @param  a sort of throwable that the {@code Throwing.BinaryOperator} may throw
 */
@FunctionalInterface
public interface TBinaryOperator extends TBiFunction {
  /**
   * Returns a {@link TBinaryOperator} which returns the lesser of two elements according to the
   * specified {@code Comparator}.
   *
   * @param  the type of the input arguments of the comparator
   * @param  the sort of throwable that the returned instance may throw
   * @param comparator a {@code Throwing.Comparator} for comparing the two values
   * @return a {@code Throwing.BinaryOperator} which returns the lesser of its operands, according
   *         to the supplied {@code Throwing.Comparator}
   */
  public static  TBinaryOperator
      minBy(TComparator comparator) {
    checkNotNull(comparator);
    return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
  }

  /**
   * Returns a {@link TBinaryOperator} which returns the greater of two elements according to the
   * specified {@code Comparator}.
   *
   * @param  the type of the input arguments of the comparator
   * @param  the sort of throwable that the returned instance may throw
   * @param comparator a {@code Throwing.Comparator} for comparing the two values
   * @return a {@code Throwing.BinaryOperator} which returns the greater of its operands, according
   *         to the supplied {@code Throwing.Comparator}
   */
  public static  TBinaryOperator
      maxBy(TComparator comparator) {
    checkNotNull(comparator);
    return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy