Examples of AviatorFunction


Examples of com.googlecode.aviator.runtime.type.AviatorFunction

        Map<String, Object> env = new HashMap<String, Object>();
        AviatorJavaType predicateName = (AviatorJavaType) fun.call(env, args);

        assertNotNull(predicateName);
        assertEquals(1, env.size());
        AviatorFunction predicate = (AviatorFunction) env.get(predicateName.getName());
        assertNotNull(predicate);
        AviatorObject result = predicate.call(null, args);
        // equals self
        assertTrue(result.booleanValue(null));

    }
View Full Code Here

Examples of com.googlecode.aviator.runtime.type.AviatorFunction

        Map<String, Object> env = new HashMap<String, Object>();
        AviatorJavaType predicateName = (AviatorJavaType) fun.call(env);

        assertNotNull(predicateName);
        assertEquals(1, env.size());
        AviatorFunction predicate = (AviatorFunction) env.get(predicateName.getName());
        assertNotNull(predicate);
        AviatorObject[] args = new AviatorObject[1];
        args[0] = new AviatorRuntimeJavaType("hello");
        AviatorObject result = predicate.call(null, args);
        // equals self
        assertTrue(result.booleanValue(null));

        args[0] = new AviatorRuntimeJavaType("he11o");
        result = predicate.call(null, args);
        // equals self
        assertFalse(result.booleanValue(null));

    }
View Full Code Here

Examples of com.googlecode.aviator.runtime.type.AviatorFunction

    public AviatorObject call(Map<String, Object> env, AviatorObject... args) {
        if (args.length != 2) {
            throw new IllegalArgumentException("filter(seq,pred)");
        }
        Object first = args[0].getValue(env);
        AviatorFunction fun = FunctionUtils.getFunction(1, args, env, 1);
        if (fun == null) {
            throw new ExpressionRuntimeException("There is no function named " + ((AviatorJavaType) args[1]).getName());
        }
        if (first == null) {
            throw new NullPointerException("null seq");
        }
        Class<?> clazz = first.getClass();

        if (Collection.class.isAssignableFrom(clazz)) {
            Collection result = null;
            try {
                result = (Collection) clazz.newInstance();
            }
            catch (Throwable t) {
                // ignore
                result = new ArrayList();
            }
            for (Object obj : (Collection<?>) first) {
                if (fun.call(env, new AviatorRuntimeJavaType(obj)).booleanValue(env)) {
                    result.add(obj);
                }
            }
            return new AviatorRuntimeJavaType(result);
        }
        else if (clazz.isArray()) {
            Object[] seq = (Object[]) first;
            List result = new ArrayList();
            for (Object obj : seq) {
                if (fun.call(env, new AviatorRuntimeJavaType(obj)).booleanValue(env)) {
                    result.add(obj);
                }
            }
            return new AviatorRuntimeJavaType(result.toArray());
        }
View Full Code Here

Examples of com.googlecode.aviator.runtime.type.AviatorFunction

    public AviatorObject call(Map<String, Object> env, AviatorObject... args) {
        if (args.length != 3) {
            throw new IllegalArgumentException("reduce(seq,fun,init)");
        }
        Object first = args[0].getValue(env);
        AviatorFunction fun = FunctionUtils.getFunction(1, args, env, 2);
        if (fun == null) {
            throw new ExpressionRuntimeException("There is no function named " + ((AviatorJavaType) args[1]).getName());
        }
        if (first == null) {
            throw new NullPointerException("null seq");
        }
        AviatorObject result = args[2];
        Class<?> clazz = first.getClass();

        if (Collection.class.isAssignableFrom(clazz)) {
            for (Object obj : (Collection<?>) first) {
                result = fun.call(env, result, new AviatorRuntimeJavaType(obj));
            }
        }
        else if (clazz.isArray()) {
            Object[] seq = (Object[]) first;
            for (Object obj : seq) {
                result = fun.call(env, result, new AviatorRuntimeJavaType(obj));
            }
        }
        else {
            throw new IllegalArgumentException(args[0].desc(env) + " is not a seq");
        }
View Full Code Here

Examples of com.googlecode.aviator.runtime.type.AviatorFunction


    public AviatorObject call(Map<String, Object> env, AviatorObject... args) {

        // generate a temp function object as predicate
        AviatorFunction fun = new SeqPredicateFunction(name, opType, value == null ? args[0] : value);
        final String funName = name + "_tmp_" + System.nanoTime();
        env.put(funName, fun);
        return new AviatorJavaType(funName);
    }
View Full Code Here

Examples of com.googlecode.aviator.runtime.type.AviatorFunction

    public AviatorObject call(Map<String, Object> env, AviatorObject... args) {
        if (args.length != 2) {
            throw new IllegalArgumentException("map(seq,fun)");
        }
        Object first = args[0].getValue(env);
        AviatorFunction fun = FunctionUtils.getFunction(1, args, env, 1);
        if (fun == null) {
            throw new ExpressionRuntimeException("There is no function named " + ((AviatorJavaType) args[1]).getName());
        }
        if (first == null) {
            throw new NullPointerException("null seq");
        }
        Class<?> clazz = first.getClass();

        if (Collection.class.isAssignableFrom(clazz)) {
            Collection result = null;
            try {
                result = (Collection) clazz.newInstance();
            }
            catch (Throwable t) {
                // ignore
                result = new ArrayList();
            }
            for (Object obj : (Collection<?>) first) {
                result.add(fun.call(env, new AviatorRuntimeJavaType(obj)).getValue(env));
            }
            return new AviatorRuntimeJavaType(result);
        }
        else if (clazz.isArray()) {
            Object[] seq = (Object[]) first;
            Object result = Array.newInstance(Object.class, seq.length);
            int index = 0;
            for (Object obj : seq) {
                Array.set(result, index++, fun.call(env, new AviatorRuntimeJavaType(obj)).getValue(env));
            }
            return new AviatorRuntimeJavaType(result);
        }
        else {
            throw new IllegalArgumentException(args[0].desc(env) + " is not a seq");
View Full Code Here

Examples of com.googlecode.aviator.runtime.type.AviatorFunction

    @Test
    public void testGetFunction_Normal() {

        AviatorObject[] args = new AviatorObject[1];
        args[0] = new AviatorJavaType("map");
        AviatorFunction fun = FunctionUtils.getFunction(0, args, AviatorEvaluator.FUNC_MAP, 2);
        assertNotNull(fun);
        assertTrue(fun instanceof SeqMapFunction);
    }
View Full Code Here

Examples of com.googlecode.aviator.runtime.type.AviatorFunction

    @Test
    public void testGetFunction_sub() {
        AviatorObject[] args = new AviatorObject[1];
        args[0] = new AviatorJavaType("-");
        AviatorFunction fun = FunctionUtils.getFunction(0, args, AviatorEvaluator.FUNC_MAP, 2);
        assertNotNull(fun);
        assertTrue(fun instanceof BinaryFunction);
        assertEquals("-sub", fun.getName());
        assertEquals(OperatorType.SUB, ((BinaryFunction) fun).getOpType());

    }
View Full Code Here

Examples of com.googlecode.aviator.runtime.type.AviatorFunction

    @Test
    public void testGetFunction_neg() {
        AviatorObject[] args = new AviatorObject[1];
        args[0] = new AviatorJavaType("-");
        AviatorFunction fun = FunctionUtils.getFunction(0, args, AviatorEvaluator.FUNC_MAP, 1);
        assertNotNull(fun);
        assertTrue(fun instanceof BinaryFunction);
        assertEquals("-neg", fun.getName());
        assertEquals(OperatorType.NEG, ((BinaryFunction) fun).getOpType());

    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.