diff --git a/classpath/java/util/ArrayList.java b/classpath/java/util/ArrayList.java index f8d3ba8ab1..0bd90b481c 100644 --- a/classpath/java/util/ArrayList.java +++ b/classpath/java/util/ArrayList.java @@ -91,6 +91,11 @@ public class ArrayList implements List { return true; } + public boolean addAll(Collection collection) { + for (T t: collection) add(t); + return true; + } + public int indexOf(T element) { for (int i = 0; i < size; ++i) { if (equal(element, array[i])) { diff --git a/classpath/java/util/Arrays.java b/classpath/java/util/Arrays.java index dd2aba24b7..d51b8e6b7a 100644 --- a/classpath/java/util/Arrays.java +++ b/classpath/java/util/Arrays.java @@ -21,6 +21,14 @@ public class Arrays { return (a == null && b == null) || (a != null && a.equals(b)); } + public static void sort(Object[] array) { + sort(array, new Comparator() { + public int compare(Object a, Object b) { + return ((Comparable) a).compareTo(b); + } + }); + } + public static void sort(T[] array, Comparator comparator) { // insertion sort for (int j = 1; j < array.length; ++j) { @@ -48,6 +56,10 @@ public class Arrays { throw new UnsupportedOperationException(); } + public boolean addAll(Collection collection) { + throw new UnsupportedOperationException(); + } + public void add(int index, T element) { throw new UnsupportedOperationException(); } diff --git a/classpath/java/util/Collection.java b/classpath/java/util/Collection.java index 5ce1105811..bfa3ae64b7 100644 --- a/classpath/java/util/Collection.java +++ b/classpath/java/util/Collection.java @@ -19,7 +19,11 @@ public interface Collection extends Iterable { public boolean add(T element); + public boolean addAll(Collection collection); + public boolean remove(T element); + public T[] toArray(T[] array); + public void clear(); } diff --git a/classpath/java/util/Collections.java b/classpath/java/util/Collections.java index afcc38391b..c4d5fc48aa 100644 --- a/classpath/java/util/Collections.java +++ b/classpath/java/util/Collections.java @@ -13,6 +13,25 @@ package java.util; public class Collections { private Collections() { } + static T[] toArray(Collection collection, T[] array) { + Class c = array.getClass().getComponentType(); + + if (array.length > collection.size()) { + array = (T[]) java.lang.reflect.Array.newInstance(c, collection.size()); + } + + int i = 0; + for (Object o: collection) { + if (c.isInstance(o)) { + array[i++] = (T) o; + } else { + throw new ArrayStoreException(); + } + } + + return array; + } + static String toString(Collection c) { StringBuilder sb = new StringBuilder(); sb.append("["); @@ -67,10 +86,18 @@ public class Collections { synchronized (lock) { return collection.add(e); } } + public boolean addAll(Collection collection) { + synchronized (lock) { return this.collection.addAll(collection); } + } + public boolean remove(T e) { synchronized (lock) { return collection.remove(e); } } + public T[] toArray(T[] array) { + synchronized (lock) { return collection.toArray(array); } + } + public void clear() { synchronized (lock) { collection.clear(); } } @@ -87,10 +114,6 @@ public class Collections { public SynchronizedSet(Object lock, Set set) { super(lock, set); } - - public void addAll(Collection c) { - synchronized (lock) { ((Set)collection).addAll(c); } - } } static class SynchronizedIterator implements Iterator { diff --git a/classpath/java/util/HashMap.java b/classpath/java/util/HashMap.java index b94bb54fd3..4f25f678e3 100644 --- a/classpath/java/util/HashMap.java +++ b/classpath/java/util/HashMap.java @@ -307,10 +307,6 @@ public class HashMap implements Map { return HashMap.this.isEmpty(); } - public void addAll(Collection> c) { - throw new UnsupportedOperationException(); - } - public boolean contains(Entry e) { return containsKey(e.getKey()); } @@ -319,10 +315,20 @@ 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(Entry e) { return removeCell(e.getKey()) != null; } + public T[] toArray(T[] array) { + return Collections.toArray(this, array); + } + public void clear() { HashMap.this.clear(); } @@ -345,18 +351,24 @@ public class HashMap implements Map { return containsKey(key); } - public void addAll(Collection c) { - throw new UnsupportedOperationException(); - } - public boolean add(K key) { 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(K key) { return removeCell(key) != null; } + public T[] toArray(T[] array) { + return Collections.toArray(this, array); + } + public void clear() { HashMap.this.clear(); } @@ -384,10 +396,18 @@ public class HashMap implements Map { throw new UnsupportedOperationException(); } + public boolean addAll(Collection collection) { + throw new UnsupportedOperationException(); + } + public boolean remove(V value) { throw new UnsupportedOperationException(); } + public T[] toArray(T[] array) { + return Collections.toArray(this, array); + } + public void clear() { HashMap.this.clear(); } diff --git a/classpath/java/util/HashSet.java b/classpath/java/util/HashSet.java index 30207e61e1..d541dc78cc 100644 --- a/classpath/java/util/HashSet.java +++ b/classpath/java/util/HashSet.java @@ -40,18 +40,24 @@ public class HashSet implements Set { return map.containsKey(element); } - public void addAll(Collection c) { - for (T t: c) add(t); - } - public boolean add(T element) { return map.put(element, Value) != Value; } + public boolean addAll(Collection collection) { + boolean change = false; + for (T t: collection) if (add(t)) change = true; + return change; + } + public boolean remove(T element) { return map.remove(element) != Value; } + public T[] toArray(T[] array) { + return Collections.toArray(this, array); + } + public void clear() { map.clear(); } diff --git a/classpath/java/util/LinkedList.java b/classpath/java/util/LinkedList.java index 17e9ed24b7..aefa979313 100644 --- a/classpath/java/util/LinkedList.java +++ b/classpath/java/util/LinkedList.java @@ -86,20 +86,7 @@ public class LinkedList implements List { } public S[] toArray(S[] a) { - Object[] retVal = null; - if (a.length >= size) { - retVal = a; - } else { - retVal = new Object[size]; - } - int i=0; - for (Object o : this) { - retVal[i++] = o; - } - if (a.length > size) { - a[size] = null; - } - return (S[])retVal; + return Collections.toArray(this, a); } public int size() { @@ -119,6 +106,11 @@ public class LinkedList implements List { return true; } + public boolean addAll(Collection collection) { + for (T t: collection) add(t); + return true; + } + public void add(int index, T element) { if (index == 0) { addFirst(element); diff --git a/classpath/java/util/Set.java b/classpath/java/util/Set.java index 2ac227b70b..b1281aebbc 100644 --- a/classpath/java/util/Set.java +++ b/classpath/java/util/Set.java @@ -10,6 +10,4 @@ package java.util; -public interface Set extends Collection { - public void addAll(Collection c); -} +public interface Set extends Collection { } diff --git a/classpath/java/util/TreeSet.java b/classpath/java/util/TreeSet.java index 5deaca8cef..4a0c16cf4d 100644 --- a/classpath/java/util/TreeSet.java +++ b/classpath/java/util/TreeSet.java @@ -41,6 +41,12 @@ public class TreeSet implements Collection { return false; } + public boolean addAll(Collection collection) { + boolean change = false; + for (T t: collection) if (add(t)) change = true; + return change; + } + // Used by hashMaps for replacement public void addAndReplace(T value) { PersistentSet.Path> p = set.find(new Cell(value, null)); @@ -69,6 +75,10 @@ public class TreeSet implements Collection { } } + public T[] toArray(T[] array) { + return Collections.toArray(this, array); + } + public int size() { return size; } diff --git a/classpath/java/util/Vector.java b/classpath/java/util/Vector.java index 138cfbd665..54d2b9ccc7 100644 --- a/classpath/java/util/Vector.java +++ b/classpath/java/util/Vector.java @@ -44,6 +44,10 @@ public class Vector implements List { return list.add(element); } + public synchronized boolean addAll(Collection collection) { + return list.addAll(collection); + } + public void addElement(T element) { add(element); } diff --git a/makefile b/makefile index 925c07ed25..bd233938c5 100644 --- a/makefile +++ b/makefile @@ -263,7 +263,7 @@ $(classpath-dep): $(classpath-sources) @echo "compiling classpath classes" @mkdir -p $(dir $(@)) $(javac) -d $(dir $(@)) -bootclasspath $(classpath-build) \ - $(shell make -s $(classpath-classes)) + $(shell make -s --no-print-directory $(classpath-classes)) @touch $(@) $(test-build)/%.class: $(test)/%.java @@ -273,7 +273,7 @@ $(test-dep): $(test-sources) @echo "compiling test classes" @mkdir -p $(dir $(@)) $(javac) -d $(dir $(@)) -bootclasspath $(classpath-build) \ - $(shell make -s $(test-classes)) + $(shell make -s --no-print-directory $(test-classes)) @touch $(@) define compile-object