From ec5037c01868fecaec2eba89b975f3e53aded89a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 30 Apr 2016 17:20:53 +0100 Subject: Export from java-mammoth 5f538798b147f0e76 --- Mammoth/Couscous/Adapters.cs | 99 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Mammoth/Couscous/Adapters.cs (limited to 'Mammoth/Couscous/Adapters.cs') diff --git a/Mammoth/Couscous/Adapters.cs b/Mammoth/Couscous/Adapters.cs new file mode 100644 index 0000000..c45f35b --- /dev/null +++ b/Mammoth/Couscous/Adapters.cs @@ -0,0 +1,99 @@ +using System.Collections.Generic; +using System.Linq; +using Mammoth.Couscous.java.lang; +using Mammoth.Couscous.java.util; +using Mammoth.Couscous.java.io; + +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); + } + + internal static java.io.InputStream StreamToInputStream(System.IO.Stream stream) { + return new StreamToInputStreamAdapter(stream); + } + + private class StreamToInputStreamAdapter : InputStream { + public System.IO.Stream Stream { get; } + + internal StreamToInputStreamAdapter(System.IO.Stream stream) { + Stream = stream; + } + } + } +} -- cgit v1.2.3