aboutsummaryrefslogtreecommitdiff
path: root/Mammoth/Couscous/mammoth/internal/util/Maps.cs
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-04-30 17:20:53 +0100
committerMichael Williamson <mike@zwobble.org>2016-04-30 17:20:53 +0100
commitec5037c01868fecaec2eba89b975f3e53aded89a (patch)
treee0d5a092419bace577a90eb29699f5be57f36078 /Mammoth/Couscous/mammoth/internal/util/Maps.cs
parent17d1a1f710edd8dfb92e75bcd273e04cee7c8146 (diff)
Export from java-mammoth 5f538798b147f0e76
Diffstat (limited to 'Mammoth/Couscous/mammoth/internal/util/Maps.cs')
-rw-r--r--Mammoth/Couscous/mammoth/internal/util/Maps.cs110
1 files changed, 110 insertions, 0 deletions
diff --git a/Mammoth/Couscous/mammoth/internal/util/Maps.cs b/Mammoth/Couscous/mammoth/internal/util/Maps.cs
new file mode 100644
index 0000000..bee2dac
--- /dev/null
+++ b/Mammoth/Couscous/mammoth/internal/util/Maps.cs
@@ -0,0 +1,110 @@
+using System.Linq;
+using Mammoth.Couscous.java.util;
+using Mammoth.Couscous.java.lang;
+using Mammoth.Couscous.java.util.function;
+using Mammoth.Couscous;
+
+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> 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);
+
+ }
+
+ 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 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;
+ }
+ }
+
+ internal static Builder<K, V> builder<K, V>() {
+ return new Builder<K, V>();
+ }
+
+ internal class Builder<K, V> {
+ private readonly System.Collections.Generic.IDictionary<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);
+ }
+ }
+ }
+}