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

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