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

io.github.pellse.util.lookup.LookupTable Maven / Gradle / Ivy

Go to download

Small library allowing to efficiently assemble entities from querying/merging external datasources or aggregating microservices

There is a newer version: 0.7.9
Show newest version
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 keyMapper, Function valueMapper) {
        return also(lookupTable(), lookupTable -> elements.forEach(e -> lookupTable.put(keyMapper.apply(e), valueMapper.apply(e))));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy