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 --- dotnet/Mammoth/Couscous/Adapters.cs | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 dotnet/Mammoth/Couscous/Adapters.cs (limited to 'dotnet/Mammoth/Couscous/Adapters.cs') diff --git a/dotnet/Mammoth/Couscous/Adapters.cs b/dotnet/Mammoth/Couscous/Adapters.cs new file mode 100644 index 0000000..a613473 --- /dev/null +++ b/dotnet/Mammoth/Couscous/Adapters.cs @@ -0,0 +1,86 @@ +using System.Collections.Generic; +using System.Linq; +using Mammoth.Couscous.java.lang; +using Mammoth.Couscous.java.util; + +namespace Mammoth.Couscous { + internal static class FromJava { + internal static IEnumerable IterableToEnumerable(Iterable iterable) { + Iterator iterator = iterable.iterator(); + while (iterator.hasNext()) { + yield return iterator.next(); + } + } + + internal static IDictionary MapToDictionary(Map map) { + return map.AsDictionary(); + } + } + + internal static class ToJava { + internal static Iterable EnumerableToIterable(IEnumerable enumerator) { + return new IterableWrapper(enumerator); + } + + private class IterableWrapper : Iterable { + private readonly IEnumerable _enumerable; + + internal IterableWrapper(IEnumerable enumerable) { + _enumerable = enumerable; + } + + public Iterator iterator() { + return EnumeratorToIterator(_enumerable.GetEnumerator()); + } + } + + internal static Iterator EnumeratorToIterator(IEnumerator enumerator) { + return new EnumeratorToIteratorAdapter(enumerator); + } + + private class EnumeratorToIteratorAdapter : Iterator { + private readonly IEnumerator _enumerator; + private bool _ready; + private bool _hasNext; + + internal EnumeratorToIteratorAdapter(IEnumerator enumerator) { + _enumerator = enumerator; + _ready = false; + } + + public bool hasNext() { + MakeReady(); + return _hasNext; + } + + public T next() { + MakeReady(); + if (_hasNext) { + _ready = false; + return _enumerator.Current; + } else { + throw new NoSuchElementException(); + } + } + + private void MakeReady() { + if (!_ready) { + MoveNext(); + } + } + + private void MoveNext() { + _hasNext = _enumerator.MoveNext(); + _ready = true; + } + } + + internal static Map DictionaryToMap(IDictionary dictionary) { + return new HashMap(dictionary); + } + + internal static java.util.List ListToList(IList list) { + return new ArrayList(list); + } + } +} -- cgit v1.2.3