Search in sources :

Example 56 with AviatorObject

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);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Date(java.util.Date)

Example 57 with AviatorObject

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);
}
Also used : AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) List(java.util.List) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Map(java.util.Map)

Example 58 with AviatorObject

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);
    }
}
Also used : AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 59 with AviatorObject

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);
}
Also used : ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString)

Example 60 with AviatorObject

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;
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Aggregations

AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)110 Test (org.junit.Test)51 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)25 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)22 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)16 HashMap (java.util.HashMap)12 Map (java.util.Map)11 List (java.util.List)10 AbstractFunction (com.googlecode.aviator.runtime.function.AbstractFunction)9 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)7 Env (com.googlecode.aviator.utils.Env)6 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)5 LinkedList (java.util.LinkedList)5 Collection (java.util.Collection)4 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)3 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)3 AviatorRuntimeJavaType (com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType)3 Sequence (com.googlecode.aviator.runtime.type.Sequence)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3