From 0f9ef005f063198ea2e16ca36beaead4bd5e3824 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 14 Apr 2016 21:12:07 +0100 Subject: Add .NET output --- .../Mammoth/Couscous/mammoth/internal/util/Maps.cs | 110 +++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 dotnet/Mammoth/Couscous/mammoth/internal/util/Maps.cs (limited to 'dotnet/Mammoth/Couscous/mammoth/internal/util/Maps.cs') diff --git a/dotnet/Mammoth/Couscous/mammoth/internal/util/Maps.cs b/dotnet/Mammoth/Couscous/mammoth/internal/util/Maps.cs new file mode 100644 index 0000000..bee2dac --- /dev/null +++ b/dotnet/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 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); + } + } + } +} -- cgit v1.2.3