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 map() { return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary()); } internal static Map map(K key1, V value1) { return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary { {key1, value1} }); } internal static Map map(K key1, V value1, K key2, V value2) { return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary { {key1, value1}, {key2, value2} }); } internal static Map toMap(Iterable iterable, Function> function) { var dictionary = FromJava.IterableToEnumerable(iterable) .Select(function.apply) .ToDictionary(entry => entry.getKey(), entry => entry.getValue()); return ToJava.DictionaryToMap(dictionary); } internal static Map toMapWithKey(Iterable iterable, Function function) { var dictionary = FromJava.IterableToEnumerable(iterable) .ToDictionary(function.apply); return ToJava.DictionaryToMap(dictionary); } internal static Map> toMultiMapWithKey(Iterable iterable, Function 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 lookup(Map map, K key) { return map._lookup(key); } internal static Map eagerMapKeys(Map map, Function function) { var dictionary = FromJava.MapToDictionary(map) .ToDictionary( entry => function.apply(entry.Key), entry => entry.Value); return ToJava.DictionaryToMap(dictionary); } internal static Map eagerMapValues(Map map, Function function) { var dictionary = FromJava.MapToDictionary(map) .ToDictionary( entry => entry.Key, entry => function.apply(entry.Value)); return ToJava.DictionaryToMap(dictionary); } internal static Map__Entry entry(K key, V value) { return new Entry(key, value); } private class Entry : Map__Entry { 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 builder() { return new Builder(); } internal class Builder { private readonly System.Collections.Generic.IDictionary _values = new System.Collections.Generic.Dictionary(); internal Builder put(K key, V value) { _values[key] = value; return this; } internal Map build() { return ToJava.DictionaryToMap(_values); } } } }