Mercurial > projects > dwt2
comparison java/src/java/util/ArrayList.d @ 10:eb8ff453285d
Added java.util collection classes.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 13 Mar 2009 16:57:07 +0100 |
parents | |
children | 9b96950f2c3c |
comparison
equal
deleted
inserted
replaced
9:950d84783eac | 10:eb8ff453285d |
---|---|
1 module java.util.ArrayList; | |
2 | |
3 import java.lang.all; | |
4 import java.util.AbstractList; | |
5 import java.util.List; | |
6 import java.util.ListIterator; | |
7 import java.util.Collection; | |
8 import java.util.Iterator; | |
9 | |
10 static import tango.core.Array; | |
11 | |
12 class ArrayList : AbstractList, List { | |
13 private Object[] data; | |
14 | |
15 this(){ | |
16 } | |
17 this(int size){ | |
18 data.length = size; | |
19 data.length = 0; | |
20 } | |
21 this(Collection col){ | |
22 this(cast(int)(col.size*1.1)); | |
23 addAll(col); | |
24 } | |
25 void add(int index, Object element){ | |
26 data.length = data.length +1; | |
27 System.arraycopy( data, index, data, index+1, data.length - index -1 ); | |
28 data[index] = element; | |
29 } | |
30 bool add(Object o){ | |
31 data ~= o; | |
32 return true; | |
33 } | |
34 public bool add(String o){ | |
35 return add(stringcast(o)); | |
36 } | |
37 bool addAll(Collection c){ | |
38 if( c.size() is 0 ) return false; | |
39 uint idx = data.length; | |
40 data.length = data.length + c.size(); | |
41 foreach( o; c ){ | |
42 data[ idx++ ] = o; | |
43 } | |
44 return true; | |
45 } | |
46 bool addAll(int index, Collection c){ | |
47 implMissing( __FILE__, __LINE__ ); | |
48 return false; | |
49 } | |
50 void clear(){ | |
51 data.length = 0; | |
52 } | |
53 ArrayList clone(){ | |
54 implMissing( __FILE__, __LINE__ ); | |
55 return null; | |
56 } | |
57 bool contains(Object o){ | |
58 foreach( v; data ){ | |
59 if( o is v ){ | |
60 return true; | |
61 } | |
62 if(( o is null ) || ( v is null )){ | |
63 continue; | |
64 } | |
65 if( o == v ){ | |
66 return true; | |
67 } | |
68 } | |
69 return false; | |
70 } | |
71 bool contains(String o){ | |
72 return contains(stringcast(o)); | |
73 } | |
74 bool containsAll(Collection c){ | |
75 implMissing( __FILE__, __LINE__ ); | |
76 return false; | |
77 } | |
78 int opEquals(Object o){ | |
79 if( auto other = cast(ArrayList)o ){ | |
80 if( data.length !is other.data.length ){ | |
81 return false; | |
82 } | |
83 for( int i = 0; i < data.length; i++ ){ | |
84 if( data[i] is other.data[i] ){ | |
85 continue; | |
86 } | |
87 if(( data[i] is null ) || ( other.data[i] is null )){ | |
88 return false; | |
89 } | |
90 if( data[i] == other.data[i] ){ | |
91 continue; | |
92 } | |
93 return false; | |
94 } | |
95 return true; | |
96 } | |
97 return false; | |
98 } | |
99 Object get(int index){ | |
100 return data[index]; | |
101 } | |
102 public override hash_t toHash(){ | |
103 return super.toHash(); | |
104 } | |
105 int indexOf(Object o){ | |
106 foreach( i, v; data ){ | |
107 if( data[i] is o ){ | |
108 return i; | |
109 } | |
110 if(( data[i] is null ) || ( o is null )){ | |
111 continue; | |
112 } | |
113 if( data[i] == o ){ | |
114 return i; | |
115 } | |
116 } | |
117 return -1; | |
118 } | |
119 bool isEmpty(){ | |
120 return data.length is 0; | |
121 } | |
122 class LocalIterator : Iterator{ | |
123 int idx = -1; | |
124 public this(){ | |
125 } | |
126 public bool hasNext(){ | |
127 return idx+1 < data.length; | |
128 } | |
129 public Object next(){ | |
130 idx++; | |
131 Object res = data[idx]; | |
132 return res; | |
133 } | |
134 public void remove(){ | |
135 implMissing( __FILE__, __LINE__ ); | |
136 this.outer.remove(idx); | |
137 idx--; | |
138 } | |
139 } | |
140 | |
141 Iterator iterator(){ | |
142 return new LocalIterator(); | |
143 } | |
144 int lastIndexOf(Object o){ | |
145 foreach_reverse( i, v; data ){ | |
146 if( data[i] is o ){ | |
147 return i; | |
148 } | |
149 if(( data[i] is null ) || ( o is null )){ | |
150 continue; | |
151 } | |
152 if( data[i] == o ){ | |
153 return i; | |
154 } | |
155 } | |
156 return -1; | |
157 } | |
158 | |
159 class LocalListIterator : ListIterator { | |
160 int idx_next = 0; | |
161 public bool hasNext(){ | |
162 return idx_next < data.length; | |
163 } | |
164 public Object next(){ | |
165 Object res = data[idx_next]; | |
166 idx_next++; | |
167 return res; | |
168 } | |
169 public void remove(){ | |
170 implMissing( __FILE__, __LINE__ ); | |
171 this.outer.remove(idx_next); | |
172 idx_next--; | |
173 } | |
174 public void add(Object o){ | |
175 implMissing( __FILE__, __LINE__ ); | |
176 } | |
177 public bool add(String o){ | |
178 implMissing( __FILE__, __LINE__ ); | |
179 return false; | |
180 } | |
181 public bool hasPrevious(){ | |
182 return idx_next > 0; | |
183 } | |
184 public int nextIndex(){ | |
185 return idx_next; | |
186 } | |
187 public Object previous(){ | |
188 idx_next--; | |
189 Object res = data[idx_next]; | |
190 return res; | |
191 } | |
192 public int previousIndex(){ | |
193 return idx_next-1; | |
194 } | |
195 public void set(Object o){ | |
196 implMissing( __FILE__, __LINE__ ); | |
197 } | |
198 } | |
199 | |
200 ListIterator listIterator(){ | |
201 return new LocalListIterator(); | |
202 } | |
203 ListIterator listIterator(int index){ | |
204 implMissing( __FILE__, __LINE__ ); | |
205 return null; | |
206 } | |
207 Object remove(int index){ | |
208 Object res = data[index]; | |
209 System.arraycopy( data, index+1, data, index, data.length - index - 1 ); | |
210 data.length = data.length -1; | |
211 return res; | |
212 } | |
213 bool remove(Object o){ | |
214 return tango.core.Array.remove(data, o) !is 0; | |
215 } | |
216 public bool remove(String key){ | |
217 return remove(stringcast(key)); | |
218 } | |
219 bool removeAll(Collection c){ | |
220 implMissing( __FILE__, __LINE__ ); | |
221 return false; | |
222 } | |
223 bool retainAll(Collection c){ | |
224 implMissing( __FILE__, __LINE__ ); | |
225 return false; | |
226 } | |
227 protected void removeRange(int fromIndex, int toIndex){ | |
228 implMissing( __FILE__, __LINE__ ); | |
229 } | |
230 Object set(int index, Object element){ | |
231 Object res = data[index]; | |
232 data[index] = element; | |
233 return res; | |
234 } | |
235 int size(){ | |
236 return data.length; | |
237 } | |
238 List subList(int fromIndex, int toIndex){ | |
239 implMissing( __FILE__, __LINE__ ); | |
240 return null; | |
241 } | |
242 Object[] toArray(){ | |
243 return data.dup; | |
244 } | |
245 Object[] toArray(Object[] a){ | |
246 if( data.length <= a.length ){ | |
247 a[ 0 .. data.length ] = data; | |
248 } | |
249 else{ | |
250 return data.dup; | |
251 } | |
252 if( data.length < a.length ){ | |
253 a[data.length] = null; | |
254 } | |
255 return a; | |
256 } | |
257 | |
258 // only for D | |
259 public int opApply (int delegate(ref Object value) dg){ | |
260 foreach( o; data ){ | |
261 auto res = dg( o ); | |
262 if( res ) return res; | |
263 } | |
264 return 0; | |
265 } | |
266 } | |
267 |