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

com.jnape.palatable.lambda.functions.builtin.fn3.CmpEqBy Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
package com.jnape.palatable.lambda.functions.builtin.fn3;

import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.Fn3;
import com.jnape.palatable.lambda.functions.builtin.fn2.CmpEq;
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
import com.jnape.palatable.lambda.functions.specialized.Predicate;

import static com.jnape.palatable.lambda.functions.specialized.Predicate.predicate;

/**
 * Given a mapping function from some type A to some {@link Comparable} type B and two values
 * of type A, return true if the first value is strictly equal to the second value (according
 * to {@link Comparable#compareTo(Object)} in terms of their mapped B results; otherwise, return false.
 *
 * @param  the value type
 * @param  the mapped comparison type
 * @see CmpEq
 * @see LTBy
 * @see GTBy
 */
public final class CmpEqBy> implements Fn3, A, A, Boolean> {

    private static final CmpEqBy INSTANCE = new CmpEqBy<>();

    private CmpEqBy() {
    }

    @Override
    public Boolean checkedApply(Fn1 compareFn, A x, A y) {
        return compareFn.apply(x).compareTo(compareFn.apply(y)) == 0;
    }

    @Override
    public BiPredicate apply(Fn1 compareFn) {
        return Fn3.super.apply(compareFn)::apply;
    }

    @Override
    public Predicate apply(Fn1 compareFn, A x) {
        return predicate(Fn3.super.apply(compareFn, x));
    }

    @SuppressWarnings("unchecked")
    public static > CmpEqBy cmpEqBy() {
        return (CmpEqBy) INSTANCE;
    }

    public static > BiPredicate cmpEqBy(Fn1 compareFn) {
        return CmpEqBy.cmpEqBy().apply(compareFn);
    }

    public static > Predicate cmpEqBy(Fn1 compareFn, A x) {
        return CmpEqBy.cmpEqBy(compareFn).apply(x);
    }

    public static > Boolean cmpEqBy(Fn1 compareFn, A x, A y) {
        return cmpEqBy(compareFn, x).apply(y);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy