--- jsr166/src/jsr166e/extra/ReadMostlyVector.java 2011/07/16 14:56:30 1.3
+++ jsr166/src/jsr166e/extra/ReadMostlyVector.java 2011/12/31 21:16:14 1.22
@@ -19,7 +19,10 @@ import java.util.*;
* best-effort in the presence of concurrent modifications, and do
* NOT throw {@link ConcurrentModificationException}. An
* iterator's {@code next()} method returns consecutive elements as
- * they appear in the underlying array upon each access.
+ * they appear in the underlying array upon each access. Alternatively,
+ * method {@link #snapshotIterator} may be used for deterministic
+ * traversals, at the expense of making a copy, and unavailability of
+ * method {@code Iterator.remove}.
*
*
Otherwise, this class supports all methods, under the same
* documented specifications, as {@code Vector}. Consult {@link
@@ -29,7 +32,8 @@ import java.util.*;
*
* @author Doug Lea
*/
-public class ReadMostlyVector implements List, RandomAccess, Cloneable, java.io.Serializable {
+public class ReadMostlyVector
+ implements List, RandomAccess, Cloneable, java.io.Serializable {
private static final long serialVersionUID = 8673264195747942595L;
/*
@@ -45,6 +49,11 @@ public class ReadMostlyVector impleme
* read-only mode, and then lock. When in read-only mode, they
* validate only at the end of an array scan unless the element is
* actually used (for example, as an argument of method equals).
+ *
+ * We rely on some invariants that are always true, even for field
+ * reads in read-only mode that have not yet been validated:
+ * - array != null
+ * - count >= 0
*/
/**
@@ -54,9 +63,9 @@ public class ReadMostlyVector impleme
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
// fields are non-private to simpify nested class access
- Object[] array;
+ volatile Object[] array;
final SequenceLock lock;
- int count;
+ volatile int count;
final int capacityIncrement;
/**
@@ -126,7 +135,7 @@ public class ReadMostlyVector impleme
}
// For explanation, see CopyOnWriteArrayList
- final void grow(int minCapacity) {
+ final Object[] grow(int minCapacity) {
int oldCapacity = array.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
@@ -134,7 +143,7 @@ public class ReadMostlyVector impleme
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
- array = Arrays.copyOf(array, newCapacity);
+ return array = Arrays.copyOf(array, newCapacity);
}
static int hugeCapacity(int minCapacity) {
@@ -151,13 +160,14 @@ public class ReadMostlyVector impleme
* as well as sublist and iterator classes.
*/
+ // Version of indexOf that returns -1 if either not present or invalid
final int validatedIndexOf(Object x, Object[] items, int index, int fence,
long seq) {
for (int i = index; i < fence; ++i) {
Object e = items[i];
if (lock.getSequence() != seq)
break;
- if ((x == null) ? e == null : (e != null && x.equals(e)))
+ if ((x == null) ? e == null : x.equals(e))
return i;
}
return -1;
@@ -167,7 +177,7 @@ public class ReadMostlyVector impleme
Object[] items = array;
for (int i = index; i < fence; ++i) {
Object e = items[i];
- if ((x == null) ? e == null : (e != null && x.equals(e)))
+ if ((x == null) ? e == null : x.equals(e))
return i;
}
return -1;
@@ -179,7 +189,7 @@ public class ReadMostlyVector impleme
Object e = items[i];
if (lock.getSequence() != seq)
break;
- if ((x == null) ? e == null : (e != null && x.equals(e)))
+ if ((x == null) ? e == null : x.equals(e))
return i;
}
return -1;
@@ -189,58 +199,62 @@ public class ReadMostlyVector impleme
Object[] items = array;
for (int i = index; i >= origin; --i) {
Object e = items[i];
- if ((x == null) ? e == null : (e != null && x.equals(e)))
+ if ((x == null) ? e == null : x.equals(e))
return i;
}
return -1;
}
- final void internalAdd(Object e) {
+ final void rawAdd(E e) {
int n = count;
- if (n >= array.length)
- grow(n + 1);
- array[n] = e;
+ Object[] items = array;
+ if (n >= items.length)
+ items = grow(n + 1);
+ items[n] = e;
count = n + 1;
}
- final void internalAddAt(int index, Object e) {
+ final void rawAddAt(int index, E e) {
int n = count;
+ Object[] items = array;
if (index > n)
throw new ArrayIndexOutOfBoundsException(index);
- if (n >= array.length)
- grow(n + 1);
+ if (n >= items.length)
+ items = grow(n + 1);
if (index < n)
- System.arraycopy(array, index, array, index + 1, n - index);
- array[index] = e;
+ System.arraycopy(items, index, items, index + 1, n - index);
+ items[index] = e;
count = n + 1;
}
- final boolean internalAddAllAt(int index, Object[] elements) {
+ final boolean rawAddAllAt(int index, Object[] elements) {
int n = count;
+ Object[] items = array;
if (index < 0 || index > n)
throw new ArrayIndexOutOfBoundsException(index);
int len = elements.length;
if (len == 0)
return false;
int newCount = n + len;
- if (newCount >= array.length)
- grow(newCount);
- int mv = count - index;
+ if (newCount >= items.length)
+ items = grow(newCount);
+ int mv = n - index;
if (mv > 0)
- System.arraycopy(array, index, array, index + len, mv);
- System.arraycopy(elements, 0, array, index, len);
+ System.arraycopy(items, index, items, index + len, mv);
+ System.arraycopy(elements, 0, items, index, len);
count = newCount;
return true;
}
- final boolean internalRemoveAt(int index) {
+ final boolean rawRemoveAt(int index) {
int n = count - 1;
+ Object[] items = array;
if (index < 0 || index > n)
return false;
int mv = n - index;
if (mv > 0)
- System.arraycopy(array, index + 1, array, index, mv);
- array[n] = null;
+ System.arraycopy(items, index + 1, items, index, mv);
+ items[n] = null;
count = n;
return true;
}
@@ -253,7 +267,7 @@ public class ReadMostlyVector impleme
* field under lock.
*/
final boolean internalRemoveAll(Collection> c, int origin, int bound) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
boolean removed = false;
lock.lock();
try {
@@ -261,7 +275,7 @@ public class ReadMostlyVector impleme
int fence = bound < 0 || bound > n ? n : bound;
if (origin >= 0 && origin < fence) {
for (Object x : c) {
- while (internalRemoveAt(rawIndexOf(x, origin, fence)))
+ while (rawRemoveAt(rawIndexOf(x, origin, fence)))
removed = true;
}
}
@@ -272,25 +286,29 @@ public class ReadMostlyVector impleme
}
final boolean internalRetainAll(Collection> c, int origin, int bound) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
boolean removed = false;
if (c != this) {
lock.lock();
try {
+ Object[] items = array;
int i = origin;
int n = count;
int fence = bound < 0 || bound > n ? n : bound;
- while (i < fence) {
- if (c.contains(array[i]))
+ while (i >= 0 && i < fence) {
+ if (c.contains(items[i]))
++i;
else {
--fence;
- int mv = --count - i;
+ int mv = --n - i;
if (mv > 0)
- System.arraycopy(array, i + 1, array, i, mv);
- removed = true;
+ System.arraycopy(items, i + 1, items, i, mv);
}
}
+ if (count != n) {
+ count = n;
+ removed = true;
+ }
} finally {
lock.unlock();
}
@@ -302,27 +320,28 @@ public class ReadMostlyVector impleme
int n = count;
int fence = bound < 0 || bound > n ? n : bound;
if (origin >= 0 && origin < fence) {
+ Object[] items = array;
int removed = fence - origin;
int newCount = n - removed;
int mv = n - (origin + removed);
if (mv > 0)
- System.arraycopy(array, origin + removed, array, origin, mv);
+ System.arraycopy(items, origin + removed, items, origin, mv);
for (int i = n; i < newCount; ++i)
- array[i] = null;
+ items[i] = null;
count = newCount;
}
}
final boolean internalContainsAll(Collection> c, int origin, int bound) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
boolean contained;
boolean locked = false;
try {
for (;;) {
long seq = lock.awaitAvailability();
+ int n = count;
Object[] items = array;
int len = items.length;
- int n = count;
if (n > len)
continue;
int fence = bound < 0 || bound > n ? n : bound;
@@ -331,7 +350,11 @@ public class ReadMostlyVector impleme
else {
contained = true;
for (Object e : c) {
- if (validatedIndexOf(e, items, origin, fence, seq) < 0) {
+ int idx = (locked ?
+ rawIndexOf(e, origin, fence) :
+ validatedIndexOf(e, items, origin,
+ fence, seq));
+ if (idx < 0) {
contained = false;
break;
}
@@ -350,33 +373,27 @@ public class ReadMostlyVector impleme
}
final boolean internalEquals(List> list, int origin, int bound) {
- SequenceLock lock = this.lock;
- boolean equal;
+ final SequenceLock lock = this.lock;
boolean locked = false;
+ boolean equal;
try {
- outer:for (;;) {
- equal = true;
+ for (;;) {
long seq = lock.awaitAvailability();
Object[] items = array;
- int len = items.length;
int n = count;
- if (n > len)
- continue;
- int fence = bound < 0 || bound > n ? n : bound;
- if (origin < 0)
+ if (n > items.length || origin < 0)
equal = false;
else {
+ equal = true;
+ int fence = bound < 0 || bound > n ? n : bound;
Iterator> it = list.iterator();
for (int i = origin; i < fence; ++i) {
- if (!it.hasNext()) {
- equal = false;
- break;
- }
- Object x = it.next();
- Object y = items[i];
- if (lock.getSequence() != seq)
- continue outer;
- if ((x == null) ? y != null : (y == null || !x.equals(y))) {
+ Object x = items[i];
+ Object y;
+ if ((!locked && lock.getSequence() != seq) ||
+ !it.hasNext() ||
+ (y = it.next()) == null ?
+ x != null : !y.equals(x)) {
equal = false;
break;
}
@@ -397,7 +414,7 @@ public class ReadMostlyVector impleme
}
final int internalHashCode(int origin, int bound) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
int hash;
boolean locked = false;
try {
@@ -405,8 +422,8 @@ public class ReadMostlyVector impleme
hash = 1;
long seq = lock.awaitAvailability();
Object[] items = array;
- int len = items.length;
int n = count;
+ int len = items.length;
if (n > len)
continue;
int fence = bound < 0 || bound > n ? n : bound;
@@ -429,15 +446,15 @@ public class ReadMostlyVector impleme
}
final String internalToString(int origin, int bound) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
String ret;
boolean locked = false;
try {
outer:for (;;) {
long seq = lock.awaitAvailability();
Object[] items = array;
- int len = items.length;
int n = count;
+ int len = items.length;
if (n > len)
continue;
int fence = bound < 0 || bound > n ? n : bound;
@@ -450,7 +467,7 @@ public class ReadMostlyVector impleme
Object e = items[i];
if (e == this)
sb.append("(this Collection)");
- else if (lock.getSequence() != seq)
+ else if (!locked && lock.getSequence() != seq)
continue outer;
else
sb.append(e.toString());
@@ -476,15 +493,15 @@ public class ReadMostlyVector impleme
final Object[] internalToArray(int origin, int bound) {
Object[] result;
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
boolean locked = false;
try {
for (;;) {
result = null;
long seq = lock.awaitAvailability();
Object[] items = array;
- int len = items.length;
int n = count;
+ int len = items.length;
if (n > len)
continue;
int fence = bound < 0 || bound > n ? n : bound;
@@ -503,32 +520,33 @@ public class ReadMostlyVector impleme
return result;
}
+ @SuppressWarnings("unchecked")
final T[] internalToArray(T[] a, int origin, int bound) {
int alen = a.length;
T[] result;
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
boolean locked = false;
try {
for (;;) {
long seq = lock.awaitAvailability();
Object[] items = array;
- int len = items.length;
int n = count;
+ int len = items.length;
if (n > len)
continue;
int fence = bound < 0 || bound > n ? n : bound;
int rlen = fence - origin;
+ if (rlen < 0)
+ rlen = 0;
if (origin < 0 || alen >= rlen) {
- if (rlen < 0)
- rlen = 0;
- else if (rlen > 0)
- System.arraycopy(array, 0, a, origin, rlen);
+ if (rlen > 0)
+ System.arraycopy(items, 0, a, origin, rlen);
if (alen > rlen)
a[rlen] = null;
result = a;
}
else
- result = (T[]) Arrays.copyOfRange(array, origin,
+ result = (T[]) Arrays.copyOfRange(items, origin,
fence, a.getClass());
if (lock.getSequence() == seq)
break;
@@ -545,10 +563,10 @@ public class ReadMostlyVector impleme
// public List methods
public boolean add(E e) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
- internalAdd(e);
+ rawAdd(e);
} finally {
lock.unlock();
}
@@ -556,10 +574,10 @@ public class ReadMostlyVector impleme
}
public void add(int index, E element) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
- internalAddAt(index, element);
+ rawAddAt(index, element);
} finally {
lock.unlock();
}
@@ -570,13 +588,15 @@ public class ReadMostlyVector impleme
int len = elements.length;
if (len == 0)
return false;
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
- int newCount = count + len;
- if (newCount >= array.length)
- grow(newCount);
- System.arraycopy(elements, 0, array, count, len);
+ Object[] items = array;
+ int n = count;
+ int newCount = n + len;
+ if (newCount >= items.length)
+ items = grow(newCount);
+ System.arraycopy(elements, 0, items, n, len);
count = newCount;
} finally {
lock.unlock();
@@ -585,12 +605,12 @@ public class ReadMostlyVector impleme
}
public boolean addAll(int index, Collection extends E> c) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
boolean ret;
Object[] elements = c.toArray();
lock.lock();
try {
- ret = internalAddAllAt(index, elements);
+ ret = rawAddAllAt(index, elements);
} finally {
lock.unlock();
}
@@ -598,11 +618,13 @@ public class ReadMostlyVector impleme
}
public void clear() {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
- for (int i = 0; i < count; i++)
- array[i] = null;
+ int n = count;
+ Object[] items = array;
+ for (int i = 0; i < n; i++)
+ items[i] = null;
count = 0;
} finally {
lock.unlock();
@@ -622,31 +644,21 @@ public class ReadMostlyVector impleme
return true;
if (!(o instanceof List))
return false;
- return internalEquals((List>)(o), 0, -1);
+ return internalEquals((List>)o, 0, -1);
}
public E get(int index) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
for (;;) {
long seq = lock.awaitAvailability();
- Object[] items = array;
int n = count;
- if (n > items.length)
- continue;
- Object e; boolean ex;
- if (index < 0 || index >= n) {
- e = null;
- ex = true;
- }
- else {
- e = items[index];
- ex = false;
- }
+ Object[] items = array;
+ @SuppressWarnings("unchecked")
+ E e = (index < items.length) ? (E) items[index] : null;
if (lock.getSequence() == seq) {
- if (ex)
+ if (index >= n)
throw new ArrayIndexOutOfBoundsException(index);
- else
- return (E)e;
+ return e;
}
}
}
@@ -656,83 +668,73 @@ public class ReadMostlyVector impleme
}
public int indexOf(Object o) {
- SequenceLock lock = this.lock;
- long seq = lock.awaitAvailability();
- Object[] items = array;
- int n = count;
- if (n <= items.length) {
- int idx = validatedIndexOf(o, items, 0, n, seq);
- if (lock.getSequence() == seq)
- return idx;
- }
- lock.lock();
- try {
- return rawIndexOf(o, 0, count);
- } finally {
- lock.unlock();
- }
+ return indexOf(o, 0);
}
public boolean isEmpty() {
- long ignore = lock.getSequence();
return count == 0;
}
public Iterator iterator() {
- return new Itr(this, 0);
+ return new Itr(this, 0);
}
public int lastIndexOf(Object o) {
- SequenceLock lock = this.lock;
- long seq = lock.awaitAvailability();
- Object[] items = array;
- int n = count;
- if (n <= items.length) {
- int idx = validatedLastIndexOf(o, items, n - 1, 0, seq);
- if (lock.getSequence() == seq)
- return idx;
- }
- lock.lock();
- try {
- return rawLastIndexOf(o, count - 1, 0);
- } finally {
- lock.unlock();
+ final SequenceLock lock = this.lock;
+ for (;;) {
+ long seq = lock.awaitAvailability();
+ Object[] items = array;
+ int n = count;
+ if (n <= items.length) {
+ for (int i = n - 1; i >= 0; --i) {
+ Object e = items[i];
+ if (lock.getSequence() != seq) {
+ lock.lock();
+ try {
+ return rawLastIndexOf(o, count - 1, 0);
+ } finally {
+ lock.unlock();
+ }
+ }
+ else if ((o == null) ? e == null : o.equals(e))
+ return i;
+ }
+ return -1;
+ }
}
}
public ListIterator listIterator() {
- return new Itr(this, 0);
+ return new Itr(this, 0);
}
public ListIterator listIterator(int index) {
- return new Itr(this, index);
+ return new Itr(this, index);
}
public E remove(int index) {
- SequenceLock lock = this.lock;
- Object oldValue;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
if (index < 0 || index >= count)
throw new ArrayIndexOutOfBoundsException(index);
- oldValue = array[index];
- internalRemoveAt(index);
+ @SuppressWarnings("unchecked")
+ E oldValue = (E) array[index];
+ rawRemoveAt(index);
+ return oldValue;
} finally {
lock.unlock();
}
- return (E)oldValue;
}
public boolean remove(Object o) {
- SequenceLock lock = this.lock;
- boolean removed;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
- removed = internalRemoveAt(rawIndexOf(o, 0, count));
+ return rawRemoveAt(rawIndexOf(o, 0, count));
} finally {
lock.unlock();
}
- return removed;
}
public boolean removeAll(Collection> c) {
@@ -744,22 +746,22 @@ public class ReadMostlyVector impleme
}
public E set(int index, E element) {
- Object oldValue;
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
+ Object[] items = array;
if (index < 0 || index >= count)
throw new ArrayIndexOutOfBoundsException(index);
- oldValue = array[index];
- array[index] = element;
+ @SuppressWarnings("unchecked")
+ E oldValue = (E) items[index];
+ items[index] = element;
+ return oldValue;
} finally {
lock.unlock();
}
- return (E)oldValue;
}
public int size() {
- long ignore = lock.getSequence();
return count;
}
@@ -768,7 +770,7 @@ public class ReadMostlyVector impleme
int ssize = toIndex - fromIndex;
if (fromIndex < 0 || toIndex > c || ssize < 0)
throw new IndexOutOfBoundsException();
- return new ReadMostlyVectorSublist(this, fromIndex, ssize);
+ return new ReadMostlyVectorSublist(this, fromIndex, ssize);
}
public Object[] toArray() {
@@ -789,23 +791,21 @@ public class ReadMostlyVector impleme
* Append the element if not present.
*
* @param e element to be added to this list, if absent
- * @return true if the element was added
+ * @return {@code true} if the element was added
*/
public boolean addIfAbsent(E e) {
- boolean added;
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
if (rawIndexOf(e, 0, count) < 0) {
- internalAdd(e);
- added = true;
+ rawAdd(e);
+ return true;
}
else
- added = false;
+ return false;
} finally {
lock.unlock();
}
- return added;
}
/**
@@ -827,9 +827,10 @@ public class ReadMostlyVector impleme
lock.lock();
try {
for (int i = 0; i < clen; ++i) {
- Object e = cs[i];
+ @SuppressWarnings("unchecked")
+ E e = (E) cs[i];
if (rawIndexOf(e, 0, count) < 0) {
- internalAdd(e);
+ rawAdd(e);
++added;
}
}
@@ -840,67 +841,71 @@ public class ReadMostlyVector impleme
return added;
}
+ /**
+ * Returns an iterator operating over a snapshot copy of the
+ * elements of this collection created upon construction of the
+ * iterator. The iterator does NOT support the
+ * {@code remove} method.
+ *
+ * @return an iterator over the elements in this list in proper sequence
+ */
+ public Iterator snapshotIterator() {
+ return new SnapshotIterator(this);
+ }
+
+ static final class SnapshotIterator implements Iterator {
+ private final Object[] items;
+ private int cursor;
+ SnapshotIterator(ReadMostlyVector v) { items = v.toArray(); }
+ public boolean hasNext() { return cursor < items.length; }
+ @SuppressWarnings("unchecked")
+ public E next() {
+ if (cursor < items.length)
+ return (E) items[cursor++];
+ throw new NoSuchElementException();
+ }
+ public void remove() { throw new UnsupportedOperationException() ; }
+ }
+
// Vector-only methods
/** See {@link Vector#firstElement} */
public E firstElement() {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
for (;;) {
long seq = lock.awaitAvailability();
Object[] items = array;
- int len = items.length;
int n = count;
- if (n > len)
- continue;
- Object e; boolean ex;
- if (n > 0) {
- e = items[0];
- ex = false;
- }
- else {
- e = null;
- ex = true;
- }
+ @SuppressWarnings("unchecked")
+ E e = (items.length > 0) ? (E) items[0] : null;
if (lock.getSequence() == seq) {
- if (ex)
+ if (n <= 0)
throw new NoSuchElementException();
- else
- return (E)e;
+ return e;
}
}
}
/** See {@link Vector#lastElement} */
public E lastElement() {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
for (;;) {
long seq = lock.awaitAvailability();
Object[] items = array;
- int len = items.length;
int n = count;
- if (n > len)
- continue;
- Object e; boolean ex;
- if (n > 0) {
- e = items[n - 1];
- ex = false;
- }
- else {
- e = null;
- ex = true;
- }
+ @SuppressWarnings("unchecked")
+ E e = (n > 0 && items.length >= n) ? (E) items[n - 1] : null;
if (lock.getSequence() == seq) {
- if (ex)
+ if (n <= 0)
throw new NoSuchElementException();
- else
- return (E)e;
+ return e;
}
}
}
/** See {@link Vector#indexOf(Object, int)} */
public int indexOf(Object o, int index) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
int idx = 0;
boolean ex = false;
long seq = lock.awaitAvailability();
@@ -919,7 +924,7 @@ public class ReadMostlyVector impleme
if (index < 0)
ex = true;
else
- idx = rawIndexOf(o, 0, count);
+ idx = rawIndexOf(o, index, count);
} finally {
lock.unlock();
}
@@ -931,7 +936,7 @@ public class ReadMostlyVector impleme
/** See {@link Vector#lastIndexOf(Object, int)} */
public int lastIndexOf(Object o, int index) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
int idx = 0;
boolean ex = false;
long seq = lock.awaitAvailability();
@@ -964,15 +969,16 @@ public class ReadMostlyVector impleme
public void setSize(int newSize) {
if (newSize < 0)
throw new ArrayIndexOutOfBoundsException(newSize);
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
int n = count;
if (newSize > n)
grow(newSize);
else {
+ Object[] items = array;
for (int i = newSize ; i < n ; i++)
- array[i] = null;
+ items[i] = null;
}
count = newSize;
} finally {
@@ -982,7 +988,7 @@ public class ReadMostlyVector impleme
/** See {@link Vector#copyInto} */
public void copyInto(Object[] anArray) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
System.arraycopy(array, 0, anArray, 0, count);
@@ -993,11 +999,13 @@ public class ReadMostlyVector impleme
/** See {@link Vector#trimToSize} */
public void trimToSize() {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
- if (count < array.length)
- array = Arrays.copyOf(array, count);
+ Object[] items = array;
+ int n = count;
+ if (n < items.length)
+ array = Arrays.copyOf(items, n);
} finally {
lock.unlock();
}
@@ -1006,7 +1014,7 @@ public class ReadMostlyVector impleme
/** See {@link Vector#ensureCapacity} */
public void ensureCapacity(int minCapacity) {
if (minCapacity > 0) {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
if (minCapacity - array.length > 0)
@@ -1019,12 +1027,11 @@ public class ReadMostlyVector impleme
/** See {@link Vector#elements} */
public Enumeration elements() {
- return new Itr(this, 0);
+ return new Itr(this, 0);
}
/** See {@link Vector#capacity} */
public int capacity() {
- long ignore = lock.getSequence();
return array.length;
}
@@ -1065,8 +1072,8 @@ public class ReadMostlyVector impleme
// other methods
- public Object clone() {
- SequenceLock lock = this.lock;
+ public ReadMostlyVector clone() {
+ final SequenceLock lock = this.lock;
Object[] a = null;
boolean retry = false;
long seq = lock.awaitAvailability();
@@ -1085,12 +1092,12 @@ public class ReadMostlyVector impleme
lock.unlock();
}
}
- return new ReadMostlyVector(a, n, capacityIncrement);
+ return new ReadMostlyVector(a, n, capacityIncrement);
}
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
- SequenceLock lock = this.lock;
+ final SequenceLock lock = this.lock;
lock.lock();
try {
s.defaultWriteObject();
@@ -1099,11 +1106,11 @@ public class ReadMostlyVector impleme
}
}
- static final class Itr implements ListIterator, Enumeration {
+ static final class Itr implements ListIterator, Enumeration {
final ReadMostlyVector list;
final SequenceLock lock;
Object[] items;
- Object next, prev;
+ E next, prev;
long seq;
int cursor;
int fence;
@@ -1129,6 +1136,7 @@ public class ReadMostlyVector impleme
lock.getSequence() != seq);
}
+ @SuppressWarnings("unchecked")
public boolean hasNext() {
boolean valid;
int i = cursor;
@@ -1137,7 +1145,7 @@ public class ReadMostlyVector impleme
valid = false;
break;
}
- next = items[i];
+ next = (E) items[i];
if (lock.getSequence() == seq) {
valid = true;
break;
@@ -1147,6 +1155,7 @@ public class ReadMostlyVector impleme
return validNext = valid;
}
+ @SuppressWarnings("unchecked")
public boolean hasPrevious() {
boolean valid;
int i = cursor - 1;
@@ -1155,7 +1164,7 @@ public class ReadMostlyVector impleme
valid = false;
break;
}
- prev = items[i];
+ prev = (E) items[i];
if (lock.getSequence() == seq) {
valid = true;
break;
@@ -1169,7 +1178,7 @@ public class ReadMostlyVector impleme
if (validNext || hasNext()) {
validNext = false;
lastRet = cursor++;
- return (E) next;
+ return next;
}
throw new NoSuchElementException();
}
@@ -1178,7 +1187,7 @@ public class ReadMostlyVector impleme
if (validPrev || hasPrevious()) {
validPrev = false;
lastRet = cursor--;
- return (E) prev;
+ return prev;
}
throw new NoSuchElementException();
}
@@ -1235,12 +1244,16 @@ public class ReadMostlyVector impleme
public int previousIndex() { return cursor - 1; }
}
- static final class ReadMostlyVectorSublist implements List, RandomAccess, java.io.Serializable {
+ static final class ReadMostlyVectorSublist
+ implements List, RandomAccess, java.io.Serializable {
+ static final long serialVersionUID = 3041673470172026059L;
+
final ReadMostlyVector list;
final int offset;
volatile int size;
- ReadMostlyVectorSublist(ReadMostlyVector list, int offset, int size) {
+ ReadMostlyVectorSublist(ReadMostlyVector list,
+ int offset, int size) {
this.list = list;
this.offset = offset;
this.size = size;
@@ -1252,11 +1265,11 @@ public class ReadMostlyVector impleme
}
public boolean add(E element) {
- SequenceLock lock = list.lock;
+ final SequenceLock lock = list.lock;
lock.lock();
try {
int c = size;
- list.internalAddAt(c + offset, element);
+ list.rawAddAt(c + offset, element);
size = c + 1;
} finally {
lock.unlock();
@@ -1265,12 +1278,12 @@ public class ReadMostlyVector impleme
}
public void add(int index, E element) {
- SequenceLock lock = list.lock;
+ final SequenceLock lock = list.lock;
lock.lock();
try {
if (index < 0 || index > size)
throw new ArrayIndexOutOfBoundsException(index);
- list.internalAddAt(index + offset, element);
+ list.rawAddAt(index + offset, element);
++size;
} finally {
lock.unlock();
@@ -1279,42 +1292,40 @@ public class ReadMostlyVector impleme
public boolean addAll(Collection extends E> c) {
Object[] elements = c.toArray();
- int added;
- SequenceLock lock = list.lock;
+ final SequenceLock lock = list.lock;
lock.lock();
try {
int s = size;
int pc = list.count;
- list.internalAddAllAt(offset + s, elements);
- added = list.count - pc;
+ list.rawAddAllAt(offset + s, elements);
+ int added = list.count - pc;
size = s + added;
+ return added != 0;
} finally {
lock.unlock();
}
- return added != 0;
}
public boolean addAll(int index, Collection extends E> c) {
Object[] elements = c.toArray();
- int added;
- SequenceLock lock = list.lock;
+ final SequenceLock lock = list.lock;
lock.lock();
try {
int s = size;
if (index < 0 || index > s)
throw new ArrayIndexOutOfBoundsException(index);
int pc = list.count;
- list.internalAddAllAt(index + offset, elements);
- added = list.count - pc;
+ list.rawAddAllAt(index + offset, elements);
+ int added = list.count - pc;
size = s + added;
+ return added != 0;
} finally {
lock.unlock();
}
- return added != 0;
}
public void clear() {
- SequenceLock lock = list.lock;
+ final SequenceLock lock = list.lock;
lock.lock();
try {
list.internalClear(offset, offset + size);
@@ -1351,18 +1362,19 @@ public class ReadMostlyVector impleme
}
public int indexOf(Object o) {
- SequenceLock lock = list.lock;
+ final SequenceLock lock = list.lock;
long seq = lock.awaitAvailability();
Object[] items = list.array;
int c = list.count;
if (c <= items.length) {
- int idx = list.validatedIndexOf(o, items, offset, offset+size, seq);
+ int idx = list.validatedIndexOf(o, items, offset,
+ offset + size, seq);
if (lock.getSequence() == seq)
return idx < 0 ? -1 : idx - offset;
}
lock.lock();
try {
- int idx = list.rawIndexOf(o, offset, offset+size);
+ int idx = list.rawIndexOf(o, offset, offset + size);
return idx < 0 ? -1 : idx - offset;
} finally {
lock.unlock();
@@ -1374,11 +1386,11 @@ public class ReadMostlyVector impleme
}
public Iterator iterator() {
- return new SubItr(this, offset);
+ return new SubItr(this, offset);
}
public int lastIndexOf(Object o) {
- SequenceLock lock = list.lock;
+ final SequenceLock lock = list.lock;
long seq = lock.awaitAvailability();
Object[] items = list.array;
int c = list.count;
@@ -1398,44 +1410,45 @@ public class ReadMostlyVector impleme
}
public ListIterator listIterator() {
- return new SubItr(this, offset);
+ return new SubItr(this, offset);
}
public ListIterator listIterator(int index) {
- return new SubItr(this, index + offset);
+ return new SubItr(this, index + offset);
}
public E remove(int index) {
- Object result;
- SequenceLock lock = list.lock;
+ final SequenceLock lock = list.lock;
lock.lock();
try {
- if (index < 0 || index >= size)
- throw new ArrayIndexOutOfBoundsException(index);
+ Object[] items = list.array;
int i = index + offset;
- result = list.array[i];
- list.internalRemoveAt(i);
+ if (index < 0 || index >= size || i >= items.length)
+ throw new ArrayIndexOutOfBoundsException(index);
+ @SuppressWarnings("unchecked")
+ E result = (E) items[i];
+ list.rawRemoveAt(i);
size--;
+ return result;
} finally {
lock.unlock();
}
- return (E)result;
}
public boolean remove(Object o) {
- boolean removed = false;
- SequenceLock lock = list.lock;
+ final SequenceLock lock = list.lock;
lock.lock();
try {
- if (list.internalRemoveAt(list.rawIndexOf(o, offset,
- offset + size))) {
- removed = true;
+ if (list.rawRemoveAt(list.rawIndexOf(o, offset,
+ offset + size))) {
--size;
+ return true;
}
+ else
+ return false;
} finally {
lock.unlock();
}
- return removed;
}
public boolean removeAll(Collection> c) {
@@ -1461,7 +1474,7 @@ public class ReadMostlyVector impleme
int ssize = toIndex - fromIndex;
if (fromIndex < 0 || toIndex > c || ssize < 0)
throw new IndexOutOfBoundsException();
- return new ReadMostlyVectorSublist(list, offset+fromIndex, ssize);
+ return new ReadMostlyVectorSublist(list, offset+fromIndex, ssize);
}
public Object[] toArray() {
@@ -1483,7 +1496,7 @@ public class ReadMostlyVector impleme
final ReadMostlyVector list;
final SequenceLock lock;
Object[] items;
- Object next, prev;
+ E next, prev;
long seq;
int cursor;
int fence;
@@ -1514,6 +1527,7 @@ public class ReadMostlyVector impleme
} while (lock.getSequence() != seq);
}
+ @SuppressWarnings("unchecked")
public boolean hasNext() {
boolean valid;
int i = cursor;
@@ -1522,7 +1536,7 @@ public class ReadMostlyVector impleme
valid = false;
break;
}
- next = items[i];
+ next = (E) items[i];
if (lock.getSequence() == seq) {
valid = true;
break;
@@ -1532,6 +1546,7 @@ public class ReadMostlyVector impleme
return validNext = valid;
}
+ @SuppressWarnings("unchecked")
public boolean hasPrevious() {
boolean valid;
int i = cursor - 1;
@@ -1540,7 +1555,7 @@ public class ReadMostlyVector impleme
valid = false;
break;
}
- prev = items[i];
+ prev = (E) items[i];
if (lock.getSequence() == seq) {
valid = true;
break;
@@ -1554,7 +1569,7 @@ public class ReadMostlyVector impleme
if (validNext || hasNext()) {
validNext = false;
lastRet = cursor++;
- return (E) next;
+ return next;
}
throw new NoSuchElementException();
}
@@ -1563,7 +1578,7 @@ public class ReadMostlyVector impleme
if (validPrev || hasPrevious()) {
validPrev = false;
lastRet = cursor--;
- return (E) prev;
+ return prev;
}
throw new NoSuchElementException();
}