All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.intelie.pipes.DefaultMapAdapter Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
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)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy