
org.joo.libra.common.BinaryPredicate Maven / Gradle / Ivy
package org.joo.libra.common;
import org.joo.libra.PredicateContext;
/**
* Abstract class for any predicate with 2 inputs. It wraps the evaluation with
* following conditions:
*
*
* - - If both objects are null, it will return true
* - - If one object is null, but the other is not, it will return false
*
*
* If none of the above conditions are matched, it will let the subclasses
* determine.
*
* @author griever
*
*/
public abstract class BinaryPredicate implements CompositionPredicate {
private HasValue one;
private HasValue other;
public BinaryPredicate(final HasValue one, final HasValue other) {
this.one = one;
this.other = other;
}
@Override
public boolean satisfiedBy(final PredicateContext context) {
T theOne = one != null ? one.getValue(context) : null;
H theOther = other != null ? other.getValue(context) : null;
if (theOne == null && theOther == null)
return true;
if (theOne == null || theOther == null)
return false;
return doSatisifiedBy(theOne, theOther);
}
protected abstract boolean doSatisifiedBy(T one, H other);
public String toString() {
String name = getClass().getSimpleName().replaceAll("Predicate", "").toUpperCase();
return name + "(" + one + "," + other + ")";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy