aboutsummaryrefslogtreecommitdiff
path: root/Mammoth/Couscous/mammoth/internal/util/Maps.cs
blob: 7848f13cd0d80b7957b153b779da5ccea891cce9 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Linq;
using Mammoth.Couscous.java.util;
using Mammoth.Couscous.java.lang;
using Mammoth.Couscous.java.util.function;
using Mammoth.Couscous;
using System.Collections.Generic;

namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util
{
    internal static class Maps
    {
        internal static Map<K, V> map<K, V>() {
            return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary<K, V>());
        }

        internal static Map<K, V> map<K, V>(K key1, V value1) {
            return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary<K, V> {
                {key1, value1}
            });
        }

        internal static Map<K, V> map<K, V>(K key1, V value1, K key2, V value2) {
            return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary<K, V> {
                {key1, value1},
                {key2, value2}
            });
        }

        internal static Map<K, V> map<K, V>(K key1, V value1, K key2, V value2, K key3, V value3) {
            return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary<K, V> {
                {key1, value1},
                {key2, value2},
                {key3, value3}
            });
        }

        internal static Map<K, V> toMap<T, K, V>(Iterable<T> iterable, Function<T, Map__Entry<K, V>> function) {
            var dictionary = FromJava.IterableToEnumerable(iterable)
                .Select(function.apply)
                .ToDictionary(entry => entry.getKey(), entry => entry.getValue());
            return ToJava.DictionaryToMap(dictionary);

        }

        /// <summary>
        /// Convert an iterable to map using the given mapping function. If the
        /// mapping function generates entries with the same key, only the first
        /// entry will be used, and subsequent entries will be ignored.
        /// </summary>
        internal static Map<K, V> toMapPreferFirst<T, K, V>(Iterable<T> iterable, Function<T, Map__Entry<K, V>> function) {
            var dictionary = new Dictionary<K, V>();

            foreach (var element in FromJava.IterableToEnumerable(iterable)) {
                var entry = function.apply(element);
                if (!dictionary.ContainsKey(entry.getKey())) {
                    dictionary[entry.getKey()] = entry.getValue();
                }
            }

            return ToJava.DictionaryToMap(dictionary);
        }

        internal static Map<K, T> toMapWithKey<T, K>(Iterable<T> iterable, Function<T, K> function) {
            var dictionary = FromJava.IterableToEnumerable(iterable)
                .ToDictionary(function.apply);
            return ToJava.DictionaryToMap(dictionary);
        }

        internal static Map<K, java.util.List<T>> toMultiMapWithKey<T, K>(Iterable<T> iterable, Function<T, K> function) {
            var dictionary = FromJava.IterableToEnumerable(iterable)
                .GroupBy(value => function.apply(value))
                .ToDictionary(
                    grouping => grouping.Key,
                    grouping => ToJava.ListToList(grouping.ToList()));
            return ToJava.DictionaryToMap(dictionary);
        }

        internal static Map<K, java.util.List<V>> toMultiMap<T, K, V>(Iterable<T> iterable, Function<T, Map__Entry<K, V>> function) {
            var dictionary = FromJava.IterableToEnumerable(iterable)
                .Select(element => function.apply(element))
                .GroupBy(entry => entry.getKey())
                .ToDictionary(
                    grouping => grouping.Key,
                    grouping => ToJava.ListToList(grouping.Select(entry => entry.getValue()).ToList()));
            return ToJava.DictionaryToMap(dictionary);
        }

        internal static Optional<V> lookup<K, V>(Map<K, V> map, K key) {
            return map._lookup(key);
        }

        internal static Map<K2, V> eagerMapKeys<K1, K2, V>(Map<K1, V> map, Function<K1, K2> function) {
            var dictionary = FromJava.MapToDictionary(map)
                .ToDictionary(
                    entry => function.apply(entry.Key),
                    entry => entry.Value);
            return ToJava.DictionaryToMap(dictionary);
        }

        internal static Map<K, V2> eagerMapValues<K, V1, V2>(Map<K, V1> map, Function<V1, V2> function) {
            var dictionary = FromJava.MapToDictionary(map)
                .ToDictionary(
                    entry => entry.Key,
                    entry => function.apply(entry.Value));
            return ToJava.DictionaryToMap(dictionary);
        }

        internal static Map__Entry<K, V> entry<K, V>(K key, V value) {
            return new Entry<K, V>(key, value);
        }

        private class Entry<K, V> : Map__Entry<K, V> {
            private readonly K _key;
            private readonly V _value;

            internal Entry(K key, V value) {
                this._key = key;
                this._value = value;
            }

            public K getKey() {
                return _key;
            }

            public V getValue() {
                return _value;
            }

            public override bool Equals(object otherObj) {
                var other = otherObj as Entry<K, V>;
                if (other == null) {
                    return false;
                } else if (object.ReferenceEquals(this, other)) {
                    return true;
                } else {
                    return _key.Equals(other._key) && _value.Equals(other._value);
                }
            }

            public override int GetHashCode() {
                return _key.GetHashCode() * 17 + _value.GetHashCode();
            }
        }

        internal static Builder<K, V> builder<K, V>() {
            return new Builder<K, V>();
        }

        internal class Builder<K, V> {
            private readonly System.Collections.Generic.Dictionary<K, V> _values = new System.Collections.Generic.Dictionary<K, V>();

            internal Builder<K, V> put(K key, V value) {
                _values[key] = value;
                return this;
            }

            internal Map<K, V> build() {
                return ToJava.DictionaryToMap(_values);
            }
        }

        internal static Map<K, V> mutableMap<K, V>(K key1, V value1) {
            var map = new HashMap<K, V>();
            map.put(key1, value1);
            return map;
        }
    }
}