Skip to content
This repository
Browse code

Merge pull request #55 from jentfoo/master

Added more to the avian classpath for the collection interface as well as the list interface
  • Loading branch information...
commit f052c7780f171f925a92560611ad49220aa03eb7 2 parents dfdd1f6 + bd2ebfc
Mike Jensen jentfoo authored
30 classpath/java/util/AbstractCollection.java
@@ -51,6 +51,21 @@ public boolean contains(Object element) {
51 51 return false;
52 52 }
53 53
  54 + public boolean containsAll(Collection<?> c) {
  55 + if (c == null) {
  56 + throw new NullPointerException("Collection is null");
  57 + }
  58 +
  59 + Iterator<?> it = c.iterator();
  60 + while (it.hasNext()) {
  61 + if (! contains(it.next())) {
  62 + return false;
  63 + }
  64 + }
  65 +
  66 + return true;
  67 + }
  68 +
54 69 public boolean isEmpty() {
55 70 return size() == 0;
56 71 }
@@ -60,6 +75,21 @@ public boolean remove(Object element) {
60 75 + this.getClass().getName());
61 76 }
62 77
  78 + public boolean removeAll(Collection<?> c) {
  79 + if (c == null) {
  80 + throw new NullPointerException("Collection is null");
  81 + }
  82 +
  83 + boolean changed = false;
  84 +
  85 + Iterator<?> it = c.iterator();
  86 + while (it.hasNext()) {
  87 + changed = remove(it.next()) || changed;
  88 + }
  89 +
  90 + return changed;
  91 + }
  92 +
63 93 public abstract int size();
64 94
65 95 public Object[] toArray() {
21 classpath/java/util/AbstractList.java
@@ -20,6 +20,27 @@ public boolean add(T o) {
20 20 return true;
21 21 }
22 22
  23 + public boolean addAll(Collection<? extends T> c) {
  24 + return addAll(size(), c);
  25 + }
  26 +
  27 + public boolean addAll(int startIndex, Collection<? extends T> c) {
  28 + if (c == null) {
  29 + throw new NullPointerException("Collection is null");
  30 + }
  31 +
  32 + int index = startIndex;
  33 + boolean changed = false;
  34 +
  35 + Iterator<? extends T> it = c.iterator();
  36 + while (it.hasNext()) {
  37 + add(index++, it.next());
  38 + changed = true;
  39 + }
  40 +
  41 + return changed;
  42 + }
  43 +
23 44 public Iterator<T> iterator() {
24 45 return listIterator();
25 46 }
55 classpath/java/util/Arrays.java
@@ -91,36 +91,15 @@ public static boolean equals(Object[] a, Object[] b) {
91 91 }
92 92
93 93 public static <T> List<T> asList(final T ... array) {
94   - return new List<T>() {
95   - public String toString() {
96   - return Collections.toString(this);
97   - }
98   -
  94 + return new AbstractList<T>() {
99 95 public int size() {
100 96 return array.length;
101 97 }
102 98
103   - public boolean add(T element) {
104   - throw new UnsupportedOperationException();
105   - }
106   -
107   - public boolean addAll(Collection<? extends T> collection) {
108   - throw new UnsupportedOperationException();
109   - }
110   -
111 99 public void add(int index, T element) {
112 100 throw new UnsupportedOperationException();
113 101 }
114   -
115   - public boolean contains(Object element) {
116   - for (int i = 0; i < array.length; ++i) {
117   - if (equal(element, array[i])) {
118   - return true;
119   - }
120   - }
121   - return false;
122   - }
123   -
  102 +
124 103 public int indexOf(Object element) {
125 104 for (int i = 0; i < array.length; ++i) {
126 105 if (equal(element, array[i])) {
@@ -138,7 +117,7 @@ public int lastIndexOf(Object element) {
138 117 }
139 118 return -1;
140 119 }
141   -
  120 +
142 121 public T get(int index) {
143 122 return array[index];
144 123 }
@@ -147,41 +126,13 @@ public T set(int index, T value) {
147 126 throw new UnsupportedOperationException();
148 127 }
149 128
150   - public Object[] toArray() {
151   - return toArray(new Object[size()]);
152   - }
153   -
154   - public <S> S[] toArray(S[] a) {
155   - return (S[])array;
156   - }
157   -
158   - public boolean isEmpty() {
159   - return size() == 0;
160   - }
161   -
162 129 public T remove(int index) {
163   - throw new UnsupportedOperationException();
164   - }
165   -
166   - public boolean remove(Object element) {
167 130 throw new UnsupportedOperationException();
168 131 }
169 132
170   - public void clear() {
171   - throw new UnsupportedOperationException();
172   - }
173   -
174   - public Iterator<T> iterator() {
175   - return listIterator();
176   - }
177   -
178 133 public ListIterator<T> listIterator(int index) {
179 134 return new Collections.ArrayListIterator(this, index);
180 135 }
181   -
182   - public ListIterator<T> listIterator() {
183   - return listIterator(0);
184   - }
185 136 };
186 137 }
187 138
4 classpath/java/util/Collection.java
@@ -17,12 +17,16 @@
17 17
18 18 public boolean contains(Object element);
19 19
  20 + public boolean containsAll(Collection<?> c);
  21 +
20 22 public boolean add(T element);
21 23
22 24 public boolean addAll(Collection<? extends T> collection);
23 25
24 26 public boolean remove(Object element);
25 27
  28 + public boolean removeAll(Collection<?> c);
  29 +
26 30 public Object[] toArray();
27 31
28 32 public <S> S[] toArray(S[] array);
28 classpath/java/util/Collections.java
@@ -170,6 +170,14 @@ public void clear() {
170 170 public Iterator<T> iterator() {
171 171 return new SynchronizedIterator(lock, collection.iterator());
172 172 }
  173 +
  174 + public boolean containsAll(Collection<?> c) {
  175 + synchronized (lock) { return collection.containsAll(c); }
  176 + }
  177 +
  178 + public boolean removeAll(Collection<?> c) {
  179 + synchronized (lock) { return collection.removeAll(c); }
  180 + }
173 181 }
174 182
175 183 static class SynchronizedMap<K,V> implements Map<K,V> {
@@ -393,6 +401,18 @@ public boolean addAll(Collection<? extends T> collection) {
393 401 public void clear() {
394 402 throw new UnsupportedOperationException("not supported");
395 403 }
  404 +
  405 + public boolean removeAll(Collection<?> c) {
  406 + throw new UnsupportedOperationException("not supported");
  407 + }
  408 +
  409 + public boolean addAll(int startIndex, Collection<? extends T> c) {
  410 + throw new UnsupportedOperationException("not supported");
  411 + }
  412 +
  413 + public boolean containsAll(Collection<?> c) {
  414 + return inner.containsAll(c);
  415 + }
396 416 }
397 417
398 418 public static <K,V> Map<K,V> unmodifiableMap(Map<K,V> m) {
@@ -500,6 +520,14 @@ public int size() {
500 520
501 521 public <S> S[] toArray(S[] array) {
502 522 return inner.toArray(array);
  523 + }
  524 +
  525 + public boolean containsAll(Collection<?> c) {
  526 + return inner.containsAll(c);
  527 + }
  528 +
  529 + public boolean removeAll(Collection<?> c) {
  530 + throw new UnsupportedOperationException("not supported");
503 531 }
504 532 }
505 533
35 classpath/java/util/HashMap.java
@@ -303,7 +303,7 @@ public boolean equal(K a, K b) {
303 303 }
304 304 }
305 305
306   - private class EntrySet implements Set<Entry<K, V>> {
  306 + private class EntrySet extends AbstractSet<Entry<K, V>> {
307 307 public int size() {
308 308 return HashMap.this.size();
309 309 }
@@ -321,12 +321,6 @@ public boolean add(Entry<K, V> e) {
321 321 return putCell(e.getKey(), e.getValue()) != null;
322 322 }
323 323
324   - public boolean addAll(Collection<? extends Entry<K, V>> collection) {
325   - boolean change = false;
326   - for (Entry<K, V> e: collection) if (add(e)) change = true;
327   - return change;
328   - }
329   -
330 324 public boolean remove(Object o) {
331 325 return (o instanceof Entry<?,?>) && remove((Entry<?,?>)o);
332 326 }
@@ -352,7 +346,7 @@ public void clear() {
352 346 }
353 347 }
354 348
355   - private class KeySet implements Set<K> {
  349 + private class KeySet extends AbstractSet<K> {
356 350 public int size() {
357 351 return HashMap.this.size();
358 352 }
@@ -369,12 +363,6 @@ public boolean add(K key) {
369 363 return putCell(key, null) != null;
370 364 }
371 365
372   - public boolean addAll(Collection<? extends K> collection) {
373   - boolean change = false;
374   - for (K k: collection) if (add(k)) change = true;
375   - return change;
376   - }
377   -
378 366 public boolean remove(Object key) {
379 367 return removeCell(key) != null;
380 368 }
@@ -409,6 +397,21 @@ public boolean contains(Object value) {
409 397 return containsValue(value);
410 398 }
411 399
  400 + public boolean containsAll(Collection<?> c) {
  401 + if (c == null) {
  402 + throw new NullPointerException("collection is null");
  403 + }
  404 +
  405 + Iterator<?> it = c.iterator();
  406 + while (it.hasNext()) {
  407 + if (! contains(it.next())) {
  408 + return false;
  409 + }
  410 + }
  411 +
  412 + return true;
  413 + }
  414 +
412 415 public boolean add(V value) {
413 416 throw new UnsupportedOperationException();
414 417 }
@@ -421,6 +424,10 @@ public boolean remove(Object value) {
421 424 throw new UnsupportedOperationException();
422 425 }
423 426
  427 + public boolean removeAll(Collection<?> c) {
  428 + throw new UnsupportedOperationException();
  429 + }
  430 +
424 431 public Object[] toArray() {
425 432 return toArray(new Object[size()]);
426 433 }
2  classpath/java/util/List.java
@@ -21,6 +21,8 @@
21 21
22 22 public void add(int index, T element);
23 23
  24 + public boolean addAll(int startIndex, Collection<? extends T> c);
  25 +
24 26 public int indexOf(Object value);
25 27
26 28 public int lastIndexOf(Object value);
21 classpath/java/util/TreeMap.java
@@ -120,7 +120,7 @@ public V setValue(V value) {
120 120
121 121 }
122 122
123   - private class KeySet implements Set<K> {
  123 + private class KeySet extends AbstractSet<K> {
124 124 public int size() {
125 125 return TreeMap.this.size();
126 126 }
@@ -177,6 +177,21 @@ public boolean contains(Object value) {
177 177 return containsValue(value);
178 178 }
179 179
  180 + public boolean containsAll(Collection<?> c) {
  181 + if (c == null) {
  182 + throw new NullPointerException("collection is null");
  183 + }
  184 +
  185 + Iterator<?> it = c.iterator();
  186 + while (it.hasNext()) {
  187 + if (! contains(it.next())) {
  188 + return false;
  189 + }
  190 + }
  191 +
  192 + return true;
  193 + }
  194 +
180 195 public boolean add(V value) {
181 196 throw new UnsupportedOperationException();
182 197 }
@@ -189,6 +204,10 @@ public boolean remove(Object value) {
189 204 throw new UnsupportedOperationException();
190 205 }
191 206
  207 + public boolean removeAll(Collection<?> c) {
  208 + throw new UnsupportedOperationException();
  209 + }
  210 +
192 211 public Object[] toArray() {
193 212 return toArray(new Object[size()]);
194 213 }

0 comments on commit f052c77

Please sign in to comment.
Something went wrong with that request. Please try again.