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(new Dictionary(dictionary)); } internal static Map DictionaryToMap(Dictionary 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; } } internal static java.util.Collection CollectionToCollection(ICollection collection) { return new CollectionToCollectionAdapter(collection); } private class CollectionToCollectionAdapter : java.util.Collection { private readonly ICollection _collection; internal CollectionToCollectionAdapter(ICollection collection) { _collection = collection; } public bool isEmpty() { return _collection.Count == 0; } public int size() { return _collection.Count; } public bool contains(object value) { return value is T && _collection.Contains((T) value); } public Iterator iterator() { return ToJava.EnumeratorToIterator(_collection.GetEnumerator()); } public void add(T value) { throw new UnsupportedOperationException(); } } } }