
io.github.pellse.util.lookup.LookupTable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of assembler Show documentation
Show all versions of assembler Show documentation
Small library allowing to efficiently assemble entities from querying/merging external datasources or aggregating microservices
package io.github.pellse.util.lookup;
import java.util.*;
import java.util.function.Function;
import static io.github.pellse.util.ObjectUtils.also;
import static java.util.Collections.emptyList;
public interface LookupTable {
void put(K key, V value);
List get(K key);
static LookupTable lookupTable() {
final Map> map = new HashMap<>();
return new LookupTable<>() {
@Override
public void put(K key, V value) {
map.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
}
@Override
public List get(K key) {
return map.getOrDefault(key, emptyList());
}
};
}
static LookupTable lookupTableFrom(Iterable elements, Function super T, ? extends K> keyMapper, Function super T, ? extends V> valueMapper) {
return also(lookupTable(), lookupTable -> elements.forEach(e -> lookupTable.put(keyMapper.apply(e), valueMapper.apply(e))));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy