use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class SeqPutFunction method call.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2, final AviatorObject arg3) {
Object coll = arg1.getValue(env);
Object key = arg2.getValue(env);
Object val = arg3.getValue(env);
if (coll == null) {
throw new NullPointerException("null seq");
}
Class<?> clazz = coll.getClass();
Object previousVal = null;
if (List.class.isAssignableFrom(clazz)) {
int index = ((Number) key).intValue();
previousVal = ((List) coll).set(index, val);
} else if (Map.class.isAssignableFrom(clazz)) {
previousVal = ((Map) coll).put(key, val);
} else if (clazz.isArray()) {
int index = ((Number) key).intValue();
previousVal = ArrayUtils.get(coll, index);
ArrayUtils.set(coll, index, val);
} else {
throw new IllegalArgumentException(arg1.desc(env) + " can't put elements.");
}
return AviatorRuntimeJavaType.valueOf(previousVal);
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class SeqRemoveFunction method call.
@SuppressWarnings("rawtypes")
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
Object coll = arg1.getValue(env);
Object element = arg2.getValue(env);
if (coll == null) {
throw new NullPointerException("null seq");
}
Class<?> clazz = coll.getClass();
if (Collection.class.isAssignableFrom(clazz)) {
((Collection) coll).remove(element);
return arg1;
} else if (Map.class.isAssignableFrom(clazz)) {
((Map) coll).remove(element);
return arg1;
} else {
throw new IllegalArgumentException(arg1.desc(env) + " is not a collection or map.");
}
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class SeqReverseFunction method call.
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1) {
Object first = arg1.getValue(env);
if (first == null) {
return AviatorNil.NIL;
}
Class<?> clazz = first.getClass();
if (List.class.isAssignableFrom(clazz)) {
List<?> list = (List<?>) first;
Collections.reverse(list);
return arg1;
} else if (clazz.isArray()) {
int length = ArrayUtils.getLength(first);
for (int i = 0; i < length / 2; i++) {
Object temp = ArrayUtils.get(first, i);
ArrayUtils.set(first, i, ArrayUtils.get(first, length - 1 - i));
ArrayUtils.set(first, length - 1 - i, temp);
}
return arg1;
} else {
throw new IllegalArgumentException(arg1.desc(env) + " is not an array or list.");
}
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class OperationRuntime method eval.
/**
* Eval with arguments array.
*
* @param args
* @param opType
* @return
*/
public static AviatorObject eval(final Map<String, Object> env, final AviatorObject[] args, final OperatorType opType) {
AviatorFunction func = RuntimeUtils.getInstance(env).getOpFunction(opType);
AviatorObject ret = eval0(env, args, opType, func);
if (RuntimeUtils.isTracedEval(env)) {
trace(env, opType, ret, args);
}
return ret;
}
use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.
the class SeqSortFunction method call.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1, final AviatorObject arg2) {
Object first = arg1.getValue(env);
Comparator comparator = (Comparator) arg2.getValue(env);
if (first == null) {
return AviatorNil.NIL;
}
if (comparator == null) {
throw new IllegalArgumentException("null comparator");
}
Class<?> clazz = first.getClass();
if (List.class.isAssignableFrom(clazz)) {
List<?> list = (List<?>) first;
Object[] a = list.toArray();
Arrays.sort(a, comparator);
return AviatorRuntimeJavaType.valueOf(Arrays.asList(a));
} else if (clazz.isArray()) {
int length = ArrayUtils.getLength(first);
Object[] dup = (Object[]) Array.newInstance(first.getClass().getComponentType(), length);
for (int i = 0; i < length; i++) {
dup[i] = ArrayUtils.get(first, i);
}
// System.arraycopy(array, 0, dup, 0, dup.length);
Arrays.sort(dup, comparator);
return AviatorRuntimeJavaType.valueOf(dup);
} else {
throw new IllegalArgumentException(arg1.desc(env) + " is not an array or list.");
}
}
Aggregations