diff --git a/classpath/java/util/AbstractCollection.java b/classpath/java/util/AbstractCollection.java index f7e06bad32..248849876e 100644 --- a/classpath/java/util/AbstractCollection.java +++ b/classpath/java/util/AbstractCollection.java @@ -51,6 +51,21 @@ public abstract class AbstractCollection implements Collection { return false; } + public boolean containsAll(Collection c) { + if (c == null) { + throw new NullPointerException("Collection is null"); + } + + Iterator it = c.iterator(); + while (it.hasNext()) { + if (! contains(it.next())) { + return false; + } + } + + return true; + } + public boolean isEmpty() { return size() == 0; } @@ -60,6 +75,21 @@ public abstract class AbstractCollection implements Collection { + this.getClass().getName()); } + public boolean removeAll(Collection c) { + if (c == null) { + throw new NullPointerException("Collection is null"); + } + + boolean changed = false; + + Iterator it = c.iterator(); + while (it.hasNext()) { + changed = remove(it.next()) || changed; + } + + return changed; + } + public abstract int size(); public Object[] toArray() { diff --git a/classpath/java/util/AbstractList.java b/classpath/java/util/AbstractList.java index 87cbd6fb6e..7ec1b10927 100644 --- a/classpath/java/util/AbstractList.java +++ b/classpath/java/util/AbstractList.java @@ -20,6 +20,27 @@ public abstract class AbstractList extends AbstractCollection return true; } + public boolean addAll(Collection c) { + return addAll(size(), c); + } + + public boolean addAll(int startIndex, Collection c) { + if (c == null) { + throw new NullPointerException("Collection is null"); + } + + int index = startIndex; + boolean changed = false; + + Iterator it = c.iterator(); + while (it.hasNext()) { + add(index++, it.next()); + changed = true; + } + + return changed; + } + public Iterator iterator() { return listIterator(); } diff --git a/classpath/java/util/Arrays.java b/classpath/java/util/Arrays.java index 1bf35c1114..57d053d4db 100644 --- a/classpath/java/util/Arrays.java +++ b/classpath/java/util/Arrays.java @@ -91,36 +91,15 @@ public class Arrays { } public static List asList(final T ... array) { - return new List() { - public String toString() { - return Collections.toString(this); - } - + return new AbstractList() { public int size() { return array.length; } - public boolean add(T element) { - throw new UnsupportedOperationException(); - } - - public boolean addAll(Collection collection) { - throw new UnsupportedOperationException(); - } - public void add(int index, T element) { throw new UnsupportedOperationException(); } - - public boolean contains(Object element) { - for (int i = 0; i < array.length; ++i) { - if (equal(element, array[i])) { - return true; - } - } - return false; - } - + public int indexOf(Object element) { for (int i = 0; i < array.length; ++i) { if (equal(element, array[i])) { @@ -138,7 +117,7 @@ public class Arrays { } return -1; } - + public T get(int index) { return array[index]; } @@ -147,41 +126,13 @@ public class Arrays { throw new UnsupportedOperationException(); } - public Object[] toArray() { - return toArray(new Object[size()]); - } - - public S[] toArray(S[] a) { - return (S[])array; - } - - public boolean isEmpty() { - return size() == 0; - } - public T remove(int index) { - throw new UnsupportedOperationException(); - } - - public boolean remove(Object element) { throw new UnsupportedOperationException(); } - public void clear() { - throw new UnsupportedOperationException(); - } - - public Iterator iterator() { - return listIterator(); - } - public ListIterator listIterator(int index) { return new Collections.ArrayListIterator(this, index); } - - public ListIterator listIterator() { - return listIterator(0); - } }; } diff --git a/classpath/java/util/Collection.java b/classpath/java/util/Collection.java index c0ca5cbe5b..36505d6f09 100644 --- a/classpath/java/util/Collection.java +++ b/classpath/java/util/Collection.java @@ -17,12 +17,16 @@ public interface Collection extends Iterable { public boolean contains(Object element); + public boolean containsAll(Collection c); + public boolean add(T element); public boolean addAll(Collection collection); public boolean remove(Object element); + public boolean removeAll(Collection c); + public Object[] toArray(); public S[] toArray(S[] array); diff --git a/classpath/java/util/Collections.java b/classpath/java/util/Collections.java index acb94cb40d..fa522799e4 100644 --- a/classpath/java/util/Collections.java +++ b/classpath/java/util/Collections.java @@ -170,6 +170,14 @@ public class Collections { public Iterator iterator() { return new SynchronizedIterator(lock, collection.iterator()); } + + public boolean containsAll(Collection c) { + synchronized (lock) { return collection.containsAll(c); } + } + + public boolean removeAll(Collection c) { + synchronized (lock) { return collection.removeAll(c); } + } } static class SynchronizedMap implements Map { @@ -393,6 +401,18 @@ public class Collections { public void clear() { throw new UnsupportedOperationException("not supported"); } + + public boolean removeAll(Collection c) { + throw new UnsupportedOperationException("not supported"); + } + + public boolean addAll(int startIndex, Collection c) { + throw new UnsupportedOperationException("not supported"); + } + + public boolean containsAll(Collection c) { + return inner.containsAll(c); + } } public static Map unmodifiableMap(Map m) { @@ -500,6 +520,14 @@ public class Collections { public S[] toArray(S[] array) { return inner.toArray(array); + } + + public boolean containsAll(Collection c) { + return inner.containsAll(c); + } + + public boolean removeAll(Collection c) { + throw new UnsupportedOperationException("not supported"); } } diff --git a/classpath/java/util/HashMap.java b/classpath/java/util/HashMap.java index 2931b52300..da378d94b8 100644 --- a/classpath/java/util/HashMap.java +++ b/classpath/java/util/HashMap.java @@ -303,7 +303,7 @@ public class HashMap implements Map { } } - private class EntrySet implements Set> { + private class EntrySet extends AbstractSet> { public int size() { return HashMap.this.size(); } @@ -321,12 +321,6 @@ public class HashMap implements Map { return putCell(e.getKey(), e.getValue()) != null; } - public boolean addAll(Collection> collection) { - boolean change = false; - for (Entry e: collection) if (add(e)) change = true; - return change; - } - public boolean remove(Object o) { return (o instanceof Entry) && remove((Entry)o); } @@ -352,7 +346,7 @@ public class HashMap implements Map { } } - private class KeySet implements Set { + private class KeySet extends AbstractSet { public int size() { return HashMap.this.size(); } @@ -369,12 +363,6 @@ public class HashMap implements Map { return putCell(key, null) != null; } - public boolean addAll(Collection collection) { - boolean change = false; - for (K k: collection) if (add(k)) change = true; - return change; - } - public boolean remove(Object key) { return removeCell(key) != null; } @@ -409,6 +397,21 @@ public class HashMap implements Map { return containsValue(value); } + public boolean containsAll(Collection c) { + if (c == null) { + throw new NullPointerException("collection is null"); + } + + Iterator it = c.iterator(); + while (it.hasNext()) { + if (! contains(it.next())) { + return false; + } + } + + return true; + } + public boolean add(V value) { throw new UnsupportedOperationException(); } @@ -421,6 +424,10 @@ public class HashMap implements Map { throw new UnsupportedOperationException(); } + public boolean removeAll(Collection c) { + throw new UnsupportedOperationException(); + } + public Object[] toArray() { return toArray(new Object[size()]); } diff --git a/classpath/java/util/List.java b/classpath/java/util/List.java index f29284206b..ceb555e395 100644 --- a/classpath/java/util/List.java +++ b/classpath/java/util/List.java @@ -21,6 +21,8 @@ public interface List extends Collection { public void add(int index, T element); + public boolean addAll(int startIndex, Collection c); + public int indexOf(Object value); public int lastIndexOf(Object value); diff --git a/classpath/java/util/TreeMap.java b/classpath/java/util/TreeMap.java index 6100dbce80..cc8da2a320 100644 --- a/classpath/java/util/TreeMap.java +++ b/classpath/java/util/TreeMap.java @@ -120,7 +120,7 @@ public class TreeMap implements Map { } - private class KeySet implements Set { + private class KeySet extends AbstractSet { public int size() { return TreeMap.this.size(); } @@ -177,6 +177,21 @@ public class TreeMap implements Map { return containsValue(value); } + public boolean containsAll(Collection c) { + if (c == null) { + throw new NullPointerException("collection is null"); + } + + Iterator it = c.iterator(); + while (it.hasNext()) { + if (! contains(it.next())) { + return false; + } + } + + return true; + } + public boolean add(V value) { throw new UnsupportedOperationException(); } @@ -189,6 +204,10 @@ public class TreeMap implements Map { throw new UnsupportedOperationException(); } + public boolean removeAll(Collection c) { + throw new UnsupportedOperationException(); + } + public Object[] toArray() { return toArray(new Object[size()]); }