How to compare dates

13 views
Skip to first unread message

gattin...@googlemail.com

unread,
Nov 13, 2017, 10:21:38 AM11/13/17
to Jep Java Users
Hi,

given the following expression "x < y" with variables x and y are values of type java.util.Date, Jep complains about the unsupported comparison:

Caused by: com.singularsys.jep.EvaluationException: < not defined for objects of type Date and Date
	at com.singularsys.jep.functions.Comparative.lt(Unknown Source)
	at com.singularsys.jep.functions.Comparative.compare(Unknown Source)
	at com.singularsys.jep.functions.Comparative.eval(Unknown Source)
	at com.singularsys.jep.standard.FastEvaluator.visitFun(Unknown Source)
	at com.singularsys.jep.standard.FastEvaluator.nodeAccept(Unknown Source)
	at com.singularsys.jep.standard.FastEvaluator.evaluate(Unknown Source)
	at com.singularsys.jep.Jep.evaluate(Unknown Source)
	at com.singularsys.jep.Jep.evaluate(Unknown Source)

I've already created a class that extends com.singularsys.jep.functions.Comparative and supports date comparisons but I wonder how to intergrate it into Jep?

Any advice for me?

Kind regards,
Marcus

gattin...@googlemail.com

unread,
Nov 13, 2017, 11:00:40 AM11/13/17
to Jep Java Users
Here is what I've tried so far:

public class ExtendedComparative extends Comparative {
/**
* Creates an instance of class {@link ExtendedComparative}.
*
* @param id The id of the comparator to use:<br/>
* 0 = is less than<br/>
* 1 = is greater than<br/>
* 2 = is less than or equal<br/>
* 3 = is greater than or equal<br/>
* 4 = is not equal<br/>
* 5 = is equal<br/>
*/
public ExtendedComparative(int id) {
super(id);
}

/**
* Creates an instance of class {@link ExtendedComparative}.
*
* @param id The id of the comparator to use:<br/>
* 0 = is less than<br/>
* 1 = is greater than<br/>
* 2 = is less than or equal<br/>
* 3 = is greater than or equal<br/>
* 4 = is not equal<br/>
* 5 = is equal<br/>
* @param tolerance The tolerance that is acceptable to consider the comparison to be successful.
*/
public ExtendedComparative(int id, double tolerance) {
super(id, tolerance);
}

@Override
public boolean lt(final Object param1, final Object param2) throws EvaluationException {
if (param1 instanceof Date && param2 instanceof Date) {
// Important: DO NOT USE java.util.Date.before() or java.util.Date.after() here, because it does not work correctly if the dates to compare differ
// only by milliseconds.
return ((Date) param1).getTime() < ((Date) param2).getTime();
} else if (param1 instanceof Date && param2 instanceof Number) {
return ((Date) param1).getTime() < ((Number) param2).longValue();
} else if (param1 instanceof Number && param2 instanceof Date) {
return ((Number) param1).longValue() < ((Date) param2).getTime();
}

return super.lt(param1, param2);
}

@Override
public boolean gt(final Object param1, final Object param2) throws EvaluationException {
if (param1 instanceof Date && param2 instanceof Date) {
// Important: DO NOT USE java.util.Date.before() or java.util.Date.after() here, because it does not work correctly if the dates to compare differ
// only by milliseconds.
return ((Date) param1).getTime() > ((Date) param2).getTime();
} else if (param1 instanceof Date && param2 instanceof Number) {
return ((Date) param1).getTime() > ((Number) param2).longValue();
} else if (param1 instanceof Number && param2 instanceof Date) {
return ((Number) param1).longValue() > ((Date) param2).getTime();
}

return super.gt(param1, param2);
}

@Override
public boolean le(final Object param1, final Object param2) throws EvaluationException {
if (param1 instanceof Date && param2 instanceof Date) {
// Important: DO NOT USE java.util.Date.before() or java.util.Date.after() here, because it does not work correctly if the dates to compare differ
// only by milliseconds.
return ((Date) param1).getTime() <= ((Date) param2).getTime();
} else if (param1 instanceof Date && param2 instanceof Number) {
return ((Date) param1).getTime() <= ((Number) param2).longValue();
} else if (param1 instanceof Number && param2 instanceof Date) {
return ((Number) param1).longValue() <= ((Date) param2).getTime();
}

return super.le(param1, param2);
}

@Override
public boolean ge(final Object param1, final Object param2) throws EvaluationException {
if (param1 instanceof Date && param2 instanceof Date) {
// Important: DO NOT USE java.util.Date.before() or java.util.Date.after() here, because it does not work correctly if the dates to compare differ
// only by milliseconds.
return ((Date) param1).getTime() >= ((Date) param2).getTime();
} else if (param1 instanceof Date && param2 instanceof Number) {
return ((Date) param1).getTime() >= ((Number) param2).longValue();
} else if (param1 instanceof Number && param2 instanceof Date) {
return ((Number) param1).longValue() >= ((Date) param2).getTime();
}

return super.ge(param1, param2);
}

@Override
public boolean eq(final Object param1, final Object param2) throws EvaluationException {
if (param1 instanceof Date && param2 instanceof Date) {
// Important: DO NOT USE java.util.Date.before() or java.util.Date.after() here, because it does not work correctly if the dates to compare differ
// only by milliseconds.
return ((Date) param1).getTime() == ((Date) param2).getTime();
} else if (param1 instanceof Date && param2 instanceof Number) {
return ((Date) param1).getTime() == ((Number) param2).longValue();
} else if (param1 instanceof Number && param2 instanceof Date) {
return ((Number) param1).longValue() == ((Date) param2).getTime();
}

return super.eq(param1, param2);
}

@Override
public boolean ne(final Object param1, final Object param2) throws EvaluationException {
if (param1 instanceof Date && param2 instanceof Date) {
// Important: DO NOT USE java.util.Date.before() or java.util.Date.after() here, because it does not work correctly if the dates to compare differ
// only by milliseconds.
return ((Date) param1).getTime() != ((Date) param2).getTime();
} else if (param1 instanceof Date && param2 instanceof Number) {
return ((Date) param1).getTime() != ((Number) param2).longValue();
} else if (param1 instanceof Number && param2 instanceof Date) {
return ((Number) param1).longValue() != ((Date) param2).getTime();
}

return super.ne(param1, param2);
}
}


private
OperatorTable2 operatorsTable;

operatorsTable
.addOperator(AdditionalOperatorKeys.EQ2, new Operator("EQ2", "=", new ExtendedComparative(Comparative.EQ), Operator.BINARY + Operator.LEFT + Operator.EQUIVALENCE));
operatorsTable.getOperator(AdditionalOperatorKeys.EQ2).setPrecedence(6);
operatorsTable.addOperator(AdditionalOperatorKeys.NE2, new Operator("NE2", "<>", new ExtendedComparative(Comparative.NE), Operator.BINARY + Operator.LEFT + Operator.EQUIVALENCE));
operatorsTable.getOperator(AdditionalOperatorKeys.NE2).setPrecedence(6);
operatorsTable.addOperator(AdditionalOperatorKeys.LT2, new Operator("LT2", "<", new ExtendedComparative(Comparative.LT), Operator.BINARY + Operator.LEFT));
operatorsTable.getOperator(AdditionalOperatorKeys.LT2).setPrecedence(6);
operatorsTable.addOperator(AdditionalOperatorKeys.LE2, new Operator("LE2", "<=", new ExtendedComparative(Comparative.LE), Operator.BINARY + Operator.LEFT));
operatorsTable.getOperator(AdditionalOperatorKeys.LE2).setPrecedence(6);
operatorsTable.addOperator(AdditionalOperatorKeys.GT2, new Operator("GT2", ">", new ExtendedComparative(Comparative.GT), Operator.BINARY + Operator.LEFT));
operatorsTable.getOperator(AdditionalOperatorKeys.GT2).setPrecedence(6);
operatorsTable.addOperator(AdditionalOperatorKeys.GE2, new Operator("GE2", ">=", new ExtendedComparative(Comparative.GE), Operator.BINARY + Operator.LEFT));
operatorsTable.getOperator(AdditionalOperatorKeys.GE2).setPrecedence(6);

ConfigurableParser cp = new ConfigurableParser();
// ... configure cp
Jep jepInstance = new Jep(cp, operatorsTable);
jepInstance.reinitializeComponents();

However, the code in ExtendedComparative is never invoked.

gattin...@googlemail.com

unread,
Nov 13, 2017, 12:42:37 PM11/13/17
to Jep Java Users
Never mind, I've solved the problem thanks to the Operators documentation (modifying an existing operator).
Reply all
Reply to author
Forward
0 new messages