diff options
| author | Michael Williamson <mike@zwobble.org> | 2017-03-30 23:09:24 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2017-04-01 16:15:41 +0100 |
| commit | 5b174110627839214c1413255e1c6715ff7d0aab (patch) | |
| tree | 3f2a97c071f82c0e44d9846a931f813474250764 /Mammoth/Couscous/java | |
| parent | 99ed1b53f084322c06975a2183d812db5f257d34 (diff) | |
Update java-mammoth to 1.3.1
Diffstat (limited to 'Mammoth/Couscous/java')
| -rw-r--r-- | Mammoth/Couscous/java/lang/StringBuilder.cs | 9 | ||||
| -rw-r--r-- | Mammoth/Couscous/java/util/AbstractMap.cs | 37 | ||||
| -rw-r--r-- | Mammoth/Couscous/java/util/ArrayList.cs | 7 | ||||
| -rw-r--r-- | Mammoth/Couscous/java/util/HashMap.cs | 27 | ||||
| -rw-r--r-- | Mammoth/Couscous/java/util/List.cs | 1 | ||||
| -rw-r--r-- | Mammoth/Couscous/java/util/Map.cs | 7 | ||||
| -rw-r--r-- | Mammoth/Couscous/java/util/Objects.cs | 10 | ||||
| -rw-r--r-- | Mammoth/Couscous/java/util/Optional.cs | 56 | ||||
| -rw-r--r-- | Mammoth/Couscous/java/util/OptionalInt.cs | 38 | ||||
| -rw-r--r-- | Mammoth/Couscous/java/util/Queue.cs | 8 | ||||
| -rw-r--r-- | Mammoth/Couscous/java/util/regex/Matcher.cs | 69 | ||||
| -rw-r--r-- | Mammoth/Couscous/java/util/regex/Pattern.cs | 25 |
12 files changed, 279 insertions, 15 deletions
diff --git a/Mammoth/Couscous/java/lang/StringBuilder.cs b/Mammoth/Couscous/java/lang/StringBuilder.cs index c894936..b7e9f99 100644 --- a/Mammoth/Couscous/java/lang/StringBuilder.cs +++ b/Mammoth/Couscous/java/lang/StringBuilder.cs @@ -7,8 +7,17 @@ namespace Mammoth.Couscous.java.lang { return this; } + internal StringBuilder append(char value) { + _builder.Append(value); + return this; + } + internal string toString() { return _builder.ToString(); } + + internal void setLength(int length) { + _builder.Length = length; + } } } diff --git a/Mammoth/Couscous/java/util/AbstractMap.cs b/Mammoth/Couscous/java/util/AbstractMap.cs new file mode 100644 index 0000000..f619267 --- /dev/null +++ b/Mammoth/Couscous/java/util/AbstractMap.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; +using System.Linq; +using Mammoth.Couscous.java.lang; + +namespace Mammoth.Couscous.java.util { + internal abstract class AbstractMap<TKey, TValue> : Map<TKey, TValue> { + public override bool Equals(object other) { + var otherMap = other as Map; + if (otherMap == null) { + return false; + } else { + return Equals(otherMap); + } + } + + public bool Equals(Map other) { + var first = AsUntypedDictionary(); + var second = other.AsUntypedDictionary(); + return + first.Count == second.Count && + first.Keys.Cast<object>().All(key => second.Contains(key) && first[key].Equals(second[key])); + } + + public override int GetHashCode() { + throw new System.NotSupportedException(); + } + + public abstract void put(TKey key, TValue value); + public abstract bool containsKey(TKey key); + public abstract Set<Map__Entry<TKey, TValue>> entrySet(); + public abstract Collection<TValue> values(); + + public abstract Optional<TValue> _lookup(TKey key); + public abstract System.Collections.Generic.IDictionary<TKey, TValue> AsDictionary(); + public abstract System.Collections.IDictionary AsUntypedDictionary(); + } +} diff --git a/Mammoth/Couscous/java/util/ArrayList.cs b/Mammoth/Couscous/java/util/ArrayList.cs index 4414123..81d180c 100644 --- a/Mammoth/Couscous/java/util/ArrayList.cs +++ b/Mammoth/Couscous/java/util/ArrayList.cs @@ -12,6 +12,9 @@ namespace Mammoth.Couscous.java.util { _list = list; } + internal ArrayList(List<T> list) : this(new System.Collections.Generic.List<T>(FromJava.IterableToEnumerable(list))) { + } + public Iterator<T> iterator() { return ToJava.EnumeratorToIterator(_list.GetEnumerator()); } @@ -45,5 +48,9 @@ namespace Mammoth.Couscous.java.util { _list.RemoveAt(index); return value; } + + public void set(int index, T value) { + _list[index] = value; + } } } diff --git a/Mammoth/Couscous/java/util/HashMap.cs b/Mammoth/Couscous/java/util/HashMap.cs index 10070fc..13383a8 100644 --- a/Mammoth/Couscous/java/util/HashMap.cs +++ b/Mammoth/Couscous/java/util/HashMap.cs @@ -4,29 +4,36 @@ using Mammoth.Couscous.org.zwobble.mammoth.@internal.util; using Mammoth.Couscous.java.lang; namespace Mammoth.Couscous.java.util { - internal class HashMap<TKey, TValue> : Map<TKey, TValue> { - private readonly IDictionary<TKey, TValue> _dictionary; + internal class HashMap<TKey, TValue> : AbstractMap<TKey, TValue> { + private readonly Dictionary<TKey, TValue> _dictionary; internal HashMap() : this(new Dictionary<TKey, TValue>()) { } - internal HashMap(IDictionary<TKey, TValue> dictionary) { + internal HashMap(Dictionary<TKey, TValue> dictionary) { _dictionary = dictionary; } - public void put(TKey key, TValue value) { + internal HashMap(Map<TKey, TValue> map) : this(new Dictionary<TKey, TValue>(map.AsDictionary())) { + } + + public override void put(TKey key, TValue value) { _dictionary[key] = value; } - public bool containsKey(TKey key) { + public override bool containsKey(TKey key) { return _dictionary.ContainsKey(key); } - public Set<Map__Entry<TKey, TValue>> entrySet() { + public override Set<Map__Entry<TKey, TValue>> entrySet() { return new EntrySet(_dictionary); } - public Optional<TValue> _lookup(TKey key) { + public override Collection<TValue> values() { + return ToJava.CollectionToCollection(_dictionary.Values); + } + + public override Optional<TValue> _lookup(TKey key) { if (_dictionary.ContainsKey(key)) { return new Some<TValue>(_dictionary[key]); } else { @@ -34,7 +41,11 @@ namespace Mammoth.Couscous.java.util { } } - public IDictionary<TKey, TValue> AsDictionary() { + public override IDictionary<TKey, TValue> AsDictionary() { + return _dictionary; + } + + public override System.Collections.IDictionary AsUntypedDictionary() { return _dictionary; } diff --git a/Mammoth/Couscous/java/util/List.cs b/Mammoth/Couscous/java/util/List.cs index 45a9299..38641e3 100644 --- a/Mammoth/Couscous/java/util/List.cs +++ b/Mammoth/Couscous/java/util/List.cs @@ -4,5 +4,6 @@ namespace Mammoth.Couscous.java.util { interface List<T> : Collection<T> { T get(int index); T remove(int index); + void set(int index, T value); } } diff --git a/Mammoth/Couscous/java/util/Map.cs b/Mammoth/Couscous/java/util/Map.cs index 5405c77..0d3eaf7 100644 --- a/Mammoth/Couscous/java/util/Map.cs +++ b/Mammoth/Couscous/java/util/Map.cs @@ -1,8 +1,13 @@ namespace Mammoth.Couscous.java.util { - internal interface Map<K, V> { + internal interface Map { + System.Collections.IDictionary AsUntypedDictionary(); + } + + internal interface Map<K, V> : Map { void put(K key, V value); bool containsKey(K key); Set<Map__Entry<K, V>> entrySet(); + Collection<V> values(); Optional<V> _lookup(K key); System.Collections.Generic.IDictionary<K, V> AsDictionary(); diff --git a/Mammoth/Couscous/java/util/Objects.cs b/Mammoth/Couscous/java/util/Objects.cs new file mode 100644 index 0000000..31948de --- /dev/null +++ b/Mammoth/Couscous/java/util/Objects.cs @@ -0,0 +1,10 @@ +using Mammoth.Couscous.java.util.function; + +namespace Mammoth.Couscous.java.util +{ + internal static class Objects { + internal static bool isNull(object obj) { + return obj == null; + } + } +} diff --git a/Mammoth/Couscous/java/util/Optional.cs b/Mammoth/Couscous/java/util/Optional.cs index 0f02c89..1ae8e00 100644 --- a/Mammoth/Couscous/java/util/Optional.cs +++ b/Mammoth/Couscous/java/util/Optional.cs @@ -1,4 +1,3 @@ -using System; using Mammoth.Couscous.java.util.function; namespace Mammoth.Couscous.java.util @@ -8,16 +7,28 @@ namespace Mammoth.Couscous.java.util bool isPresent(); Optional<U> map<U>(Function<T, U> function); Optional<U> flatMap<U>(Function<T, Optional<U>> function); + Optional<T> filter(Predicate<T> predicate); T orElse(T value); T orElseGet(Supplier<T> supplier); - T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : Exception; + T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : System.Exception; T get(); void ifPresent(Consumer<T> consumer); } + + internal interface None { + } - internal struct None<T> : Optional<T> { + internal struct None<T> : None, Optional<T> { internal static readonly None<T> Instance = new None<T>(); + public override bool Equals(object other) { + return other is None; + } + + public override int GetHashCode() { + return 0; + } + public bool isPresent() { return false; } @@ -29,6 +40,10 @@ namespace Mammoth.Couscous.java.util public Optional<U> flatMap<U>(Function<T, Optional<U>> function) { return new None<U>(); } + + public Optional<T> filter(Predicate<T> predicate) { + return this; + } public T orElse(T value) { return value; @@ -38,7 +53,7 @@ namespace Mammoth.Couscous.java.util return supplier.get(); } - public T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : Exception { + public T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : System.Exception { throw exceptionSupplier.get(); } @@ -49,13 +64,30 @@ namespace Mammoth.Couscous.java.util public void ifPresent(Consumer<T> consumer) { } } + + internal interface Some { + object getObject(); + } - internal struct Some<T> : Optional<T> { + internal struct Some<T> : Some, Optional<T> { private readonly T _value; internal Some(T value) { _value = value; } + + public override bool Equals(object other) { + var otherSome = other as Some; + if (otherSome == null) { + return false; + } else { + return _value.Equals(otherSome.getObject()); + } + } + + public override int GetHashCode() { + return _value.GetHashCode(); + } public bool isPresent() { return true; @@ -68,6 +100,14 @@ namespace Mammoth.Couscous.java.util public Optional<U> flatMap<U>(Function<T, Optional<U>> function) { return function.apply(_value); } + + public Optional<T> filter(Predicate<T> predicate) { + if (predicate.test(_value)) { + return this; + } else { + return None<T>.Instance; + } + } public T orElse(T value) { return _value; @@ -77,13 +117,17 @@ namespace Mammoth.Couscous.java.util return _value; } - public T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : Exception { + public T orElseThrow<TException>(Supplier<TException> exceptionSupplier) where TException : System.Exception { return _value; } public T get() { return _value; } + + public object getObject() { + return _value; + } public void ifPresent(Consumer<T> consumer) { consumer.accept(_value); diff --git a/Mammoth/Couscous/java/util/OptionalInt.cs b/Mammoth/Couscous/java/util/OptionalInt.cs new file mode 100644 index 0000000..4dfca2f --- /dev/null +++ b/Mammoth/Couscous/java/util/OptionalInt.cs @@ -0,0 +1,38 @@ +using Mammoth.Couscous.java.util.function; + +namespace Mammoth.Couscous.java.util +{ + internal interface OptionalInt + { + bool isPresent(); + int getAsInt(); + } + + internal struct NoneInt : OptionalInt { + internal static readonly NoneInt Instance = new NoneInt(); + + public bool isPresent() { + return false; + } + + public int getAsInt() { + throw new NoSuchElementException(); + } + } + + internal struct SomeInt : OptionalInt { + private readonly int _value; + + internal SomeInt(int value) { + _value = value; + } + + public bool isPresent() { + return true; + } + + public int getAsInt() { + return _value; + } + } +} diff --git a/Mammoth/Couscous/java/util/Queue.cs b/Mammoth/Couscous/java/util/Queue.cs new file mode 100644 index 0000000..6cb462f --- /dev/null +++ b/Mammoth/Couscous/java/util/Queue.cs @@ -0,0 +1,8 @@ +using Mammoth.Couscous.java.lang; + +namespace Mammoth.Couscous.java.util { + internal interface Queue<T> : Iterable<T> { + void add(T value); + T remove(); + } +} diff --git a/Mammoth/Couscous/java/util/regex/Matcher.cs b/Mammoth/Couscous/java/util/regex/Matcher.cs new file mode 100644 index 0000000..db477d7 --- /dev/null +++ b/Mammoth/Couscous/java/util/regex/Matcher.cs @@ -0,0 +1,69 @@ +using System.Text.RegularExpressions; + +namespace Mammoth.Couscous.java.util.regex { + internal class Matcher { + private readonly Regex _regex; + private readonly string _value; + private int _regionStart = 0; + private Match _lastMatch = null; + + internal Matcher(Regex regex, string value) { + _regex = regex; + _value = value; + } + + internal bool lookingAt() { + var match = _regex.Match(_value, _regionStart); + if (match.Success && match.Index == _regionStart) { + _lastMatch = match; + return true; + } else { + return false; + } + } + + internal bool find() { + var match = _regex.Match(_value, _regionStart); + if (match.Success) { + _lastMatch = match; + _regionStart = end(); + return true; + } else { + return false; + } + } + + internal string group() { + return group(0); + } + + internal string group(int groupIndex) { + var group = _lastMatch.Groups[groupIndex]; + return group.Success ? group.Value : null; + } + + internal int groupCount() { + return _regex.GetGroupNumbers().Length - 1; + } + + internal int start() { + return _lastMatch.Index; + } + + internal int end() { + return _lastMatch.Index + _lastMatch.Length; + } + + internal int regionStart() { + return _regionStart; + } + + internal void region(int start, int end) { + if (end == _value.Length) { + _regionStart = start; + } else { + throw new System.NotSupportedException(); + } + } + } +} diff --git a/Mammoth/Couscous/java/util/regex/Pattern.cs b/Mammoth/Couscous/java/util/regex/Pattern.cs new file mode 100644 index 0000000..42277ae --- /dev/null +++ b/Mammoth/Couscous/java/util/regex/Pattern.cs @@ -0,0 +1,25 @@ +using System.Text.RegularExpressions; + +namespace Mammoth.Couscous.java.util.regex { + internal class Pattern { + internal static Pattern compile(string pattern) { + return new Pattern(pattern, new Regex(pattern)); + } + + private readonly string _pattern; + private readonly Regex _regex; + + internal Pattern(string pattern, Regex regex) { + _pattern = pattern; + _regex = regex; + } + + internal string pattern() { + return _pattern; + } + + internal Matcher matcher(string value) { + return new Matcher(_regex, value); + } + } +} |
