com.jnape.palatable.lambda.semigroup.builtin.MaxWith Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lambda Show documentation
Show all versions of lambda Show documentation
Functional patterns for Java
package com.jnape.palatable.lambda.semigroup.builtin;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.specialized.SemigroupFactory;
import com.jnape.palatable.lambda.semigroup.Semigroup;
import java.util.Comparator;
import static com.jnape.palatable.lambda.functions.builtin.fn3.LTWith.ltWith;
/**
* Given a comparator for some type A
, produce a {@link Semigroup} over A
that chooses
* between two values x
and y
via the following rules:
*
* - If
x
is strictly less than y
in terms of B
, return y
* - Otherwise, return
x
*
*
* @param the value type
* @see Max
* @see MaxBy
* @see MinWith
*/
public final class MaxWith implements SemigroupFactory, A> {
private static final MaxWith> INSTANCE = new MaxWith<>();
private MaxWith() {
}
@SuppressWarnings("unchecked")
public static MaxWith maxWith() {
return (MaxWith) INSTANCE;
}
public static Semigroup maxWith(Comparator compareFn) {
return MaxWith.maxWith().apply(compareFn);
}
public static Fn1 maxWith(Comparator compareFn, A x) {
return MaxWith.maxWith(compareFn).apply(x);
}
public static A maxWith(Comparator compareFn, A x, A y) {
return maxWith(compareFn, x).apply(y);
}
@Override
public Semigroup checkedApply(Comparator comparator) {
return (x, y) -> ltWith(comparator, y, x) ? y : x;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy