208 * @param index first index to search
209 * @return index of element, or -1 if absent
210 */
211 private static int lastIndexOf(Object o, Object[] elements, int index) {
212 if (o == null) {
213 for (int i = index; i >= 0; i--)
214 if (elements[i] == null)
215 return i;
216 } else {
217 for (int i = index; i >= 0; i--)
218 if (o.equals(elements[i]))
219 return i;
220 }
221 return -1;
222 }
223
224 /**
225 * Returns {@code true} if this list contains the specified element.
226 * More formally, returns {@code true} if and only if this list contains
227 * at least one element {@code e} such that
228 * <tt>(o==null ? e==null : o.equals(e))</tt>.
229 *
230 * @param o element whose presence in this list is to be tested
231 * @return {@code true} if this list contains the specified element
232 */
233 public boolean contains(Object o) {
234 Object[] elements = getArray();
235 return indexOf(o, elements, 0, elements.length) >= 0;
236 }
237
238 /**
239 * {@inheritDoc}
240 */
241 public int indexOf(Object o) {
242 Object[] elements = getArray();
243 return indexOf(o, elements, 0, elements.length);
244 }
245
246 /**
247 * Returns the index of the first occurrence of the specified element in
248 * this list, searching forwards from {@code index}, or returns -1 if
249 * the element is not found.
250 * More formally, returns the lowest index {@code i} such that
251 * <tt>(i >= index && (e==null ? get(i)==null : e.equals(get(i))))</tt>,
252 * or -1 if there is no such index.
253 *
254 * @param e element to search for
255 * @param index index to start searching from
256 * @return the index of the first occurrence of the element in
257 * this list at position {@code index} or later in the list;
258 * {@code -1} if the element is not found.
259 * @throws IndexOutOfBoundsException if the specified index is negative
260 */
261 public int indexOf(E e, int index) {
262 Object[] elements = getArray();
263 return indexOf(e, elements, index, elements.length);
264 }
265
266 /**
267 * {@inheritDoc}
268 */
269 public int lastIndexOf(Object o) {
270 Object[] elements = getArray();
271 return lastIndexOf(o, elements, elements.length - 1);
272 }
273
274 /**
275 * Returns the index of the last occurrence of the specified element in
276 * this list, searching backwards from {@code index}, or returns -1 if
277 * the element is not found.
278 * More formally, returns the highest index {@code i} such that
279 * <tt>(i <= index && (e==null ? get(i)==null : e.equals(get(i))))</tt>,
280 * or -1 if there is no such index.
281 *
282 * @param e element to search for
283 * @param index index to start searching backwards from
284 * @return the index of the last occurrence of the element at position
285 * less than or equal to {@code index} in this list;
286 * -1 if the element is not found.
287 * @throws IndexOutOfBoundsException if the specified index is greater
288 * than or equal to the current size of this list
289 */
290 public int lastIndexOf(E e, int index) {
291 Object[] elements = getArray();
292 return lastIndexOf(e, elements, index);
293 }
294
295 /**
296 * Returns a shallow copy of this list. (The elements themselves
297 * are not copied.)
298 *
299 * @return a clone of this list
497 if (numMoved == 0)
498 setArray(Arrays.copyOf(elements, len - 1));
499 else {
500 Object[] newElements = new Object[len - 1];
501 System.arraycopy(elements, 0, newElements, 0, index);
502 System.arraycopy(elements, index + 1, newElements, index,
503 numMoved);
504 setArray(newElements);
505 }
506 return oldValue;
507 } finally {
508 lock.unlock();
509 }
510 }
511
512 /**
513 * Removes the first occurrence of the specified element from this list,
514 * if it is present. If this list does not contain the element, it is
515 * unchanged. More formally, removes the element with the lowest index
516 * {@code i} such that
517 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
518 * (if such an element exists). Returns {@code true} if this list
519 * contained the specified element (or equivalently, if this list
520 * changed as a result of the call).
521 *
522 * @param o element to be removed from this list, if present
523 * @return {@code true} if this list contained the specified element
524 */
525 public boolean remove(Object o) {
526 Object[] snapshot = getArray();
527 int index = indexOf(o, snapshot, 0, snapshot.length);
528 return (index < 0) ? false : remove(o, snapshot, index);
529 }
530
531 /**
532 * A version of remove(Object) using the strong hint that given
533 * recent snapshot contains o at the given index.
534 */
535 private boolean remove(Object o, Object[] snapshot, int index) {
536 final ReentrantLock lock = this.lock;
537 lock.lock();
|
208 * @param index first index to search
209 * @return index of element, or -1 if absent
210 */
211 private static int lastIndexOf(Object o, Object[] elements, int index) {
212 if (o == null) {
213 for (int i = index; i >= 0; i--)
214 if (elements[i] == null)
215 return i;
216 } else {
217 for (int i = index; i >= 0; i--)
218 if (o.equals(elements[i]))
219 return i;
220 }
221 return -1;
222 }
223
224 /**
225 * Returns {@code true} if this list contains the specified element.
226 * More formally, returns {@code true} if and only if this list contains
227 * at least one element {@code e} such that
228 * <code>(o==null ? e==null : o.equals(e))</code>.
229 *
230 * @param o element whose presence in this list is to be tested
231 * @return {@code true} if this list contains the specified element
232 */
233 public boolean contains(Object o) {
234 Object[] elements = getArray();
235 return indexOf(o, elements, 0, elements.length) >= 0;
236 }
237
238 /**
239 * {@inheritDoc}
240 */
241 public int indexOf(Object o) {
242 Object[] elements = getArray();
243 return indexOf(o, elements, 0, elements.length);
244 }
245
246 /**
247 * Returns the index of the first occurrence of the specified element in
248 * this list, searching forwards from {@code index}, or returns -1 if
249 * the element is not found.
250 * More formally, returns the lowest index {@code i} such that
251 * <code>(i >= index && (e==null ? get(i)==null : e.equals(get(i))))</code>,
252 * or -1 if there is no such index.
253 *
254 * @param e element to search for
255 * @param index index to start searching from
256 * @return the index of the first occurrence of the element in
257 * this list at position {@code index} or later in the list;
258 * {@code -1} if the element is not found.
259 * @throws IndexOutOfBoundsException if the specified index is negative
260 */
261 public int indexOf(E e, int index) {
262 Object[] elements = getArray();
263 return indexOf(e, elements, index, elements.length);
264 }
265
266 /**
267 * {@inheritDoc}
268 */
269 public int lastIndexOf(Object o) {
270 Object[] elements = getArray();
271 return lastIndexOf(o, elements, elements.length - 1);
272 }
273
274 /**
275 * Returns the index of the last occurrence of the specified element in
276 * this list, searching backwards from {@code index}, or returns -1 if
277 * the element is not found.
278 * More formally, returns the highest index {@code i} such that
279 * <code>(i <= index && (e==null ? get(i)==null : e.equals(get(i))))</code>,
280 * or -1 if there is no such index.
281 *
282 * @param e element to search for
283 * @param index index to start searching backwards from
284 * @return the index of the last occurrence of the element at position
285 * less than or equal to {@code index} in this list;
286 * -1 if the element is not found.
287 * @throws IndexOutOfBoundsException if the specified index is greater
288 * than or equal to the current size of this list
289 */
290 public int lastIndexOf(E e, int index) {
291 Object[] elements = getArray();
292 return lastIndexOf(e, elements, index);
293 }
294
295 /**
296 * Returns a shallow copy of this list. (The elements themselves
297 * are not copied.)
298 *
299 * @return a clone of this list
497 if (numMoved == 0)
498 setArray(Arrays.copyOf(elements, len - 1));
499 else {
500 Object[] newElements = new Object[len - 1];
501 System.arraycopy(elements, 0, newElements, 0, index);
502 System.arraycopy(elements, index + 1, newElements, index,
503 numMoved);
504 setArray(newElements);
505 }
506 return oldValue;
507 } finally {
508 lock.unlock();
509 }
510 }
511
512 /**
513 * Removes the first occurrence of the specified element from this list,
514 * if it is present. If this list does not contain the element, it is
515 * unchanged. More formally, removes the element with the lowest index
516 * {@code i} such that
517 * <code>(o==null ? get(i)==null : o.equals(get(i)))</code>
518 * (if such an element exists). Returns {@code true} if this list
519 * contained the specified element (or equivalently, if this list
520 * changed as a result of the call).
521 *
522 * @param o element to be removed from this list, if present
523 * @return {@code true} if this list contained the specified element
524 */
525 public boolean remove(Object o) {
526 Object[] snapshot = getArray();
527 int index = indexOf(o, snapshot, 0, snapshot.length);
528 return (index < 0) ? false : remove(o, snapshot, index);
529 }
530
531 /**
532 * A version of remove(Object) using the strong hint that given
533 * recent snapshot contains o at the given index.
534 */
535 private boolean remove(Object o, Object[] snapshot, int index) {
536 final ReentrantLock lock = this.lock;
537 lock.lock();
|