net.intelie.pipes.util.GetUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pipes-api Show documentation
Show all versions of pipes-api Show documentation
Intelie Pipes' API classes and interfaces
package net.intelie.pipes.util;
import net.intelie.pipes.Expression;
import net.intelie.pipes.Row;
import net.intelie.pipes.Scalar;
import net.intelie.pipes.types.*;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public abstract class GetUtils {
public static Type inferType(Type target, Expression... exprs) {
for (Expression expr : exprs) {
target = inferSingle(target, expr);
}
return target;
}
private static Type inferSingle(Type target, Expression expr) {
if (target == null)
return null;
if (Level.CONSTANT.accepts(expr) && Type.extract(target, RowType.class) != null) {
RowType extractedRow = Type.extract(target, RowType.class);
RowFields fields = extractedRow.fields();
if (fields != null) {
int index = fields.indexOf(Type.STRING.cast(((Scalar) expr).eval(null, null)));
if (index >= 0)
return fields.type(index);
}
}
MapType extractedMap = Type.extract(target, MapType.class);
if (extractedMap != null)
return extractedMap.valueType();
SeqType extractedSeq = Type.extract(target, SeqType.class);
if (extractedSeq != null)
return extractedSeq.type();
if (Type.extract(target, StringType.class) != null)
return Type.STRING;
return null;
}
public static Double len(Object target) {
try {
if (target == null) return null;
if (target instanceof String)
return (double) ((String) target).length();
if (target instanceof Row)
return (double) ((Row) target).size();
if (target instanceof Iterable)
return (double) Iterables.size((Iterable) target);
if (target instanceof Map)
return (double) ((Map) target).size();
if (target.getClass().isArray())
return (double) Array.getLength(target);
return null;
} catch (Exception e) {
return null;
}
}
public static Object get(Object target, Object... args) {
try {
for (Object arg : args)
target = getSingle(target, arg);
return target;
} catch (Exception e) {
return null;
}
}
public static Object getSingle(Object target, Object arg) {
if (target == null) return null;
if (target instanceof Map)
return getFromMap((Map) target, arg);
if (arg instanceof Number)
return getWithNumericIndex(target, (Number) arg);
if (arg instanceof String) {
try {
return getWithNumericIndex(target, Double.parseDouble((String) arg));
} catch (Exception ignored) {
}
}
return null;
}
private static Object getWithNumericIndex(Object target, Number arg) {
int index = arg.intValue();
if (index < 0)
return null;
if (target instanceof String) {
if (index >= ((String) target).length())
return null;
return Character.toString(((String) target).charAt(index));
}
if (target instanceof Row) {
if (index >= ((Row) target).size())
return null;
return ((Row) target).get(index);
}
if (target instanceof Collection) {
if (index >= ((Collection) target).size())
return null;
if (target instanceof List)
return ((List) target).get(index);
}
if (target instanceof Iterable)
return getFromIterable((Iterable) target, index);
if (target != null && (target instanceof Object[] || target.getClass().isArray())) {
if (index >= Array.getLength(target))
return null;
return Array.get(target, index);
}
return null;
}
private static Object getFromIterable(Iterable target, int index) {
Iterator iterator = target.iterator();
int skipped = 0;
while (iterator.hasNext()) {
Object value = iterator.next();
if (skipped++ == index)
return value;
}
return null;
}
private static Object getFromMap(Map target, Object arg) {
return target.get(arg);
}
}