use of com.google.refine.grel.ast.VariableExpr in project OpenRefine by OpenRefine.
the class ForEachIndex method call.
@Override
public Object call(Properties bindings, Evaluable[] args) {
Object o = args[0].evaluate(bindings);
if (ExpressionUtils.isError(o)) {
return o;
} else if (!ExpressionUtils.isArrayOrCollection(o) && !(o instanceof JSONArray)) {
return new EvalError("First argument to forEach is not an array");
}
String indexName = ((VariableExpr) args[1]).getName();
String elementName = ((VariableExpr) args[2]).getName();
Object oldIndexValue = bindings.get(indexName);
Object oldElementValue = bindings.get(elementName);
try {
List<Object> results = null;
if (o.getClass().isArray()) {
Object[] values = (Object[]) o;
results = new ArrayList<Object>(values.length);
for (int i = 0; i < values.length; i++) {
Object v = values[i];
bindings.put(indexName, i);
bindings.put(elementName, v);
Object r = args[3].evaluate(bindings);
results.add(r);
}
} else if (o instanceof JSONArray) {
JSONArray a = (JSONArray) o;
int l = a.length();
results = new ArrayList<Object>(l);
for (int i = 0; i < l; i++) {
try {
Object v = a.get(i);
bindings.put(indexName, i);
bindings.put(elementName, v);
Object r = args[3].evaluate(bindings);
results.add(r);
} catch (JSONException e) {
results.add(new EvalError(e.getMessage()));
}
}
} else {
List<Object> list = ExpressionUtils.toObjectList(o);
results = new ArrayList<Object>(list.size());
for (int i = 0; i < list.size(); i++) {
Object v = list.get(i);
bindings.put(indexName, i);
bindings.put(elementName, v);
Object r = args[3].evaluate(bindings);
results.add(r);
}
}
return results.toArray();
} finally {
/*
* Restore the old values bound to the variables, if any.
*/
if (oldIndexValue != null) {
bindings.put(indexName, oldIndexValue);
} else {
bindings.remove(indexName);
}
if (oldElementValue != null) {
bindings.put(elementName, oldElementValue);
} else {
bindings.remove(elementName);
}
}
}
use of com.google.refine.grel.ast.VariableExpr in project OpenRefine by OpenRefine.
the class ForRange method call.
@Override
public Object call(Properties bindings, Evaluable[] args) {
Object fromO = args[0].evaluate(bindings);
Object toO = args[1].evaluate(bindings);
Object stepO = args[2].evaluate(bindings);
if (ExpressionUtils.isError(fromO)) {
return fromO;
} else if (ExpressionUtils.isError(toO)) {
return toO;
} else if (ExpressionUtils.isError(stepO)) {
return stepO;
} else if (!(fromO instanceof Number) || !(toO instanceof Number) || !(stepO instanceof Number)) {
return new EvalError("First, second, and third arguments of forRange must all be numbers");
}
String indexName = ((VariableExpr) args[3]).getName();
Object oldIndexValue = bindings.get(indexName);
try {
List<Object> results = new ArrayList<Object>();
if (isIntegral((Number) fromO) && isIntegral((Number) stepO)) {
long from = ((Number) fromO).longValue();
long step = ((Number) stepO).longValue();
double to = ((Number) toO).doubleValue();
while (from < to) {
bindings.put(indexName, from);
Object r = args[4].evaluate(bindings);
results.add(r);
from += step;
}
} else {
double from = ((Number) fromO).longValue();
double step = ((Number) stepO).longValue();
double to = ((Number) toO).doubleValue();
while (from < to) {
bindings.put(indexName, from);
Object r = args[4].evaluate(bindings);
results.add(r);
from += step;
}
}
return results.toArray();
} finally {
/*
* Restore the old values bound to the variables, if any.
*/
if (oldIndexValue != null) {
bindings.put(indexName, oldIndexValue);
} else {
bindings.remove(indexName);
}
}
}
Aggregations