Search in sources :

Example 41 with AviatorJavaType

use of com.googlecode.aviator.runtime.type.AviatorJavaType in project aviatorscript by killme2008.

the class SeqFilterFunction method call.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    Object first = arg1.getValue(env);
    AviatorFunction fun = FunctionUtils.getFunction(arg2, env, 1);
    if (fun == null) {
        throw new FunctionNotFoundException("There is no function named " + ((AviatorJavaType) arg2).getName());
    }
    if (first == null) {
        return AviatorNil.NIL;
    }
    Sequence seq = RuntimeUtils.seq(first, env);
    Collector collector = seq.newCollector(0);
    for (Object obj : seq) {
        if (fun.call(env, AviatorRuntimeJavaType.valueOf(obj)).booleanValue(env)) {
            collector.add(obj);
        }
    }
    return AviatorRuntimeJavaType.valueOf(collector.getRawContainer());
}
Also used : FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Collector(com.googlecode.aviator.runtime.type.Collector) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Sequence(com.googlecode.aviator.runtime.type.Sequence)

Example 42 with AviatorJavaType

use of com.googlecode.aviator.runtime.type.AviatorJavaType in project aviatorscript by killme2008.

the class SeqNotAnyFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    Object first = arg1.getValue(env);
    AviatorFunction fun = FunctionUtils.getFunction(arg2, env, 1);
    if (fun == null) {
        throw new FunctionNotFoundException("There is no function named " + ((AviatorJavaType) arg2).getName());
    }
    if (first == null) {
        return AviatorBoolean.TRUE;
    }
    for (Object obj : RuntimeUtils.seq(first, env)) {
        if (fun.call(env, AviatorRuntimeJavaType.valueOf(obj)).booleanValue(env)) {
            return AviatorBoolean.FALSE;
        }
    }
    return AviatorBoolean.TRUE;
}
Also used : FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 43 with AviatorJavaType

use of com.googlecode.aviator.runtime.type.AviatorJavaType in project aviatorscript by killme2008.

the class SeqReduceFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3) {
    Object first = arg1.getValue(env);
    AviatorFunction fun = FunctionUtils.getFunction(arg2, env, 2);
    if (fun == null) {
        throw new FunctionNotFoundException("There is no function named " + ((AviatorJavaType) arg2).getName());
    }
    if (first == null) {
        return arg3;
    }
    AviatorObject result = arg3;
    for (Object obj : RuntimeUtils.seq(first, env)) {
        result = fun.call(env, result, AviatorRuntimeJavaType.valueOf(obj));
    }
    return result;
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 44 with AviatorJavaType

use of com.googlecode.aviator.runtime.type.AviatorJavaType in project aviatorscript by killme2008.

the class SeqSomeFunction method call.

@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    Object first = arg1.getValue(env);
    AviatorFunction fun = FunctionUtils.getFunction(arg2, env, 1);
    if (fun == null) {
        throw new FunctionNotFoundException("There is no function named " + ((AviatorJavaType) arg2).getName());
    }
    if (first == null) {
        return AviatorNil.NIL;
    }
    for (Object obj : RuntimeUtils.seq(first, env)) {
        // return first fun returns true element.
        if (fun.call(env, AviatorRuntimeJavaType.valueOf(obj)).booleanValue(env)) {
            return AviatorRuntimeJavaType.valueOf(obj);
        }
    }
    // else return nil
    return AviatorNil.NIL;
}
Also used : FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject)

Example 45 with AviatorJavaType

use of com.googlecode.aviator.runtime.type.AviatorJavaType in project aviatorscript by killme2008.

the class SeqMapFunction method call.

@Override
@SuppressWarnings({ "rawtypes" })
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
    Object first = arg1.getValue(env);
    AviatorFunction fun = FunctionUtils.getFunction(arg2, env, 1);
    if (fun == null) {
        throw new FunctionNotFoundException("There is no function named " + ((AviatorJavaType) arg2).getName());
    }
    if (first == null) {
        return AviatorNil.NIL;
    }
    Sequence seq = RuntimeUtils.seq(first, env);
    Collector collector = seq.newCollector(seq.hintSize());
    for (Object obj : seq) {
        collector.add(fun.call(env, AviatorRuntimeJavaType.valueOf(obj)).getValue(env));
    }
    return AviatorRuntimeJavaType.valueOf(collector.getRawContainer());
}
Also used : FunctionNotFoundException(com.googlecode.aviator.exception.FunctionNotFoundException) AviatorFunction(com.googlecode.aviator.runtime.type.AviatorFunction) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Collector(com.googlecode.aviator.runtime.type.Collector) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Sequence(com.googlecode.aviator.runtime.type.Sequence)

Aggregations

AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)45 Test (org.junit.Test)29 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)25 HashMap (java.util.HashMap)16 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)11 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)10 Env (com.googlecode.aviator.utils.Env)10 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)6 LinkedList (java.util.LinkedList)4 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 FunctionParam (com.googlecode.aviator.runtime.FunctionParam)2 BinaryFunction (com.googlecode.aviator.runtime.function.system.BinaryFunction)2 Collector (com.googlecode.aviator.runtime.type.Collector)2 Sequence (com.googlecode.aviator.runtime.type.Sequence)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 CodeGenerator (com.googlecode.aviator.code.CodeGenerator)1