2008-02-19 18:06:52 +00:00
|
|
|
/* Copyright (c) 2008, Avian Contributors
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
2007-07-22 03:47:29 +00:00
|
|
|
package java.util;
|
|
|
|
|
|
|
|
public class Collections {
|
|
|
|
private Collections() { }
|
|
|
|
|
2008-02-28 18:37:10 +00:00
|
|
|
static <T> T[] toArray(Collection collection, T[] array) {
|
|
|
|
Class c = array.getClass().getComponentType();
|
|
|
|
|
2008-03-05 21:21:53 +00:00
|
|
|
if (array.length < collection.size()) {
|
2008-02-28 18:37:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-10-17 01:17:37 +00:00
|
|
|
static String toString(Collection c) {
|
2007-08-24 01:57:42 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append("[");
|
2007-10-17 01:17:37 +00:00
|
|
|
for (Iterator it = c.iterator(); it.hasNext();) {
|
2007-08-24 01:57:42 +00:00
|
|
|
sb.append(it.next());
|
|
|
|
if (it.hasNext()) {
|
|
|
|
sb.append(",");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.append("]");
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
2007-07-22 03:47:29 +00:00
|
|
|
static class IteratorEnumeration<T> implements Enumeration<T> {
|
|
|
|
private final Iterator<T> it;
|
|
|
|
|
|
|
|
public IteratorEnumeration(Iterator<T> it) {
|
|
|
|
this.it = it;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T nextElement() {
|
|
|
|
return it.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasMoreElements() {
|
|
|
|
return it.hasNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class SynchronizedCollection<T> implements Collection<T> {
|
2007-09-26 15:19:21 +00:00
|
|
|
protected final Object lock;
|
|
|
|
protected final Collection<T> collection;
|
2007-07-22 03:47:29 +00:00
|
|
|
|
|
|
|
public SynchronizedCollection(Object lock, Collection<T> collection) {
|
|
|
|
this.lock = lock;
|
|
|
|
this.collection = collection;
|
|
|
|
}
|
|
|
|
|
2008-01-28 15:10:23 +00:00
|
|
|
public int size() {
|
2007-07-22 03:47:29 +00:00
|
|
|
synchronized (lock) { return collection.size(); }
|
|
|
|
}
|
|
|
|
|
2007-11-07 16:48:09 +00:00
|
|
|
public boolean isEmpty() {
|
|
|
|
return size() == 0;
|
|
|
|
}
|
|
|
|
|
2008-01-28 15:10:23 +00:00
|
|
|
public boolean contains(T e) {
|
2007-07-22 19:06:21 +00:00
|
|
|
synchronized (lock) { return collection.contains(e); }
|
|
|
|
}
|
|
|
|
|
2008-01-28 15:10:23 +00:00
|
|
|
public boolean add(T e) {
|
2007-07-22 03:47:29 +00:00
|
|
|
synchronized (lock) { return collection.add(e); }
|
|
|
|
}
|
|
|
|
|
2008-02-28 18:37:10 +00:00
|
|
|
public boolean addAll(Collection<? extends T> collection) {
|
|
|
|
synchronized (lock) { return this.collection.addAll(collection); }
|
|
|
|
}
|
|
|
|
|
2008-01-28 15:10:23 +00:00
|
|
|
public boolean remove(T e) {
|
2007-07-22 03:47:29 +00:00
|
|
|
synchronized (lock) { return collection.remove(e); }
|
|
|
|
}
|
|
|
|
|
2008-02-28 18:37:10 +00:00
|
|
|
public <T> T[] toArray(T[] array) {
|
|
|
|
synchronized (lock) { return collection.toArray(array); }
|
|
|
|
}
|
|
|
|
|
2008-01-28 15:10:23 +00:00
|
|
|
public void clear() {
|
2007-07-22 03:47:29 +00:00
|
|
|
synchronized (lock) { collection.clear(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public Iterator<T> iterator() {
|
|
|
|
return new SynchronizedIterator(lock, collection.iterator());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class SynchronizedSet<T>
|
|
|
|
extends SynchronizedCollection<T>
|
|
|
|
implements Set<T>
|
|
|
|
{
|
|
|
|
public SynchronizedSet(Object lock, Set<T> set) {
|
|
|
|
super(lock, set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class SynchronizedIterator<T> implements Iterator<T> {
|
|
|
|
private final Object lock;
|
|
|
|
private final Iterator<T> it;
|
|
|
|
|
|
|
|
public SynchronizedIterator(Object lock, Iterator<T> it) {
|
|
|
|
this.lock = lock;
|
|
|
|
this.it = it;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T next() {
|
|
|
|
synchronized (lock) { return it.next(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
synchronized (lock) { return it.hasNext(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public void remove() {
|
|
|
|
synchronized (lock) { it.remove(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-26 15:48:59 +00:00
|
|
|
static class ArrayListIterator<T> implements ListIterator<T> {
|
2007-07-22 03:47:29 +00:00
|
|
|
private final List<T> list;
|
|
|
|
private boolean canRemove = false;
|
2007-09-26 15:48:59 +00:00
|
|
|
private int index;
|
2007-07-22 03:47:29 +00:00
|
|
|
|
|
|
|
public ArrayListIterator(List<T> list) {
|
2007-09-26 15:48:59 +00:00
|
|
|
this(list, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayListIterator(List<T> list, int index) {
|
2007-07-22 03:47:29 +00:00
|
|
|
this.list = list;
|
2007-09-26 15:48:59 +00:00
|
|
|
this.index = index - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasPrevious() {
|
|
|
|
return index >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T previous() {
|
|
|
|
if (hasPrevious()) {
|
|
|
|
canRemove = true;
|
|
|
|
return list.get(index--);
|
|
|
|
} else {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
2007-07-22 03:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public T next() {
|
|
|
|
if (hasNext()) {
|
|
|
|
canRemove = true;
|
|
|
|
return list.get(++index);
|
|
|
|
} else {
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
return index + 1 < list.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void remove() {
|
|
|
|
if (canRemove) {
|
|
|
|
canRemove = false;
|
|
|
|
list.remove(index--);
|
|
|
|
} else {
|
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|