Search in sources :

Example 1 with Sequence

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

the class AbstractSeqMinMaxFunction method call.

@SuppressWarnings("rawtypes")
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
    Object first = arg1.getValue(env);
    if (first == null) {
        return AviatorNil.NIL;
    }
    Sequence seq = RuntimeUtils.seq(first, env);
    boolean wasFirst = true;
    Object result = null;
    for (Object obj : seq) {
        result = compareObjects(result, obj, wasFirst);
        if (wasFirst) {
            wasFirst = false;
        }
        if (getOp() == Op.Min && result == null) {
            break;
        }
    }
    return AviatorRuntimeJavaType.valueOf(result);
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Sequence(com.googlecode.aviator.runtime.type.Sequence)

Example 2 with Sequence

use of com.googlecode.aviator.runtime.type.Sequence 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 3 with Sequence

use of com.googlecode.aviator.runtime.type.Sequence 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

AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)3 Sequence (com.googlecode.aviator.runtime.type.Sequence)3 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)2 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)2 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)2 Collector (com.googlecode.aviator.runtime.type.Collector)2