Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCT...
If you think the Android project NexusData listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.github.dkharrat.nexusdata.predicate;
//www.java2s.comimport com.github.dkharrat.nexusdata.utils.ObjectUtil;
import java.math.BigDecimal;
import java.math.BigInteger;
publicclass ComparisonPredicate implements Predicate {
publicenum Operator {
EQUAL,
NOT_EQUAL,
GREATER_THAN,
GREATER_THAN_OR_EQUAL,
LESS_THAN,
LESS_THAN_OR_EQUAL,
}
privatefinal Expression<?> lhs, rhs;
privatefinal Operator op;
public ComparisonPredicate(Expression<?> lhs, Operator op, Expression<?> rhs) {
this.lhs = lhs;
this.op = op;
this.rhs = rhs;
}
public Expression<?> getLhs() {
return lhs;
}
public Operator getOperator() {
return op;
}
public Expression<?> getRhs() {
return rhs;
}
@Override
public <T> T accept(ExpressionVisitor<T> visitor) {
return visitor.visit(this);
}
@Override
public Boolean evaluate(Object object) {
Object lhsValue = lhs.evaluate(object);
Object rhsValue = rhs.evaluate(object);
if (op == Operator.EQUAL) {
return ObjectUtil.objectsEqual(lhsValue, rhsValue);
} elseif (op == Operator.NOT_EQUAL) {
return !ObjectUtil.objectsEqual(lhsValue, rhsValue);
}
if (!(lhsValue instanceof Number) || !(rhsValue instanceof Number)) {
thrownew IllegalArgumentException("Cannot compare non-Numbers");
}
Number lhsNumber = (Number)lhsValue;
Number rhsNumber = (Number)rhsValue;
int comparison = toBigDecimal(lhsNumber).compareTo(toBigDecimal(rhsNumber));
switch (op) {
case GREATER_THAN:
return comparison >= 1;
case GREATER_THAN_OR_EQUAL:
return comparison >= 1 || comparison == 0;
case LESS_THAN:
return comparison <= -1;
case LESS_THAN_OR_EQUAL:
return comparison <= -1 || comparison == 0;
default:
thrownew UnsupportedOperationException("Not implemented yet");
}
}
privatestatic BigDecimal toBigDecimal(final Number number) {
if(number instanceof BigDecimal)
return (BigDecimal) number;
if(number instanceof BigInteger)
returnnew BigDecimal((BigInteger) number);
if(number instanceof Byte || number instanceof Short
|| number instanceofInteger || number instanceof Long)
returnnew BigDecimal(number.longValue());
if(number instanceof Float || number instanceof Double)
returnnew BigDecimal(number.doubleValue());
try {
returnnew BigDecimal(number.toString());
} catch(final NumberFormatException e) {
thrownew RuntimeException("The given number (\"" + number
+ "\" of class " + number.getClass().getName()
+ ") does not have a parsable string representation", e);
}
}
@Override
public String toString() {
return"(" + lhs + " " + op + " " + rhs + ")";
}
@Override
publicboolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ComparisonPredicate that = (ComparisonPredicate) o;
if (!lhs.equals(that.lhs)) return false;
if (op != that.op) return false;
if (!rhs.equals(that.rhs)) return false;
return true;
}
@Override
publicint hashCode() {
int result = lhs.hashCode();
result = 31 * result + rhs.hashCode();
result = 31 * result + op.hashCode();
return result;
}
}