2010-08-04 01:01:18 +02:00
|
|
|
package com.google.gridworks.expr.functions;
|
2010-05-05 01:24:48 +02:00
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONWriter;
|
|
|
|
|
2010-08-04 01:01:18 +02:00
|
|
|
import com.google.gridworks.expr.EvalError;
|
|
|
|
import com.google.gridworks.gel.ControlFunctionRegistry;
|
|
|
|
import com.google.gridworks.gel.Function;
|
2010-05-05 01:24:48 +02:00
|
|
|
|
|
|
|
public class Length implements Function {
|
|
|
|
|
|
|
|
public Object call(Properties bindings, Object[] args) {
|
|
|
|
if (args.length == 1) {
|
|
|
|
Object v = args[0];
|
|
|
|
|
|
|
|
if (v != null) {
|
|
|
|
if (v.getClass().isArray()) {
|
|
|
|
Object[] a = (Object[]) v;
|
|
|
|
return a.length;
|
|
|
|
} else if (v instanceof Collection<?>) {
|
|
|
|
return ((Collection<?>) v).size();
|
|
|
|
} else {
|
|
|
|
String s = (v instanceof String ? (String) v : v.toString());
|
|
|
|
return s.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array or a string");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
|
|
|
|
|
|
|
writer.object();
|
|
|
|
writer.key("description"); writer.value("Returns the length of o");
|
|
|
|
writer.key("params"); writer.value("array or string o");
|
|
|
|
writer.key("returns"); writer.value("number");
|
|
|
|
writer.endObject();
|
|
|
|
}
|
|
|
|
}
|