1 module java.util.Collections;
2 
3 import java.lang.all;
4 import java.util.Collection;
5 import java.util.Map;
6 import java.util.Set;
7 import java.util.TreeMap;
8 import java.util.TreeSet;
9 import java.util.List;
10 import java.util.Iterator;
11 import java.util.ListIterator;
12 import java.util.Enumeration;
13 import java.util.ArrayList;
14 import java.util.Comparator;
15 
16 class Collections {
17     private static void unsupported(){
18         throw new UnsupportedOperationException();
19     }
20 
21     private static List EMPTY_LIST_;
22     public static List EMPTY_LIST(){
23         if( EMPTY_LIST_ is null ){
24             synchronized(Collections.classinfo ){
25                 if( EMPTY_LIST_ is null ){
26                     EMPTY_LIST_ = new ArrayList(0);
27                 }
28             }
29         }
30         return EMPTY_LIST_;
31     }
32     private static Map EMPTY_MAP_;
33     public static Map EMPTY_MAP(){
34         if( EMPTY_MAP_ is null ){
35             synchronized(Collections.classinfo ){
36                 if( EMPTY_MAP_ is null ){
37                     EMPTY_MAP_ = new TreeMap();
38                 }
39             }
40         }
41         return EMPTY_MAP_;
42     }
43     private static Set EMPTY_SET_;
44     public static Set EMPTY_SET(){
45         if( EMPTY_SET_ is null ){
46             synchronized(Collections.classinfo ){
47                 if( EMPTY_SET_ is null ){
48                     EMPTY_SET_ = new TreeSet();
49                 }
50             }
51         }
52         return EMPTY_SET_;
53     }
54     static class UnmodifiableIterator : Iterator {
55         Iterator it;
56         this(Iterator it){
57             this.it = it;
58         }
59         public bool hasNext(){
60             return it.hasNext();
61         }
62         public Object next(){
63             return it.next();
64         }
65         public void  remove(){
66             unsupported();
67         }
68     }
69     static class UnmodifiableListIterator : ListIterator {
70         ListIterator it;
71         this(ListIterator it){
72             this.it = it;
73         }
74         public void   add(Object o){
75             unsupported();
76         }
77         public void   add(String o){
78             unsupported();
79         }
80         public bool   hasNext(){
81             return it.hasNext();
82         }
83         public bool   hasPrevious(){
84             return it.hasPrevious();
85         }
86         public Object next(){
87             return it.next();
88         }
89         public int    nextIndex(){
90             return it.nextIndex();
91         }
92         public Object previous(){
93             return it.previous();
94         }
95         public int    previousIndex(){
96             return it.previousIndex();
97         }
98         public void   remove(){
99             unsupported();
100         }
101         public void   set(Object o){
102             unsupported();
103         }
104     }
105     static class UnmodifieableList : List {
106         List list;
107         this(List list){
108             this.list = list;
109         }
110         public void     add(int index, Object element){
111             unsupported();
112         }
113         public bool     add(Object o){
114             unsupported();
115             return false; // make compiler happy
116         }
117         public bool     add(String o){
118             unsupported();
119             return false; // make compiler happy
120         }
121         public bool     addAll(Collection c){
122             unsupported();
123             return false; // make compiler happy
124         }
125         public bool     addAll(int index, Collection c){
126             unsupported();
127             return false; // make compiler happy
128         }
129         public void     clear(){
130             unsupported();
131         }
132         public bool     contains(Object o){
133             return list.contains(o);
134         }
135         public bool     contains(String o){
136             return list.contains(o);
137         }
138         public bool     containsAll(Collection c){
139             return list.containsAll(c);
140         }
141         override
142         public equals_t      opEquals(Object o){
143             return cast(equals_t)list.opEquals(o);
144         }
145         public Object   get(int index){
146             return list.get(index);
147         }
148         override
149         public hash_t   toHash(){
150             return list.toHash();
151         }
152         public int      indexOf(Object o){
153             return list.indexOf(o);
154         }
155         public bool     isEmpty(){
156             return list.isEmpty();
157         }
158         public Iterator iterator(){
159             return new UnmodifiableIterator( list.iterator() );
160         }
161         public int      lastIndexOf(Object o){
162             return list.lastIndexOf(o);
163         }
164         public ListIterator   listIterator(){
165             return new UnmodifiableListIterator( list.listIterator() );
166         }
167         public ListIterator   listIterator(int index){
168             return new UnmodifiableListIterator( list.listIterator(index) );
169         }
170         public Object   remove(int index){
171             unsupported();
172             return null; // make compiler happy
173         }
174         public bool     remove(Object o){
175             unsupported();
176             return false; // make compiler happy
177         }
178         public bool     remove(String o){
179             unsupported();
180             return false; // make compiler happy
181         }
182         public bool     removeAll(Collection c){
183             unsupported();
184             return false; // make compiler happy
185         }
186         public bool     retainAll(Collection c){
187             unsupported();
188             return false; // make compiler happy
189         }
190         public Object   set(int index, Object element){
191             unsupported();
192             return null; // make compiler happy
193         }
194         public int      size(){
195             return list.size();
196         }
197         public List     subList(int fromIndex, int toIndex){
198             return new UnmodifieableList( list.subList(fromIndex,toIndex));
199         }
200         public Object[] toArray(){
201             return list.toArray();
202         }
203         public Object[] toArray(Object[] a){
204             return list.toArray(a);
205         }
206         public String[] toArray(String[] a){
207             return list.toArray(a);
208         }
209         public int opApply (int delegate(ref Object value) dg){
210             implMissing(__FILE__, __LINE__ );
211             return 0;
212         }
213         public int opApply (int delegate(ref Object key, ref Object value) dg){
214             implMissing(__FILE__, __LINE__ );
215             return 0;
216         }
217         override
218         public String toString(){
219             return list.toString();
220         }
221     }
222     static int binarySearch(List list, Object key){
223         implMissing( __FILE__, __LINE__ );
224         return 0;
225     }
226     static int binarySearch(List list, Object key, Comparator c){
227         implMissing( __FILE__, __LINE__ );
228         return 0;
229     }
230     public static List unmodifiableList( List list ){
231         return new UnmodifieableList(list);
232     }
233     public static Map unmodifiableMap( Map list ){
234         implMissing( __FILE__, __LINE__ );
235         return null;
236     }
237     public static Set unmodifiableSet( Set list ){
238         implMissing( __FILE__, __LINE__ );
239         return null;
240     }
241     public static List singletonList( Object o ){
242         implMissing( __FILE__, __LINE__ );
243         return null;
244     }
245     public static Set singleton( Object o ){
246         TreeSet res = new TreeSet();
247         res.add(o);
248         return res;
249     }
250     public static void     sort(List list){
251         implMissing( __FILE__, __LINE__ );
252     }
253     public static void     sort(List list, Comparator c){
254         implMissing( __FILE__, __LINE__ );
255     }
256 
257     static Collection   synchronizedCollection(Collection c){
258         implMissing( __FILE__, __LINE__ );
259         return null;
260     }
261     static class SynchronizedList : List {
262         private List list;
263         private this( List list ){
264             this.list = list;
265         }
266         // Collection
267         public int opApply (int delegate(ref Object value) dg){ synchronized(this){ return this.list.opApply(dg); } }
268         // List
269         public void     add(int index, Object element){ synchronized(this){ return this.list.add(index, element); } }
270         public bool     add(Object o){ synchronized(this){ return this.list.add(o); } }
271         public bool     add(String o){ synchronized(this){ return this.list.add(o); } }
272         public bool     addAll(Collection c){ synchronized(this){ return this.list.addAll(c); } }
273         public bool     addAll(int index, Collection c){ synchronized(this){ return this.list.addAll(index, c); } }
274         public void     clear(){ synchronized(this){ return this.list.clear(); } }
275         public bool     contains(Object o){ synchronized(this){ return this.list.contains(o); } }
276         public bool     contains(String o){ synchronized(this){ return this.list.contains(o); } }
277         public bool     containsAll(Collection c){ synchronized(this){ return this.list.containsAll(c); } }
278         override
279         public equals_t      opEquals(Object o){ synchronized(this){ return cast(equals_t)this.list.opEquals(o); } }
280         public Object   get(int index){ synchronized(this){ return this.list.get(index); } }
281         override
282         public hash_t   toHash(){ return this.list.toHash(); }
283         public int      indexOf(Object o){ synchronized(this){ return this.list.indexOf(o); } }
284         public bool     isEmpty(){ synchronized(this){ return this.list.isEmpty(); } }
285         public Iterator iterator(){ synchronized(this){ return this.list.iterator(); } }
286         public int      lastIndexOf(Object o){ synchronized(this){ return this.list.lastIndexOf(o); } }
287         public ListIterator   listIterator(){ synchronized(this){ return this.list.listIterator(); } }
288         public ListIterator   listIterator(int index){ synchronized(this){ return this.list.listIterator(index); } }
289         public Object   remove(int index){ synchronized(this){ return this.list.remove(index); } }
290         public bool     remove(Object o){ synchronized(this){ return this.list.remove(o); } }
291         public bool     remove(String o){ synchronized(this){ return this.list.remove(o); } }
292         public bool     removeAll(Collection c){ synchronized(this){ return this.list.removeAll(c); } }
293         public bool     retainAll(Collection c){ synchronized(this){ return this.list.retainAll(c); } }
294         public Object   set(int index, Object element){ synchronized(this){ return this.list.set(index,element); } }
295         public int      size(){ synchronized(this){ return this.list.size(); } }
296         public List     subList(int fromIndex, int toIndex){ synchronized(this){ return this.list.subList(fromIndex,toIndex); } }
297         public Object[] toArray(){ synchronized(this){ return this.list.toArray(); } }
298         public Object[] toArray(Object[] a){ synchronized(this){ return this.list.toArray(a); } }
299         public String[] toArray(String[] a){ synchronized(this){ return this.list.toArray(a); } }
300         override
301         public String toString(){ synchronized(this){ return this.list.toString(); } }
302     }
303     static List     synchronizedList(List list){
304         return new SynchronizedList(list);
305     }
306 
307     static class SynchronizedMap : Map {
308         private Map map;
309         //interface Entry {
310         //    int   opEquals(Object o);
311         //    Object     getKey();
312         //    Object     getValue();
313         //    hash_t     toHash();
314         //    Object     setValue(Object value);
315         //}
316         private this( Map map ){
317             this.map = map;
318         }
319         public void clear(){ synchronized(this){ this.map.clear(); } }
320         public bool containsKey(Object key){ synchronized(this){ return this.map.containsKey(key); } }
321         public bool containsKey(String key){ synchronized(this){ return this.map.containsKey(key); } }
322         public bool containsValue(Object value){ synchronized(this){ return this.map.containsValue(value); } }
323         public Set  entrySet(){ synchronized(this){ return this.map.entrySet(); } }
324         override
325         public equals_t opEquals(Object o){ synchronized(this){ return this.map.opEquals(o); } }
326         public Object get(Object key){ synchronized(this){ return this.map.get(key); } }
327         public Object get(String key){ synchronized(this){ return this.map.get(key); } }
328         override
329         public hash_t toHash(){ return this.map.toHash(); }
330         public bool isEmpty(){ synchronized(this){ return this.map.isEmpty(); } }
331         public Set    keySet(){ synchronized(this){ return this.map.keySet(); } }
332         public Object put(Object key, Object value){ synchronized(this){ return this.map.put(key,value); } }
333         public Object put(String key, Object value){ synchronized(this){ return this.map.put(key,value); } }
334         public Object put(Object key, String value){ synchronized(this){ return this.map.put(key,value); } }
335         public Object put(String key, String value){ synchronized(this){ return this.map.put(key,value); } }
336         public void   putAll(Map t){ synchronized(this){ return this.map.putAll(t); } }
337         public Object remove(Object key){ synchronized(this){ return this.map.remove(key); } }
338         public Object remove(String key){ synchronized(this){ return this.map.remove(key); } }
339         public int    size(){ synchronized(this){ return this.map.size(); } }
340         public Collection values(){ synchronized(this){ return this.map.values(); } }
341 
342         // only for D
343         public int opApply (int delegate(ref Object value) dg){ synchronized(this){ return this.map.opApply( dg ); } }
344         public int opApply (int delegate(ref Object key, ref Object value) dg){ synchronized(this){ return this.map.opApply( dg ); } }
345     }
346     static Map  synchronizedMap(Map m){
347         return new SynchronizedMap(m);
348     }
349     static Set  synchronizedSet(Set s){
350         implMissing( __FILE__, __LINE__ );
351         return null;
352     }
353 //     static SortedMap    synchronizedSortedMap(SortedMap m){
354 //         implMissing( __FILE__, __LINE__ );
355 //         return null;
356 //     }
357 //     static SortedSet    synchronizedSortedSet(SortedSet s){
358 //         implMissing( __FILE__, __LINE__ );
359 //         return null;
360 //     }
361     static void     reverse(List list) {
362         Object[] data = list.toArray();
363         for( int idx = 0; idx < data.length; idx++ ){
364             list.set( cast(int)/*64bit*/data.length -1 -idx, data[idx] );
365         }
366     }
367     static class LocalEnumeration : Enumeration {
368         Object[] data;
369         this( Object[] data ){
370             this.data = data;
371         }
372         public bool hasMoreElements(){
373             return data.length > 0;
374         }
375         public Object nextElement(){
376             Object res = data[0];
377             data = data[ 1 .. $ ];
378             return res;
379         }
380     }
381     static Enumeration     enumeration(Collection c){
382         return new LocalEnumeration( c.toArray() );
383     }
384 }
385