mirror of
https://github.com/corda/corda.git
synced 2025-01-01 02:36:44 +00:00
119 lines
2.6 KiB
Java
119 lines
2.6 KiB
Java
package java.util;
|
|
|
|
public class Collections {
|
|
private Collections() { }
|
|
|
|
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> {
|
|
private final Object lock;
|
|
private final Collection<T> collection;
|
|
|
|
public SynchronizedCollection(Object lock, Collection<T> collection) {
|
|
this.lock = lock;
|
|
this.collection = collection;
|
|
}
|
|
|
|
public synchronized int size() {
|
|
synchronized (lock) { return collection.size(); }
|
|
}
|
|
|
|
public synchronized boolean contains(T e) {
|
|
synchronized (lock) { return collection.contains(e); }
|
|
}
|
|
|
|
public synchronized boolean add(T e) {
|
|
synchronized (lock) { return collection.add(e); }
|
|
}
|
|
|
|
public synchronized boolean remove(T e) {
|
|
synchronized (lock) { return collection.remove(e); }
|
|
}
|
|
|
|
public synchronized void clear() {
|
|
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(); }
|
|
}
|
|
}
|
|
|
|
static class ArrayListIterator<T> implements Iterator<T> {
|
|
private final List<T> list;
|
|
private boolean canRemove = false;
|
|
private int index = -1;
|
|
|
|
public ArrayListIterator(List<T> list) {
|
|
this.list = list;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|