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 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 map(K key1, V value1, K key2, V value2, K key3, V value3) { return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary { {key1, value1}, {key2, value2}, {key3, value3} }); } 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); } /// /// 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. /// internal static Map toMapPreferFirst(Iterable iterable, Function> function) { var dictionary = new Dictionary(); 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 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 Map> toMultiMap(Iterable iterable, Function> 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 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; } public override bool Equals(object otherObj) { var other = otherObj as Entry; 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 builder() { return new Builder(); } internal class Builder { private readonly System.Collections.Generic.Dictionary _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); } } internal static Map mutableMap(K key1, V value1) { var map = new HashMap(); map.put(key1, value1); return map; } } }