implement a few more classpath methods, including Collection.addAll and Collection.toArray

This commit is contained in:
Joel Dice 2008-02-28 11:37:10 -07:00
parent c810eb36d8
commit 9d76d6a04e
11 changed files with 109 additions and 35 deletions

View File

@ -91,6 +91,11 @@ public class ArrayList<T> implements List<T> {
return true;
}
public boolean addAll(Collection<? extends T> 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])) {

View File

@ -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 <T> void sort(T[] array, Comparator<? super T> 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<? extends T> collection) {
throw new UnsupportedOperationException();
}
public void add(int index, T element) {
throw new UnsupportedOperationException();
}

View File

@ -19,7 +19,11 @@ public interface Collection<T> extends Iterable<T> {
public boolean add(T element);
public boolean addAll(Collection<? extends T> collection);
public boolean remove(T element);
public <T> T[] toArray(T[] array);
public void clear();
}

View File

@ -13,6 +13,25 @@ package java.util;
public class Collections {
private Collections() { }
static <T> 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<? extends T> collection) {
synchronized (lock) { return this.collection.addAll(collection); }
}
public boolean remove(T e) {
synchronized (lock) { return collection.remove(e); }
}
public <T> 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<T> set) {
super(lock, set);
}
public void addAll(Collection<T> c) {
synchronized (lock) { ((Set<T>)collection).addAll(c); }
}
}
static class SynchronizedIterator<T> implements Iterator<T> {

View File

@ -307,10 +307,6 @@ public class HashMap<K, V> implements Map<K, V> {
return HashMap.this.isEmpty();
}
public void addAll(Collection<Entry<K, V>> c) {
throw new UnsupportedOperationException();
}
public boolean contains(Entry<K, V> e) {
return containsKey(e.getKey());
}
@ -319,10 +315,20 @@ public class HashMap<K, V> implements Map<K, V> {
return putCell(e.getKey(), e.getValue()) != null;
}
public boolean addAll(Collection<? extends Entry<K, V>> collection) {
boolean change = false;
for (Entry<K, V> e: collection) if (add(e)) change = true;
return change;
}
public boolean remove(Entry<K, V> e) {
return removeCell(e.getKey()) != null;
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
public void clear() {
HashMap.this.clear();
}
@ -345,18 +351,24 @@ public class HashMap<K, V> implements Map<K, V> {
return containsKey(key);
}
public void addAll(Collection<K> c) {
throw new UnsupportedOperationException();
}
public boolean add(K key) {
return putCell(key, null) != null;
}
public boolean addAll(Collection<? extends K> 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> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
public void clear() {
HashMap.this.clear();
}
@ -384,10 +396,18 @@ public class HashMap<K, V> implements Map<K, V> {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection<? extends V> collection) {
throw new UnsupportedOperationException();
}
public boolean remove(V value) {
throw new UnsupportedOperationException();
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
public void clear() {
HashMap.this.clear();
}

View File

@ -40,18 +40,24 @@ public class HashSet<T> implements Set<T> {
return map.containsKey(element);
}
public void addAll(Collection<T> c) {
for (T t: c) add(t);
}
public boolean add(T element) {
return map.put(element, Value) != Value;
}
public boolean addAll(Collection<? extends T> 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> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
public void clear() {
map.clear();
}

View File

@ -86,20 +86,7 @@ public class LinkedList<T> implements List<T> {
}
public <S> 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<T> implements List<T> {
return true;
}
public boolean addAll(Collection<? extends T> collection) {
for (T t: collection) add(t);
return true;
}
public void add(int index, T element) {
if (index == 0) {
addFirst(element);

View File

@ -10,6 +10,4 @@
package java.util;
public interface Set<T> extends Collection<T> {
public void addAll(Collection<T> c);
}
public interface Set<T> extends Collection<T> { }

View File

@ -41,6 +41,12 @@ public class TreeSet<T> implements Collection<T> {
return false;
}
public boolean addAll(Collection<? extends T> 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<Cell<T>> p = set.find(new Cell(value, null));
@ -69,6 +75,10 @@ public class TreeSet<T> implements Collection<T> {
}
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
public int size() {
return size;
}

View File

@ -44,6 +44,10 @@ public class Vector<T> implements List<T> {
return list.add(element);
}
public synchronized boolean addAll(Collection<? extends T> collection) {
return list.addAll(collection);
}
public void addElement(T element) {
add(element);
}

View File

@ -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