mirror of
https://github.com/corda/corda.git
synced 2025-06-13 04:38:19 +00:00
fun with collections
This commit is contained in:
114
classpath/java/util/Collections.java
Normal file
114
classpath/java/util/Collections.java
Normal file
@ -0,0 +1,114 @@
|
||||
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 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user