mirror of
https://github.com/corda/corda.git
synced 2025-02-05 10:39:13 +00:00
fun with collections
This commit is contained in:
parent
da17490206
commit
ecd31a10a4
19
classpath/java/lang/IndexOutOfBoundsException.java
Normal file
19
classpath/java/lang/IndexOutOfBoundsException.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package java.lang;
|
||||||
|
|
||||||
|
public class IndexOutOfBoundsException extends RuntimeException {
|
||||||
|
public IndexOutOfBoundsException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IndexOutOfBoundsException(String message) {
|
||||||
|
this(message, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IndexOutOfBoundsException(Throwable cause) {
|
||||||
|
this(null, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IndexOutOfBoundsException() {
|
||||||
|
this(null, null);
|
||||||
|
}
|
||||||
|
}
|
19
classpath/java/lang/UnsupportedOperationException.java
Normal file
19
classpath/java/lang/UnsupportedOperationException.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package java.lang;
|
||||||
|
|
||||||
|
public class UnsupportedOperationException extends RuntimeException {
|
||||||
|
public UnsupportedOperationException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UnsupportedOperationException(String message) {
|
||||||
|
this(message, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UnsupportedOperationException(Throwable cause) {
|
||||||
|
this(null, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UnsupportedOperationException() {
|
||||||
|
this(null, null);
|
||||||
|
}
|
||||||
|
}
|
95
classpath/java/util/ArrayList.java
Normal file
95
classpath/java/util/ArrayList.java
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
package java.util;
|
||||||
|
|
||||||
|
public class ArrayList<T> implements List<T> {
|
||||||
|
private Object[] array;
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
public ArrayList(int capacity) {
|
||||||
|
if (capacity != 0) {
|
||||||
|
array = new Object[capacity];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList() {
|
||||||
|
this(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resize() {
|
||||||
|
if (array == null || size >= array.length - 1) {
|
||||||
|
resize(array == null ? 16 : array.length * 2);
|
||||||
|
} else if (size <= array.length / 3) {
|
||||||
|
resize(array.length / 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resize(int capacity) {
|
||||||
|
Object[] newArray = null;
|
||||||
|
if (capacity != 0) {
|
||||||
|
if (array != null && array.length == capacity) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
newArray = new Object[capacity];
|
||||||
|
if (array != null) {
|
||||||
|
System.arraycopy(array, 0, newArray, 0, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
array = newArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean equal(Object a, Object b) {
|
||||||
|
return (a == null && b == null) || (a != null && a.equals(b));
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean add(T element) {
|
||||||
|
resize();
|
||||||
|
array[size++] = element;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T get(int index) {
|
||||||
|
if (index >= 0 && index < size) {
|
||||||
|
return (T) array[index];
|
||||||
|
} else {
|
||||||
|
throw new IndexOutOfBoundsException(index + " not in [0, " + size + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T remove(int index) {
|
||||||
|
T v = get(index);
|
||||||
|
|
||||||
|
if (index == size - 1) {
|
||||||
|
array[index] = null;
|
||||||
|
} else {
|
||||||
|
System.arraycopy(array, index + 1, array, index, size - index);
|
||||||
|
}
|
||||||
|
|
||||||
|
-- size;
|
||||||
|
resize();
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean remove(T element) {
|
||||||
|
for (int i = 0; i < size; ++i) {
|
||||||
|
if (equal(element, array[i])) {
|
||||||
|
remove(i);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
array = null;
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterator<T> iterator() {
|
||||||
|
return new Collections.ArrayListIterator(this);
|
||||||
|
}
|
||||||
|
}
|
37
classpath/java/util/Arrays.java
Normal file
37
classpath/java/util/Arrays.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package java.util;
|
||||||
|
|
||||||
|
public class Arrays {
|
||||||
|
private Arrays() { }
|
||||||
|
|
||||||
|
public static <T> List<T> asList(final T ... array) {
|
||||||
|
return new List<T>() {
|
||||||
|
public int size() {
|
||||||
|
return array.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean add(T element) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public T get(int index) {
|
||||||
|
return array[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
public T remove(int index) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean remove(T element) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterator<T> iterator() {
|
||||||
|
return new Collections.ArrayListIterator(this);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
classpath/java/util/Enumeration.java
Normal file
7
classpath/java/util/Enumeration.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package java.util;
|
||||||
|
|
||||||
|
public interface Enumeration<T> {
|
||||||
|
public T nextElement();
|
||||||
|
|
||||||
|
public boolean hasMoreElements();
|
||||||
|
}
|
54
classpath/java/util/Hashtable.java
Normal file
54
classpath/java/util/Hashtable.java
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package java.util;
|
||||||
|
|
||||||
|
public class Hashtable<K, V> implements Map<K, V> {
|
||||||
|
private final HashMap<K, V> map;
|
||||||
|
|
||||||
|
public Hashtable(int capacity) {
|
||||||
|
map = new HashMap(capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Hashtable() {
|
||||||
|
this(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized int size() {
|
||||||
|
return map.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized V get(K key) {
|
||||||
|
return map.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized V put(K key, V value) {
|
||||||
|
return map.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized V remove(K key) {
|
||||||
|
return map.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void clear() {
|
||||||
|
map.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Enumeration<K> keys() {
|
||||||
|
return new Collections.IteratorEnumeration(keySet().iterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Enumeration<V> elements() {
|
||||||
|
return new Collections.IteratorEnumeration(values().iterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Entry<K, V>> entrySet() {
|
||||||
|
return new Collections.SynchronizedSet(this, map.entrySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<K> keySet() {
|
||||||
|
return new Collections.SynchronizedSet(this, map.keySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<V> values() {
|
||||||
|
return new Collections.SynchronizedCollection(this, map.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
140
classpath/java/util/LinkedList.java
Normal file
140
classpath/java/util/LinkedList.java
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
package java.util;
|
||||||
|
|
||||||
|
public class LinkedList<T> implements List<T> {
|
||||||
|
private Cell<T> front;
|
||||||
|
private Cell<T> rear;
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
private Cell<T> find(int index) {
|
||||||
|
int i = 0;
|
||||||
|
for (Cell<T> c = front; c != null; c = c.next) {
|
||||||
|
if (i == index) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
++ i;
|
||||||
|
}
|
||||||
|
throw new IndexOutOfBoundsException(index + " not in [0, " + size + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean equal(Object a, Object b) {
|
||||||
|
return (a == null && b == null) || (a != null && a.equals(b));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void add(Cell<T> c) {
|
||||||
|
++ size;
|
||||||
|
|
||||||
|
if (front == null) {
|
||||||
|
front = rear = c;
|
||||||
|
} else {
|
||||||
|
c.prev = rear;
|
||||||
|
rear = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Cell<T> find(T element) {
|
||||||
|
for (Cell<T> c = front; c != null; c = c.next) {
|
||||||
|
if (equal(c.value, element)) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void remove(Cell<T> c) {
|
||||||
|
-- size;
|
||||||
|
|
||||||
|
if (c.prev == null) {
|
||||||
|
front = c.next;
|
||||||
|
} else {
|
||||||
|
c.prev.next = c.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c.next == null) {
|
||||||
|
rear = c.prev;
|
||||||
|
} else {
|
||||||
|
c.next.prev = c.prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean add(T element) {
|
||||||
|
add(new Cell(element, null, null));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T get(int index) {
|
||||||
|
return find(index).value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T remove(int index) {
|
||||||
|
Cell<T> c = find(index);
|
||||||
|
remove(c);
|
||||||
|
return c.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean remove(T element) {
|
||||||
|
Cell<T> c = find(element);
|
||||||
|
if (c == null) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
remove(c);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
front = rear = null;
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterator<T> iterator() {
|
||||||
|
return new MyIterator(front);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Cell<T> {
|
||||||
|
public T value;
|
||||||
|
public Cell<T> prev;
|
||||||
|
public Cell<T> next;
|
||||||
|
|
||||||
|
public Cell(T value, Cell<T> prev, Cell<T> next) {
|
||||||
|
this.value = value;
|
||||||
|
this.prev = prev;
|
||||||
|
this.next = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class MyIterator implements Iterator<T> {
|
||||||
|
private Cell<T> current;
|
||||||
|
private Cell<T> next;
|
||||||
|
|
||||||
|
public MyIterator(Cell<T> start) {
|
||||||
|
next = start;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T next() {
|
||||||
|
if (hasNext()) {
|
||||||
|
current = next;
|
||||||
|
next = next.next;
|
||||||
|
return current.value;
|
||||||
|
} else {
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasNext() {
|
||||||
|
return next != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove() {
|
||||||
|
if (current != null) {
|
||||||
|
LinkedList.this.remove(current);
|
||||||
|
current = null;
|
||||||
|
} else {
|
||||||
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
classpath/java/util/List.java
Normal file
7
classpath/java/util/List.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package java.util;
|
||||||
|
|
||||||
|
public interface List<T> extends Collection<T> {
|
||||||
|
public T get(int index);
|
||||||
|
|
||||||
|
public T remove(int index);
|
||||||
|
}
|
20
classpath/java/util/Stack.java
Normal file
20
classpath/java/util/Stack.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package java.util;
|
||||||
|
|
||||||
|
public class Stack<T> extends Vector<T> {
|
||||||
|
public boolean empty() {
|
||||||
|
return size() != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T peek() {
|
||||||
|
return get(size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T pop() {
|
||||||
|
return remove(size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T push(T element) {
|
||||||
|
add(element);
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
}
|
54
classpath/java/util/Vector.java
Normal file
54
classpath/java/util/Vector.java
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package java.util;
|
||||||
|
|
||||||
|
public class Vector<T> implements List<T> {
|
||||||
|
private final ArrayList<T> list;
|
||||||
|
|
||||||
|
public Vector(int capacity) {
|
||||||
|
list = new ArrayList(capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector() {
|
||||||
|
this(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized int size() {
|
||||||
|
return list.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean add(T element) {
|
||||||
|
return list.add(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addElement(T element) {
|
||||||
|
add(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized T get(int index) {
|
||||||
|
return list.get(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T elementAt(int index) {
|
||||||
|
return get(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized T remove(int index) {
|
||||||
|
return list.remove(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean remove(T element) {
|
||||||
|
return list.remove(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void clear() {
|
||||||
|
list.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterator<T> iterator() {
|
||||||
|
return new Collections.ArrayListIterator(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Enumeration<T> elements() {
|
||||||
|
return new Collections.IteratorEnumeration(iterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user