aboutsummaryrefslogtreecommitdiff
path: root/dotnet/Mammoth/Couscous/Adapters.cs
blob: c45f35bc774d4b2f3da023cca2e6c8a356f543bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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<T> IterableToEnumerable<T>(Iterable<T> iterable) {
            Iterator<T> iterator = iterable.iterator();
            while (iterator.hasNext()) {
                yield return iterator.next();
            }
        }
        
        internal static IDictionary<TKey, TValue> MapToDictionary<TKey, TValue>(Map<TKey, TValue> map) {
            return map.AsDictionary();
        }
    }
    
    internal static class ToJava {
        internal static Iterable<T> EnumerableToIterable<T>(IEnumerable<T> enumerator) {
            return new IterableWrapper<T>(enumerator);
        }
        
        private class IterableWrapper<T> : Iterable<T> {
            private readonly IEnumerable<T> _enumerable;
            
            internal IterableWrapper(IEnumerable<T> enumerable) {
                _enumerable = enumerable;
            }
            
            public Iterator<T> iterator() {
                return EnumeratorToIterator(_enumerable.GetEnumerator());
            }
        }
        
        internal static Iterator<T> EnumeratorToIterator<T>(IEnumerator<T> enumerator) {
            return new EnumeratorToIteratorAdapter<T>(enumerator);
        }
        
        private class EnumeratorToIteratorAdapter<T> : Iterator<T> {
            private readonly IEnumerator<T> _enumerator;
            private bool _ready;
            private bool _hasNext;
            
            internal EnumeratorToIteratorAdapter(IEnumerator<T> 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<TKey, TValue> DictionaryToMap<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
            return new HashMap<TKey, TValue>(dictionary);
        }
        
        internal static java.util.List<T> ListToList<T>(IList<T> list) {
            return new ArrayList<T>(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;
            }
        }
    }
}