aboutsummaryrefslogtreecommitdiff
path: root/Mammoth/Couscous/Adapters.cs
blob: 20d5f4f1ff0678429e9a760de86a75c73010caf7 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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>(new Dictionary<TKey, TValue>(dictionary));
        }
        
        internal static Map<TKey, TValue> DictionaryToMap<TKey, TValue>(Dictionary<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;
            }
        }
        
        internal static java.util.Collection<T> CollectionToCollection<T>(ICollection<T> collection) {
            return new CollectionToCollectionAdapter<T>(collection);
        }
        
        private class CollectionToCollectionAdapter<T> : java.util.Collection<T> {
            private readonly ICollection<T> _collection;
            
            internal CollectionToCollectionAdapter(ICollection<T> 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<T> iterator() {
                return ToJava.EnumeratorToIterator(_collection.GetEnumerator());
            }
            
            public void add(T value) {
                throw new UnsupportedOperationException();
            }
        }
    }
}