package org.zwobble.couscous.util; import com.google.common.collect.ImmutableMap; import java.util.AbstractMap; import java.util.Map; import java.util.Optional; import java.util.function.Function; public class ExtraMaps { public static Map map() { return ImmutableMap.of(); } public static Map map(K key1, V value1) { return ImmutableMap.of(key1, value1); } public static Map map(K key1, V value1, K key2, V value2) { return ImmutableMap.of(key1, value1, key2, value2); } public static Map map(K key1, V value1, K key2, V value2, K key3, V value3) { return ImmutableMap.of(key1, value1, key2, value2, key3, value3); } public static Optional lookup(Map map, K key) { return Optional.ofNullable(map.get(key)); } public static Map toMap(Iterable keys, Iterable values) { return toMap(ExtraIterables.lazyMap(keys, values, ExtraMaps::entry)); } public static Map toMap(Iterable> entries) { ImmutableMap.Builder builder = ImmutableMap.builder(); entries.forEach(builder::put); return builder.build(); } public static Map toMap(Iterable iterable, Function> function) { ImmutableMap.Builder builder = ImmutableMap.builder(); for (T value : iterable) { builder.put(function.apply(value)); } return builder.build(); } public static Map toMapWithKeys(Iterable iterable, Function function) { return toMap(iterable, value -> entry(function.apply(value), value)); } public static Map.Entry entry(K key, V value) { return new AbstractMap.SimpleImmutableEntry<>(key, value); } }