aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/internal/util/MammothMaps.java
blob: 873f90c3cbed6743e1115371d0545a896c32d785 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.zwobble.mammoth.internal.util;

import com.google.common.collect.ImmutableMap;

import java.util.*;
import java.util.function.Function;

public class MammothMaps {
    public static <K, V> Map<K, V> map() {
        return ImmutableMap.of();
    }

    public static <K, V> Map<K, V> map(K key1, V value1) {
        return ImmutableMap.of(key1, value1);
    }

    public static <K, V> Map<K, V> map(K key1, V value1, K key2, V value2) {
        return ImmutableMap.of(key1, value1, key2, value2);
    }

    public static <K, V> Map<K, V> map(K key1, V value1, K key2, V value2, K key3, V value3) {
        return ImmutableMap.of(key1, value1, key2, value2, key3, value3);
    }

    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) {
        ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
        for (T element : iterable) {
            builder.put(function.apply(element));
        }
        return builder.build();
    }

    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 <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;
    }
}