use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.
the class Date method evaluate.
@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
String arg = (String) this.getOperands().get(0);
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = null;
try {
date = format.parse(arg);
} catch (Exception e) {
throw new TMLExpressionException("Invalid date format: " + arg);
}
return date;
}
use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.
the class DateSubtract method evaluate.
@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
if (this.getOperands().size() != 2)
throw new TMLExpressionException("DateSubtract(Date1, Date2) expected. Wrong no. of args.");
java.util.Date date1 = (java.util.Date) this.getOperands().get(0);
java.util.Date date2 = (java.util.Date) this.getOperands().get(1);
if (date1 == null || date2 == null) {
return null;
}
long diff = date1.getTime() - date2.getTime();
int hours = (int) (diff / 3600000);
int millis = (int) (diff % 3600000);
Calendar cc = Calendar.getInstance();
cc.clear();
cc.add(Calendar.HOUR_OF_DAY, hours);
cc.add(Calendar.MILLISECOND, millis);
return cc.getTime();
}
use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.
the class DayOfWeek method evaluate.
@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
Object o = this.getOperand(0);
Object f = this.getOperand(1);
if (!(o instanceof Integer))
throw new TMLExpressionException("Invalid operand type, Integer expected");
if (!(f instanceof Boolean))
throw new TMLExpressionException("Invalid operand type, Boolean expected");
Integer weekday = (Integer) o;
Boolean past = (Boolean) f;
Calendar today = Calendar.getInstance();
if (today.get(Calendar.DAY_OF_WEEK) == weekday.intValue())
return reset(today, past.booleanValue());
int factor = (past.booleanValue() ? -1 : 1);
for (int i = 1; i <= 8; i++) {
today.add(Calendar.DAY_OF_WEEK, factor);
if (today.get(Calendar.DAY_OF_WEEK) == weekday.intValue()) {
return reset(today, past.booleanValue());
}
}
return null;
}
use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.
the class FormatDecimal method evaluate.
/* (non-Javadoc)
* @see com.dexels.navajo.parser.FunctionInterface#evaluate()
*/
@Override
public Object evaluate() throws TMLExpressionException {
if (getOperands().size() != 2) {
throw new TMLExpressionException(this, "Invalid number of arguments");
}
Object o1 = getOperand(0);
Object o2 = getOperand(1);
if (!(o2 instanceof String)) {
throw new TMLExpressionException(this, "Invalid argument: " + o2);
}
DecimalFormat df = new DecimalFormat(o2.toString());
return df.format(o1);
}
use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.
the class FormatStringList method evaluate.
@Override
@SuppressWarnings("rawtypes")
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
Object a = this.getOperands().get(0);
Object b = this.getOperands().get(1);
if (a instanceof String)
return a;
if (!(a instanceof List))
throw new TMLExpressionException("FormatStringList: invalid operand: " + a.getClass().getName());
if (!(b instanceof String))
throw new TMLExpressionException("FormatStringList: invalid operand: " + a.getClass().getName());
List strings = (List) a;
String sep = (String) b;
StringBuffer result = new StringBuffer(20 * strings.size());
for (int i = 0; i < strings.size(); i++) {
String el = (String) strings.get(i);
result.append(el);
if (i < (strings.size() - 1))
result.append(sep);
}
return result.toString();
}
Aggregations