comparison base/src/java/util/Collections.d @ 27:1bf55a6eb092

Renamed java tree to base
author Frank Benoit <benoit@tionex.de>
date Sat, 21 Mar 2009 11:33:57 +0100
parents java/src/java/util/Collections.d@9b96950f2c3c
children fcf926c91ca4
comparison
equal deleted inserted replaced
26:f589fc20a5f9 27:1bf55a6eb092
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
15 class Collections {
16 private static void unsupported(){
17 throw new UnsupportedOperationException();
18 }
19
20 private static List EMPTY_LIST_;
21 public static List EMPTY_LIST(){
22 if( EMPTY_LIST_ is null ){
23 synchronized(Collections.classinfo ){
24 if( EMPTY_LIST_ is null ){
25 EMPTY_LIST_ = new ArrayList(0);
26 }
27 }
28 }
29 return EMPTY_LIST_;
30 }
31 private static Map EMPTY_MAP_;
32 public static Map EMPTY_MAP(){
33 if( EMPTY_MAP_ is null ){
34 synchronized(Collections.classinfo ){
35 if( EMPTY_MAP_ is null ){
36 EMPTY_MAP_ = new TreeMap();
37 }
38 }
39 }
40 return EMPTY_MAP_;
41 }
42 private static Set EMPTY_SET_;
43 public static Set EMPTY_SET(){
44 if( EMPTY_SET_ is null ){
45 synchronized(Collections.classinfo ){
46 if( EMPTY_SET_ is null ){
47 EMPTY_SET_ = new TreeSet();
48 }
49 }
50 }
51 return EMPTY_SET_;
52 }
53 static class UnmodifiableIterator : Iterator {
54 Iterator it;
55 this(Iterator it){
56 this.it = it;
57 }
58 public bool hasNext(){
59 return it.hasNext();
60 }
61 public Object next(){
62 return it.next();
63 }
64 public void remove(){
65 unsupported();
66 }
67 }
68 static class UnmodifiableListIterator : ListIterator {
69 ListIterator it;
70 this(ListIterator it){
71 this.it = it;
72 }
73 public void add(Object o){
74 unsupported();
75 }
76 public bool add(String o){
77 unsupported();
78 return false; // make compiler happy
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 return false; // make compiler happy
132 }
133 public bool contains(Object o){
134 return list.contains(o);
135 }
136 public bool contains(String o){
137 return list.contains(o);
138 }
139 public bool containsAll(Collection c){
140 return list.containsAll(c);
141 }
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 public hash_t toHash(){
149 return list.toHash();
150 }
151 public int indexOf(Object o){
152 return list.indexOf(o);
153 }
154 public bool isEmpty(){
155 return list.isEmpty();
156 }
157 public Iterator iterator(){
158 return new UnmodifiableIterator( list.iterator() );
159 }
160 public int lastIndexOf(Object o){
161 return list.lastIndexOf(o);
162 }
163 public ListIterator listIterator(){
164 return new UnmodifiableListIterator( list.listIterator() );
165 }
166 public ListIterator listIterator(int index){
167 return new UnmodifiableListIterator( list.listIterator(index) );
168 }
169 public Object remove(int index){
170 unsupported();
171 return null; // make compiler happy
172 }
173 public bool remove(Object o){
174 unsupported();
175 return false; // make compiler happy
176 }
177 public bool remove(String o){
178 unsupported();
179 return false; // make compiler happy
180 }
181 public bool removeAll(Collection c){
182 unsupported();
183 return false; // make compiler happy
184 }
185 public bool retainAll(Collection c){
186 unsupported();
187 return false; // make compiler happy
188 }
189 public Object set(int index, Object element){
190 unsupported();
191 return null; // make compiler happy
192 }
193 public int size(){
194 return list.size();
195 }
196 public List subList(int fromIndex, int toIndex){
197 return new UnmodifieableList( list.subList(fromIndex,toIndex));
198 }
199 public Object[] toArray(){
200 return list.toArray();
201 }
202 public Object[] toArray(Object[] a){
203 return list.toArray(a);
204 }
205 public int opApply (int delegate(ref Object value) dg){
206 implMissing(__FILE__, __LINE__ );
207 return 0;
208 }
209 public int opApply (int delegate(ref Object key, ref Object value) dg){
210 implMissing(__FILE__, __LINE__ );
211 return 0;
212 }
213 }
214 static int binarySearch(List list, Object key){
215 implMissing( __FILE__, __LINE__ );
216 return 0;
217 }
218 static int binarySearch(List list, Object key, Comparator c){
219 implMissing( __FILE__, __LINE__ );
220 return 0;
221 }
222 public static List unmodifiableList( List list ){
223 return new UnmodifieableList(list);
224 }
225 public static Map unmodifiableMap( Map list ){
226 implMissing( __FILE__, __LINE__ );
227 return null;
228 }
229 public static Set unmodifiableSet( Set list ){
230 implMissing( __FILE__, __LINE__ );
231 return null;
232 }
233 public static Set singleton( Object o ){
234 TreeSet res = new TreeSet();
235 res.add(o);
236 return res;
237 }
238 public static void sort(List list){
239 implMissing( __FILE__, __LINE__ );
240 }
241 public static void sort(List list, Comparator c){
242 implMissing( __FILE__, __LINE__ );
243 }
244
245 static Collection synchronizedCollection(Collection c){
246 implMissing( __FILE__, __LINE__ );
247 return null;
248 }
249 static class SynchronizedList : List {
250 private List list;
251 private this( List list ){ this.list = list; }
252 // Collection
253 public int opApply (int delegate(ref Object value) dg){ synchronized(this){ return this.list.opApply(dg); } }
254 // List
255 public void add(int index, Object element){ synchronized(this){ return this.list.add(index, element); } }
256 public bool add(Object o){ synchronized(this){ return this.list.add(o); } }
257 public bool add(String o){ synchronized(this){ return this.list.add(o); } }
258 public bool addAll(Collection c){ synchronized(this){ return this.list.addAll(c); } }
259 public bool addAll(int index, Collection c){ synchronized(this){ return this.list.addAll(index, c); } }
260 public void clear(){ synchronized(this){ return this.list.clear(); } }
261 public bool contains(Object o){ synchronized(this){ return this.list.contains(o); } }
262 public bool contains(String o){ synchronized(this){ return this.list.contains(o); } }
263 public bool containsAll(Collection c){ synchronized(this){ return this.list.containsAll(c); } }
264 public equals_t opEquals(Object o){ synchronized(this){ return cast(equals_t)this.list.opEquals(o); } }
265 public Object get(int index){ synchronized(this){ return this.list.get(index); } }
266 public hash_t toHash(){ synchronized(this){ return this.list.toHash(); } }
267 public int indexOf(Object o){ synchronized(this){ return this.list.indexOf(o); } }
268 public bool isEmpty(){ synchronized(this){ return this.list.isEmpty(); } }
269 public Iterator iterator(){ synchronized(this){ return this.list.iterator(); } }
270 public int lastIndexOf(Object o){ synchronized(this){ return this.list.lastIndexOf(o); } }
271 public ListIterator listIterator(){ synchronized(this){ return this.list.listIterator(); } }
272 public ListIterator listIterator(int index){ synchronized(this){ return this.list.listIterator(index); } }
273 public Object remove(int index){ synchronized(this){ return this.list.remove(index); } }
274 public bool remove(Object o){ synchronized(this){ return this.list.remove(o); } }
275 public bool remove(String o){ synchronized(this){ return this.list.remove(o); } }
276 public bool removeAll(Collection c){ synchronized(this){ return this.list.removeAll(c); } }
277 public bool retainAll(Collection c){ synchronized(this){ return this.list.retainAll(c); } }
278 public Object set(int index, Object element){ synchronized(this){ return this.list.set(index,element); } }
279 public int size(){ synchronized(this){ return this.list.size(); } }
280 public List subList(int fromIndex, int toIndex){ synchronized(this){ return this.list.subList(fromIndex,toIndex); } }
281 public Object[] toArray(){ synchronized(this){ return this.list.toArray(); } }
282 public Object[] toArray(Object[] a){ synchronized(this){ return this.list.toArray(a); } }
283 }
284 static List synchronizedList(List list){
285 return new SynchronizedList(list);
286 }
287
288 static class SynchronizedMap : Map {
289 private Map map;
290 //interface Entry {
291 // int opEquals(Object o);
292 // Object getKey();
293 // Object getValue();
294 // hash_t toHash();
295 // Object setValue(Object value);
296 //}
297 private this( Map map ){
298 this.map = map;
299 }
300 public void clear(){ synchronized(this){ this.map.clear(); } }
301 public bool containsKey(Object key){ synchronized(this){ return this.map.containsKey(key); } }
302 public bool containsKey(String key){ synchronized(this){ return this.map.containsKey(key); } }
303 public bool containsValue(Object value){ synchronized(this){ return this.map.containsValue(value); } }
304 public Set entrySet(){ synchronized(this){ return this.map.entrySet(); } }
305 public equals_t opEquals(Object o){ synchronized(this){ return this.map.opEquals(o); } }
306 public Object get(Object key){ synchronized(this){ return this.map.get(key); } }
307 public Object get(String key){ synchronized(this){ return this.map.get(key); } }
308 public hash_t toHash(){ synchronized(this){ return this.map.toHash(); } }
309 public bool isEmpty(){ synchronized(this){ return this.map.isEmpty(); } }
310 public Set keySet(){ synchronized(this){ return this.map.keySet(); } }
311 public Object put(Object key, Object value){ synchronized(this){ return this.map.put(key,value); } }
312 public Object put(String key, Object value){ synchronized(this){ return this.map.put(key,value); } }
313 public Object put(Object key, String value){ synchronized(this){ return this.map.put(key,value); } }
314 public Object put(String key, String value){ synchronized(this){ return this.map.put(key,value); } }
315 public void putAll(Map t){ synchronized(this){ return this.map.putAll(t); } }
316 public Object remove(Object key){ synchronized(this){ return this.map.remove(key); } }
317 public Object remove(String key){ synchronized(this){ return this.map.remove(key); } }
318 public int size(){ synchronized(this){ return this.map.size(); } }
319 public Collection values(){ synchronized(this){ return this.map.values(); } }
320
321 // only for D
322 public int opApply (int delegate(ref Object value) dg){ synchronized(this){ return this.map.opApply( dg ); } }
323 public int opApply (int delegate(ref Object key, ref Object value) dg){ synchronized(this){ return this.map.opApply( dg ); } }
324 }
325 static Map synchronizedMap(Map m){
326 return new SynchronizedMap(m);
327 }
328 static Set synchronizedSet(Set s){
329 implMissing( __FILE__, __LINE__ );
330 return null;
331 }
332 // static SortedMap synchronizedSortedMap(SortedMap m){
333 // implMissing( __FILE__, __LINE__ );
334 // return null;
335 // }
336 // static SortedSet synchronizedSortedSet(SortedSet s){
337 // implMissing( __FILE__, __LINE__ );
338 // return null;
339 // }
340 static void reverse(List list) {
341 Object[] data = list.toArray();
342 for( int idx = 0; idx < data.length; idx++ ){
343 list.set( data.length -1 -idx, data[idx] );
344 }
345 }
346 static class LocalEnumeration : Enumeration {
347 Object[] data;
348 this( Object[] data ){
349 this.data = data;
350 }
351 public bool hasMoreElements(){
352 return data.length > 0;
353 }
354 public Object nextElement(){
355 Object res = data[0];
356 data = data[ 1 .. $ ];
357 return res;
358 }
359 }
360 static Enumeration enumeration(Collection c){
361 return new LocalEnumeration( c.toArray() );
362 }
363 }
364