package eu.dnetlib.miscutils.collections; import java.util.Map; import eu.dnetlib.miscutils.functional.UnaryFunction; /** * A special case of a PositionalMapGenerator, bound for String values. It provides automatic split which returns an * iterable of maps. * * @author marko * * @param * key */ public class PositionalStringMapGenerator extends PositionalMapGenerator { /** * Creates a new positional string map generator * * @param keys */ public PositionalStringMapGenerator(final K... keys) { super(keys); } /** * Pass a list of strings and return a list of maps where each key has a value according to the position in the * splitted array relative to the position of the key in the constructor. * * @param elements * list of strings which will be splitted * @param separator * separator * @return iterable of maps contained splitted values */ public Iterable> split(Iterable elements, final String separator) { return new MappedCollection, String>(elements, new UnaryFunction, String>() { @Override public Map evaluate(String value) { return asMap(value.split(separator)); } }); } /** * Pass a list of strings and return a list of objects constructed by passing the splitted string values to the * constructor of the new object. * *

* This method doesn't need to setup a list of key names in the constructor *

* * @param * some class * @param clazz * some class * @param elements * list of strings to split * @param separator * separator * @return iterable of instances of some class constructed with splitted values */ public Iterable split(final Class clazz, Iterable elements, final String separator) { return new MappedCollection(elements, new UnaryFunction() { @Override public X evaluate(String value) { return construct(clazz, String.class, value.split(separator)); } }); } }