diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-03-26 17:57:45 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-03-26 17:57:45 +0000 |
| commit | 581aec7d05f06456a384aa9adef7d781a666bf91 (patch) | |
| tree | 58bdd3c3de41a3b8c4886b23fc368be47d4486e2 /src/main/java/org/zwobble/mammoth/internal/util/Maps.java | |
| parent | 3f978b230ce8f5713be07b5dec2f0ccebacbda71 (diff) | |
Remove Mammoth prefix from util classes
Diffstat (limited to 'src/main/java/org/zwobble/mammoth/internal/util/Maps.java')
| -rw-r--r-- | src/main/java/org/zwobble/mammoth/internal/util/Maps.java | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/main/java/org/zwobble/mammoth/internal/util/Maps.java b/src/main/java/org/zwobble/mammoth/internal/util/Maps.java new file mode 100644 index 0000000..76ce5cf --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/internal/util/Maps.java @@ -0,0 +1,97 @@ +package org.zwobble.mammoth.internal.util; + +import java.util.*; +import java.util.function.Function; + +public class Maps { + public static <K, V> Map<K, V> map() { + return Collections.emptyMap(); + } + + public static <K, V> Map<K, V> map(K key1, V value1) { + return Collections.singletonMap(key1, value1); + } + + public static <K, V> Map<K, V> map(K key1, V value1, K key2, V value2) { + Map<K, V> map = new HashMap<>(); + map.put(key1, value1); + map.put(key2, value2); + return map; + } + + public static <K, V> Map<K, V> map(K key1, V value1, K key2, V value2, K key3, V value3) { + Map<K, V> map = new HashMap<>(); + map.put(key1, value1); + map.put(key2, value2); + map.put(key3, value3); + return map; + } + + public static <K, V> Map.Entry<K, V> entry(K key, V value) { + return new AbstractMap.SimpleImmutableEntry<K, V>(key, value); + } + + public static <K, V> Optional<V> lookup(Map<K, V> map, K key) { + return Optional.ofNullable(map.get(key)); + } + + public static <T, K> Map<K, T> toMapWithKey(Iterable<T> iterable, Function<T, K> function) { + return toMap(iterable, element -> entry(function.apply(element), element)); + } + + public static <T, K, V> Map<K, V> toMap(Iterable<T> iterable, Function<T, Map.Entry<K, V>> function) { + Map<K, V> map = new HashMap<>(); + for (T element : iterable) { + Map.Entry<K, V> entry = function.apply(element); + map.put(entry.getKey(), entry.getValue()); + } + return map; + } + + public static <T, K> Map<K, List<T>> toMultiMapWithKey(Iterable<T> iterable, Function<T, K> function) { + return toMultiMap(iterable, element -> entry(function.apply(element), element)); + } + + public static <T, K, V> Map<K, List<V>> toMultiMap(Iterable<T> iterable, Function<T, Map.Entry<K, V>> function) { + Map<K, List<V>> map = new HashMap<>(); + for (T element : iterable) { + Map.Entry<K, V> pair = function.apply(element); + map.computeIfAbsent(pair.getKey(), key -> new ArrayList<V>()) + .add(pair.getValue()); + } + return map; + } + + public static <K1, K2, V> Map<K2, V> eagerMapKeys(Map<K1, V> map, Function<K1, K2> function) { + Map<K2, V> result = new HashMap<>(); + for (Map.Entry<K1, V> element : map.entrySet()) { + result.put(function.apply(element.getKey()), element.getValue()); + } + return result; + } + + public static <K, V1, V2> Map<K, V2> eagerMapValues(Map<K, V1> map, Function<V1, V2> function) { + Map<K, V2> result = new HashMap<>(); + for (Map.Entry<K, V1> element : map.entrySet()) { + result.put(element.getKey(), function.apply(element.getValue())); + } + return result; + } + + public static <K, V> Builder<K, V> builder() { + return new Builder<>(); + } + + public static class Builder<K, V> { + private final Map<K, V> values = new HashMap<>(); + + public Builder<K, V> put(K key, V value) { + values.put(key, value); + return this; + } + + public Map<K, V> build() { + return values; + } + } +} |
