mirror of
https://github.com/corda/corda.git
synced 2025-02-13 22:22:35 +00:00
add AbstractList and AbstractSequentialList classes to java.util and listIterator methods to java.util.List
This commit is contained in:
parent
93597a4d1d
commit
7911989055
15
classpath/java/util/AbstractList.java
Normal file
15
classpath/java/util/AbstractList.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
/* Copyright (c) 2009, 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. */
|
||||||
|
|
||||||
|
package java.util;
|
||||||
|
|
||||||
|
public abstract class AbstractList<T> extends AbstractCollection<T>
|
||||||
|
implements List<T>
|
||||||
|
{ }
|
15
classpath/java/util/AbstractSequentialList.java
Normal file
15
classpath/java/util/AbstractSequentialList.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
/* Copyright (c) 2009, 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. */
|
||||||
|
|
||||||
|
package java.util;
|
||||||
|
|
||||||
|
public abstract class AbstractSequentialList<T> extends AbstractList<T>
|
||||||
|
implements List<T>
|
||||||
|
{ }
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
package java.util;
|
package java.util;
|
||||||
|
|
||||||
public class ArrayList<T> implements List<T> {
|
public class ArrayList<T> extends AbstractList<T> {
|
||||||
private static final int MinimumCapacity = 16;
|
private static final int MinimumCapacity = 16;
|
||||||
|
|
||||||
private Object[] array;
|
private Object[] array;
|
||||||
@ -169,7 +169,7 @@ public class ArrayList<T> implements List<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Iterator<T> iterator() {
|
public Iterator<T> iterator() {
|
||||||
return new Collections.ArrayListIterator(this);
|
return listIterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ListIterator<T> listIterator(int index) {
|
public ListIterator<T> listIterator(int index) {
|
||||||
|
@ -102,7 +102,15 @@ public class Arrays {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Iterator<T> iterator() {
|
public Iterator<T> iterator() {
|
||||||
return new Collections.ArrayListIterator(this);
|
return listIterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListIterator<T> listIterator(int index) {
|
||||||
|
return new Collections.ArrayListIterator(this, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListIterator<T> listIterator() {
|
||||||
|
return listIterator(0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -198,7 +198,6 @@ public class Collections {
|
|||||||
return new SynchronizedMap<K, V> (map);
|
return new SynchronizedMap<K, V> (map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static class SynchronizedSet<T>
|
static class SynchronizedSet<T>
|
||||||
extends SynchronizedCollection<T>
|
extends SynchronizedCollection<T>
|
||||||
implements Set<T>
|
implements Set<T>
|
||||||
@ -232,7 +231,7 @@ public class Collections {
|
|||||||
|
|
||||||
static class ArrayListIterator<T> implements ListIterator<T> {
|
static class ArrayListIterator<T> implements ListIterator<T> {
|
||||||
private final List<T> list;
|
private final List<T> list;
|
||||||
private boolean canRemove = false;
|
private int toRemove = -1;
|
||||||
private int index;
|
private int index;
|
||||||
|
|
||||||
public ArrayListIterator(List<T> list) {
|
public ArrayListIterator(List<T> list) {
|
||||||
@ -250,7 +249,7 @@ public class Collections {
|
|||||||
|
|
||||||
public T previous() {
|
public T previous() {
|
||||||
if (hasPrevious()) {
|
if (hasPrevious()) {
|
||||||
canRemove = true;
|
toRemove = index;
|
||||||
return list.get(index--);
|
return list.get(index--);
|
||||||
} else {
|
} else {
|
||||||
throw new NoSuchElementException();
|
throw new NoSuchElementException();
|
||||||
@ -259,8 +258,8 @@ public class Collections {
|
|||||||
|
|
||||||
public T next() {
|
public T next() {
|
||||||
if (hasNext()) {
|
if (hasNext()) {
|
||||||
canRemove = true;
|
toRemove = ++index;
|
||||||
return list.get(++index);
|
return list.get(index);
|
||||||
} else {
|
} else {
|
||||||
throw new NoSuchElementException();
|
throw new NoSuchElementException();
|
||||||
}
|
}
|
||||||
@ -271,9 +270,10 @@ public class Collections {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void remove() {
|
public void remove() {
|
||||||
if (canRemove) {
|
if (toRemove != -1) {
|
||||||
canRemove = false;
|
list.remove(toRemove);
|
||||||
list.remove(index--);
|
index = toRemove - 1;
|
||||||
|
toRemove = -1;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
package java.util;
|
package java.util;
|
||||||
|
|
||||||
public class LinkedList<T> implements List<T> {
|
public class LinkedList<T> extends AbstractSequentialList<T> {
|
||||||
private Cell<T> front;
|
private Cell<T> front;
|
||||||
private Cell<T> rear;
|
private Cell<T> rear;
|
||||||
private int size;
|
private int size;
|
||||||
@ -198,7 +198,19 @@ public class LinkedList<T> implements List<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Iterator<T> iterator() {
|
public Iterator<T> iterator() {
|
||||||
return new MyIterator(front);
|
return listIterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListIterator<T> listIterator(int index) {
|
||||||
|
MyIterator it = new MyIterator();
|
||||||
|
for (int i = 0; i < index; ++i) {
|
||||||
|
it.next();
|
||||||
|
}
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListIterator<T> listIterator() {
|
||||||
|
return listIterator(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
@ -217,18 +229,29 @@ public class LinkedList<T> implements List<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MyIterator implements Iterator<T> {
|
private class MyIterator implements ListIterator<T> {
|
||||||
|
private Cell<T> toRemove;
|
||||||
private Cell<T> current;
|
private Cell<T> current;
|
||||||
private Cell<T> next;
|
|
||||||
|
|
||||||
public MyIterator(Cell<T> start) {
|
public T previous() {
|
||||||
next = start;
|
if (hasPrevious()) {
|
||||||
|
T v = current.value;
|
||||||
|
toRemove = current;
|
||||||
|
current = current.prev;
|
||||||
|
return v;
|
||||||
|
} else {
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public T next() {
|
public T next() {
|
||||||
if (hasNext()) {
|
if (hasNext()) {
|
||||||
current = next;
|
if (current == null) {
|
||||||
next = next.next;
|
current = front;
|
||||||
|
} else {
|
||||||
|
current = current.next;
|
||||||
|
}
|
||||||
|
toRemove = current;
|
||||||
return current.value;
|
return current.value;
|
||||||
} else {
|
} else {
|
||||||
throw new NoSuchElementException();
|
throw new NoSuchElementException();
|
||||||
@ -236,13 +259,22 @@ public class LinkedList<T> implements List<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return next != null;
|
if (current == null) {
|
||||||
|
return front != null;
|
||||||
|
} else {
|
||||||
|
return current.next != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasPrevious() {
|
||||||
|
return current != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove() {
|
public void remove() {
|
||||||
if (current != null) {
|
if (toRemove != null) {
|
||||||
LinkedList.this.remove(current);
|
current = toRemove.prev;
|
||||||
current = null;
|
LinkedList.this.remove(toRemove);
|
||||||
|
toRemove = null;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
|
@ -22,4 +22,8 @@ public interface List<T> extends Collection<T> {
|
|||||||
public void add(int index, T element);
|
public void add(int index, T element);
|
||||||
|
|
||||||
public boolean isEmpty();
|
public boolean isEmpty();
|
||||||
|
|
||||||
|
public ListIterator<T> listIterator(int index);
|
||||||
|
|
||||||
|
public ListIterator<T> listIterator();
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
package java.util;
|
package java.util;
|
||||||
|
|
||||||
public class Vector<T> implements List<T> {
|
public class Vector<T> extends AbstractList<T> {
|
||||||
private final ArrayList<T> list;
|
private final ArrayList<T> list;
|
||||||
|
|
||||||
public Vector(int capacity) {
|
public Vector(int capacity) {
|
||||||
@ -112,7 +112,15 @@ public class Vector<T> implements List<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Iterator<T> iterator() {
|
public Iterator<T> iterator() {
|
||||||
return new Collections.ArrayListIterator(this);
|
return listIterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListIterator<T> listIterator(int index) {
|
||||||
|
return new Collections.ArrayListIterator(this, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListIterator<T> listIterator() {
|
||||||
|
return listIterator(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Enumeration<T> elements() {
|
public Enumeration<T> elements() {
|
||||||
|
116
test/List.java
116
test/List.java
@ -1,4 +1,6 @@
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.ListIterator;
|
||||||
|
|
||||||
public class List {
|
public class List {
|
||||||
private static void expect(boolean v) {
|
private static void expect(boolean v) {
|
||||||
@ -21,6 +23,114 @@ public class List {
|
|||||||
expect(s1.equals(s2));
|
expect(s1.equals(s2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void testIterators(java.util.List<Integer> l) {
|
||||||
|
l.add(1);
|
||||||
|
l.add(2);
|
||||||
|
l.add(3);
|
||||||
|
|
||||||
|
ListIterator<Integer> it = l.listIterator();
|
||||||
|
expect(it.next().equals(Integer.valueOf(1)));
|
||||||
|
expect(it.next().equals(Integer.valueOf(2)));
|
||||||
|
expect(it.next().equals(Integer.valueOf(3)));
|
||||||
|
expect(! it.hasNext());
|
||||||
|
|
||||||
|
it = l.listIterator(1);
|
||||||
|
expect(it.next().equals(Integer.valueOf(2)));
|
||||||
|
expect(it.next().equals(Integer.valueOf(3)));
|
||||||
|
expect(! it.hasNext());
|
||||||
|
|
||||||
|
it = l.listIterator(2);
|
||||||
|
expect(it.next().equals(Integer.valueOf(3)));
|
||||||
|
expect(! it.hasNext());
|
||||||
|
|
||||||
|
it = l.listIterator(3);
|
||||||
|
expect(it.previous().equals(Integer.valueOf(3)));
|
||||||
|
expect(it.previous().equals(Integer.valueOf(2)));
|
||||||
|
expect(it.previous().equals(Integer.valueOf(1)));
|
||||||
|
expect(! it.hasPrevious());
|
||||||
|
|
||||||
|
it = l.listIterator(2);
|
||||||
|
expect(it.previous().equals(Integer.valueOf(2)));
|
||||||
|
expect(it.previous().equals(Integer.valueOf(1)));
|
||||||
|
expect(! it.hasPrevious());
|
||||||
|
|
||||||
|
it = l.listIterator(1);
|
||||||
|
expect(it.previous().equals(Integer.valueOf(1)));
|
||||||
|
expect(! it.hasPrevious());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void testIterators2(java.util.List<Integer> l) {
|
||||||
|
l.add(1);
|
||||||
|
l.add(2);
|
||||||
|
l.add(3);
|
||||||
|
|
||||||
|
ListIterator<Integer> it = l.listIterator();
|
||||||
|
expect(it.next().equals(Integer.valueOf(1)));
|
||||||
|
it.remove();
|
||||||
|
expect(it.next().equals(Integer.valueOf(2)));
|
||||||
|
it.remove();
|
||||||
|
expect(it.next().equals(Integer.valueOf(3)));
|
||||||
|
it.remove();
|
||||||
|
expect(! it.hasNext());
|
||||||
|
expect(l.isEmpty());
|
||||||
|
|
||||||
|
l.add(1);
|
||||||
|
l.add(2);
|
||||||
|
l.add(3);
|
||||||
|
|
||||||
|
it = l.listIterator(1);
|
||||||
|
expect(it.next().equals(Integer.valueOf(2)));
|
||||||
|
it.remove();
|
||||||
|
expect(it.next().equals(Integer.valueOf(3)));
|
||||||
|
it.remove();
|
||||||
|
expect(! it.hasNext());
|
||||||
|
expect(l.size() == 1);
|
||||||
|
|
||||||
|
l.add(2);
|
||||||
|
l.add(3);
|
||||||
|
|
||||||
|
it = l.listIterator(2);
|
||||||
|
expect(it.next().equals(Integer.valueOf(3)));
|
||||||
|
it.remove();
|
||||||
|
expect(! it.hasNext());
|
||||||
|
expect(l.size() == 2);
|
||||||
|
|
||||||
|
l.add(3);
|
||||||
|
|
||||||
|
it = l.listIterator(3);
|
||||||
|
expect(it.previous().equals(Integer.valueOf(3)));
|
||||||
|
it.remove();
|
||||||
|
expect(it.previous().equals(Integer.valueOf(2)));
|
||||||
|
it.remove();
|
||||||
|
expect(it.previous().equals(Integer.valueOf(1)));
|
||||||
|
it.remove();
|
||||||
|
expect(! it.hasPrevious());
|
||||||
|
expect(l.isEmpty());
|
||||||
|
|
||||||
|
l.add(1);
|
||||||
|
l.add(2);
|
||||||
|
l.add(3);
|
||||||
|
|
||||||
|
it = l.listIterator(2);
|
||||||
|
expect(it.previous().equals(Integer.valueOf(2)));
|
||||||
|
it.remove();
|
||||||
|
expect(it.previous().equals(Integer.valueOf(1)));
|
||||||
|
it.remove();
|
||||||
|
expect(! it.hasPrevious());
|
||||||
|
expect(l.size() == 1);
|
||||||
|
|
||||||
|
l.clear();
|
||||||
|
l.add(1);
|
||||||
|
l.add(2);
|
||||||
|
l.add(3);
|
||||||
|
|
||||||
|
it = l.listIterator(1);
|
||||||
|
expect(it.previous().equals(Integer.valueOf(1)));
|
||||||
|
it.remove();
|
||||||
|
expect(! it.hasPrevious());
|
||||||
|
expect(l.size() == 2);
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String args[]) {
|
public static void main(String args[]) {
|
||||||
ArrayList<Integer> l = new ArrayList<Integer>();
|
ArrayList<Integer> l = new ArrayList<Integer>();
|
||||||
l.add(1); l.add(2); l.add(3); l.add(4); l.add(5);
|
l.add(1); l.add(2); l.add(3); l.add(4); l.add(5);
|
||||||
@ -39,5 +149,11 @@ public class List {
|
|||||||
for (int i=0; i < z.length; i++) {
|
for (int i=0; i < z.length; i++) {
|
||||||
System.out.println(z[i]);
|
System.out.println(z[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testIterators(new ArrayList());
|
||||||
|
testIterators(new LinkedList());
|
||||||
|
|
||||||
|
testIterators2(new ArrayList());
|
||||||
|
testIterators2(new LinkedList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user