net.intelie.pipes.DefaultMapAdapter 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;
import net.intelie.pipes.types.ClauseInfo;
import net.intelie.pipes.types.RowFields;
import net.intelie.pipes.types.Type;
import net.intelie.pipes.util.GetUtils;
import net.intelie.pipes.util.MapRow;
import net.intelie.pipes.util.MergedMap;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
public class DefaultMapAdapter implements MapAdapter {
private static final long serialVersionUID = 1L;
private final boolean multivalued;
public DefaultMapAdapter() {
this(false);
}
public DefaultMapAdapter(boolean multivalued) {
this.multivalued = multivalued;
}
public static Object getFirst(Iterable iterable) {
Iterator it = iterable.iterator();
if (it.hasNext())
return iterable.iterator().next();
else
return null;
}
@Override
public Object get(Object target, String key, boolean single) {
Object obj = GetUtils.getSingle(target, key);
if (single) {
if (multivalued && obj instanceof Iterable)
obj = getFirst((Iterable) obj);
} else {
if (multivalued && obj == null)
obj = Collections.emptyList();
}
return obj;
}
@Override
public Type type(String key, boolean single) throws PipeException {
return null;
}
@Override
public Object replaceValues(Object obj, RowFields fields, Row row) {
Map map;
if (obj instanceof Map)
map = (Map) obj;
else
map = Collections.singletonMap("value", obj);
return new MergedMap<>(Arrays.asList(map, new MapRow(fields, row)));
}
}