use of com.googlecode.aviator.runtime.function.AbstractFunction in project aviatorscript by killme2008.
the class SeqCompsitePredFunFunction method createFunction.
private static AviatorFunction createFunction(final Map<String, Object> env, final AviatorObject[] args, final LogicOp op) {
return new AbstractFunction() {
private static final long serialVersionUID = -3935448162130773442L;
@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
switch(op) {
case AND:
boolean ret = true;
for (AviatorObject obj : args) {
AviatorFunction fn = FunctionUtils.getFunction(obj, env, 1);
if (fn == null)
throw new IllegalArgumentException("Expect " + obj.desc(env) + " as a function.");
ret = fn.call(env, arg1) == AviatorBoolean.TRUE;
if (!ret)
break;
}
return AviatorBoolean.valueOf(ret);
case OR:
ret = false;
for (AviatorObject obj : args) {
AviatorFunction fn = FunctionUtils.getFunction(obj, env, 1);
if (fn == null)
throw new IllegalArgumentException("Expect " + obj.desc(env) + " as a function.");
ret = fn.call(env, arg1) == AviatorBoolean.TRUE;
if (ret)
break;
}
return AviatorBoolean.valueOf(ret);
}
return AviatorBoolean.FALSE;
}
@Override
public String getName() {
return op == LogicOp.AND ? "seq.and" : "seq.or";
}
};
}
use of com.googlecode.aviator.runtime.function.AbstractFunction in project aviatorscript by killme2008.
the class OperatorFunctionTest method testCustomArrayIndexOperator.
@Test
public void testCustomArrayIndexOperator() {
Map<String, Object> env = new HashMap<>();
env.put("a", Arrays.asList(4, 5, 6));
assertEquals(4, this.instance.execute("a[0]", env));
try {
assertEquals(6, this.instance.execute("a[3]", env));
fail();
} catch (Exception e) {
}
this.instance.addOpFunction(OperatorType.INDEX, new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
return arg1.getElement(env, arg2.sub(AviatorLong.valueOf(1), env));
}
@Override
public String getName() {
return "[]";
}
});
assertEquals(6, this.instance.execute("a[3]", env));
assertEquals(5, this.instance.execute("a[2]", env));
assertEquals(4, this.instance.execute("a[1]", env));
try {
assertEquals(4, this.instance.execute("a[0]", env));
fail();
} catch (Exception e) {
}
}
use of com.googlecode.aviator.runtime.function.AbstractFunction in project aviatorscript by killme2008.
the class OperatorFunctionTest method testCustomBinOperatorFunction.
@Test
public void testCustomBinOperatorFunction() {
try {
assertEquals("hello world", this.instance.execute("'hello' & ' world'"));
fail();
} catch (Exception e) {
}
this.instance.addOpFunction(OperatorType.BIT_AND, new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
return new AviatorString(arg1.getValue(env).toString() + arg2.getValue(env).toString());
}
@Override
public String getName() {
return "&";
}
});
assertEquals("43", this.instance.exec("a&3", 4));
assertEquals("43", this.instance.exec("4&3", 4));
assertEquals("hello world", this.instance.execute("'hello' & ' world'"));
assertEquals("hello world", this.instance.exec("a&' world'", "hello"));
assertEquals("hello3 world", this.instance.exec("a & 3 & ' world'", "hello"));
Map<String, Object> env = new HashMap<>();
env.put("list", Arrays.asList(1, 2, 3));
assertEquals("123", this.instance.execute("reduce(list, &, '')", env));
}
Aggregations