com.jnape.palatable.lambda.semigroup.builtin.MaxBy 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 static com.jnape.palatable.lambda.functions.builtin.fn3.LTBy.ltBy;
/**
* Given a mapping function from some type A
to some {@link Comparable} type B
, 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
* @param the mapped comparison type
* @see Max
* @see MaxWith
* @see MinBy
*/
public final class MaxBy> implements SemigroupFactory, A> {
private static final MaxBy, ?> INSTANCE = new MaxBy<>();
private MaxBy() {
}
@Override
public Semigroup checkedApply(Fn1 super A, ? extends B> compareFn) {
return (x, y) -> ltBy(compareFn, y, x) ? y : x;
}
@SuppressWarnings("unchecked")
public static > MaxBy maxBy() {
return (MaxBy) INSTANCE;
}
public static > Semigroup maxBy(Fn1 super A, ? extends B> compareFn) {
return MaxBy.maxBy().apply(compareFn);
}
public static > Fn1 maxBy(Fn1 super A, ? extends B> compareFn, A x) {
return MaxBy.maxBy(compareFn).apply(x);
}
public static > A maxBy(Fn1 super A, ? extends B> compareFn, A x, A y) {
return maxBy(compareFn, x).apply(y);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy