160 |
|
* as well as sublist and iterator classes. |
161 |
|
*/ |
162 |
|
|
163 |
< |
// Version of indexOf that returns -1 if either not present or invalid |
163 |
> |
/** |
164 |
> |
* Version of indexOf that returns -1 if either not present or invalid. |
165 |
> |
* |
166 |
> |
* @throws ArrayIndexOutOfBoundsException if index is negative |
167 |
> |
*/ |
168 |
|
final int validatedIndexOf(Object x, Object[] items, int index, int fence, |
169 |
|
long seq) { |
170 |
|
for (int i = index; i < fence; ++i) { |
177 |
|
return -1; |
178 |
|
} |
179 |
|
|
180 |
+ |
/** |
181 |
+ |
* @throws ArrayIndexOutOfBoundsException if index is negative |
182 |
+ |
*/ |
183 |
|
final int rawIndexOf(Object x, int index, int fence) { |
184 |
|
Object[] items = array; |
185 |
|
for (int i = index; i < fence; ++i) { |
913 |
|
/** See {@link Vector#indexOf(Object, int)} */ |
914 |
|
public int indexOf(Object o, int index) { |
915 |
|
final SequenceLock lock = this.lock; |
909 |
– |
int idx = 0; |
910 |
– |
boolean ex = false; |
916 |
|
long seq = lock.awaitAvailability(); |
917 |
|
Object[] items = array; |
918 |
|
int n = count; |
919 |
< |
boolean retry = false; |
920 |
< |
if (n > items.length) |
916 |
< |
retry = true; |
917 |
< |
else if (index < 0) |
918 |
< |
ex = true; |
919 |
< |
else |
919 |
> |
int idx = -1; |
920 |
> |
if (n <= items.length) |
921 |
|
idx = validatedIndexOf(o, items, index, n, seq); |
922 |
< |
if (retry || lock.getSequence() != seq) { |
922 |
> |
if (lock.getSequence() != seq) { |
923 |
|
lock.lock(); |
924 |
|
try { |
925 |
< |
if (index < 0) |
925 |
< |
ex = true; |
926 |
< |
else |
927 |
< |
idx = rawIndexOf(o, index, count); |
925 |
> |
idx = rawIndexOf(o, index, count); |
926 |
|
} finally { |
927 |
|
lock.unlock(); |
928 |
|
} |
929 |
|
} |
930 |
< |
if (ex) |
933 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
930 |
> |
// Above code will throw AIOOBE when index < 0 |
931 |
|
return idx; |
932 |
|
} |
933 |
|
|
934 |
|
/** See {@link Vector#lastIndexOf(Object, int)} */ |
935 |
|
public int lastIndexOf(Object o, int index) { |
936 |
|
final SequenceLock lock = this.lock; |
940 |
– |
int idx = 0; |
941 |
– |
boolean ex = false; |
937 |
|
long seq = lock.awaitAvailability(); |
938 |
|
Object[] items = array; |
939 |
|
int n = count; |
940 |
< |
boolean retry = false; |
941 |
< |
if (n > items.length) |
947 |
< |
retry = true; |
948 |
< |
else if (index >= n) |
949 |
< |
ex = true; |
950 |
< |
else |
940 |
> |
int idx = -1; |
941 |
> |
if (index < Math.min(n, items.length)) |
942 |
|
idx = validatedLastIndexOf(o, items, index, 0, seq); |
943 |
< |
if (retry || lock.getSequence() != seq) { |
943 |
> |
if (lock.getSequence() != seq) { |
944 |
|
lock.lock(); |
945 |
|
try { |
946 |
< |
if (index >= count) |
947 |
< |
ex = true; |
957 |
< |
else |
946 |
> |
n = count; |
947 |
> |
if (index < n) |
948 |
|
idx = rawLastIndexOf(o, index, 0); |
949 |
|
} finally { |
950 |
|
lock.unlock(); |
951 |
|
} |
952 |
|
} |
953 |
< |
if (ex) |
954 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
953 |
> |
if (index >= n) |
954 |
> |
throw new IndexOutOfBoundsException(index + " >= " + n); |
955 |
|
return idx; |
956 |
|
} |
957 |
|
|