use of com.googlecode.aviator.runtime.type.AviatorObject in project rebuild by getrebuild.
the class DateDiffFunction method call.
@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
Object o = arg1.getValue(env);
Date $date1 = o instanceof Date ? (Date) o : CalendarUtils.parse(o.toString());
if ($date1 == null) {
return AviatorNil.NIL;
}
o = arg2.getValue(env);
Date $date2 = o instanceof Date ? (Date) o : CalendarUtils.parse(o.toString());
if ($date2 == null) {
return AviatorNil.NIL;
}
final String $du = arg3.getValue(env) == null ? null : arg3.getValue(env).toString();
if (isUseMysql) {
String mysqlUnit = "DAY";
if (AviatorDate.DU_YEAR.equalsIgnoreCase($du))
mysqlUnit = "YEAR";
else if (AviatorDate.DU_MONTH.equalsIgnoreCase($du))
mysqlUnit = "MONTH";
else if (AviatorDate.DU_HOUR.equalsIgnoreCase($du))
mysqlUnit = "HOUR";
else if (AviatorDate.DU_MINUTE.equalsIgnoreCase($du))
mysqlUnit = "MINUTE";
// 利用 MySQL 计算,可预期
String mysql = String.format("select TIMESTAMPDIFF(%s, '%s', '%s')", mysqlUnit, CalendarUtils.getUTCDateTimeFormat().format($date1), CalendarUtils.getUTCDateTimeFormat().format($date2));
Object[] res = Application.getPersistManagerFactory().createNativeQuery(mysql).unique();
return AviatorLong.valueOf(ObjectUtils.toLong(res[0]));
} else {
long res = 0;
if (AviatorDate.DU_YEAR.equalsIgnoreCase($du))
res = DateUtil.betweenYear($date1, $date2, true);
else if (AviatorDate.DU_MONTH.equalsIgnoreCase($du))
res = DateUtil.betweenMonth($date1, $date2, true);
else if (AviatorDate.DU_DAY.equalsIgnoreCase($du))
res = DateUtil.betweenDay($date1, $date2, true);
else if (AviatorDate.DU_HOUR.equalsIgnoreCase($du))
res = DateUtil.between($date1, $date2, DateUnit.HOUR, false);
else if (AviatorDate.DU_MINUTE.equalsIgnoreCase($du))
res = DateUtil.between($date1, $date2, DateUnit.MINUTE, false);
return AviatorLong.valueOf(res);
}
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project graphql-calculator by graphql-calculator.
the class FindOne method call.
@Override
public AviatorObject call(Map<String, Object> env, AviatorObject listElement, AviatorObject elementFieldName, AviatorObject envKey) {
String elementKey = ((AviatorString) elementFieldName).getLexeme(Collections.emptyMap());
String envLexeme = ((AviatorString) envKey).getLexeme(Collections.emptyMap());
Object targetValue = env.get(envLexeme);
List<Map> listValue = (List) listElement.getValue(Collections.emptyMap());
Map result = listValue.stream().filter(map -> Objects.equals(map.get(elementKey), targetValue)).findFirst().orElse(null);
return AviatorRuntimeJavaType.valueOf(result);
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class IsAFunction method call.
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
Object obj = arg1.getValue(env);
if (obj == null) {
return AviatorBoolean.FALSE;
}
if (arg2.getAviatorType() != AviatorType.JavaType) {
throw new IllegalArgumentException("Invalid class type: " + arg2.desc(env));
}
try {
Class<?> clazz = null;
final String name = ((AviatorJavaType) arg2).getName();
if (TypeUtils.PRIMITIVE_TYPES.containsKey(name)) {
clazz = TypeUtils.PRIMITIVE_TYPES.get(name);
} else {
clazz = ((Env) env).resolveClassSymbol(name, false);
}
return AviatorBoolean.valueOf(clazz.isInstance(obj));
} catch (ClassNotFoundException e) {
throw Reflector.sneakyThrow(e);
}
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class StringJoinFunction method call.
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
Object target = arg1.getValue(env);
String split = FunctionUtils.getStringValue(arg2, env);
if (target == null) {
throw new ExpressionRuntimeException("Could not replace with null string");
}
return join(env, arg1, target, split);
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class AbstractMinMaxFunction method variadicCall.
@Override
public AviatorObject variadicCall(final Map<String, Object> env, final AviatorObject... args) {
if (args == null || args.length == 0) {
return AviatorNil.NIL;
}
boolean wasFirst = true;
AviatorObject result = AviatorNil.NIL;
for (AviatorObject obj : args) {
result = compareObjects(env, result, obj, wasFirst);
if (wasFirst) {
wasFirst = false;
}
if (getOp() == Op.Min && result.isNull(env)) {
break;
}
}
return result;
}
Aggregations