--- jsr166/src/jsr166e/extra/ReadMostlyVector.java 2011/12/31 21:16:14 1.22 +++ jsr166/src/jsr166e/extra/ReadMostlyVector.java 2012/08/13 11:20:39 1.27 @@ -62,7 +62,7 @@ public class ReadMostlyVector */ private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; - // fields are non-private to simpify nested class access + // fields are non-private to simplify nested class access volatile Object[] array; final SequenceLock lock; volatile int count; @@ -160,7 +160,11 @@ public class ReadMostlyVector * as well as sublist and iterator classes. */ - // Version of indexOf that returns -1 if either not present or invalid + /** + * Version of indexOf that returns -1 if either not present or invalid. + * + * @throws ArrayIndexOutOfBoundsException if index is negative + */ final int validatedIndexOf(Object x, Object[] items, int index, int fence, long seq) { for (int i = index; i < fence; ++i) { @@ -173,6 +177,9 @@ public class ReadMostlyVector return -1; } + /** + * @throws ArrayIndexOutOfBoundsException if index is negative + */ final int rawIndexOf(Object x, int index, int fence) { Object[] items = array; for (int i = index; i < fence; ++i) { @@ -788,7 +795,7 @@ public class ReadMostlyVector // ReadMostlyVector-only methods /** - * Append the element if not present. + * Appends the element, if not present. * * @param e element to be added to this list, if absent * @return {@code true} if the element was added @@ -906,62 +913,45 @@ public class ReadMostlyVector /** See {@link Vector#indexOf(Object, int)} */ public int indexOf(Object o, int index) { final SequenceLock lock = this.lock; - int idx = 0; - boolean ex = false; long seq = lock.awaitAvailability(); Object[] items = array; int n = count; - boolean retry = false; - if (n > items.length) - retry = true; - else if (index < 0) - ex = true; - else + int idx = -1; + if (n <= items.length) idx = validatedIndexOf(o, items, index, n, seq); - if (retry || lock.getSequence() != seq) { + if (lock.getSequence() != seq) { lock.lock(); try { - if (index < 0) - ex = true; - else - idx = rawIndexOf(o, index, count); + idx = rawIndexOf(o, index, count); } finally { lock.unlock(); } } - if (ex) - throw new ArrayIndexOutOfBoundsException(index); + // Above code will throw AIOOBE when index < 0 return idx; } /** See {@link Vector#lastIndexOf(Object, int)} */ public int lastIndexOf(Object o, int index) { final SequenceLock lock = this.lock; - int idx = 0; - boolean ex = false; long seq = lock.awaitAvailability(); Object[] items = array; int n = count; - boolean retry = false; - if (n > items.length) - retry = true; - else if (index >= n) - ex = true; - else + int idx = -1; + if (index < Math.min(n, items.length)) idx = validatedLastIndexOf(o, items, index, 0, seq); - if (retry || lock.getSequence() != seq) { + if (lock.getSequence() != seq) { lock.lock(); try { - if (index >= count) - ex = true; - else + n = count; + if (index < n) idx = rawLastIndexOf(o, index, 0); } finally { lock.unlock(); } } - if (ex) - throw new ArrayIndexOutOfBoundsException(index); + if (index >= n) + throw new IndexOutOfBoundsException(index + " >= " + n); return idx; } @@ -1176,7 +1166,7 @@ public class ReadMostlyVector public E next() { if (validNext || hasNext()) { - validNext = false; + validNext = validPrev = false; lastRet = cursor++; return next; } @@ -1185,7 +1175,7 @@ public class ReadMostlyVector public E previous() { if (validPrev || hasPrevious()) { - validPrev = false; + validNext = validPrev = false; lastRet = cursor--; return prev; } @@ -1567,7 +1557,7 @@ public class ReadMostlyVector public E next() { if (validNext || hasNext()) { - validNext = false; + validNext = validPrev = false; lastRet = cursor++; return next; } @@ -1576,7 +1566,7 @@ public class ReadMostlyVector public E previous() { if (validPrev || hasPrevious()) { - validPrev = false; + validNext = validPrev = false; lastRet = cursor--; return prev; } @@ -1643,4 +1633,3 @@ public class ReadMostlyVector } } -