📄 fastarraylist.java
字号:
}
}
}
/**
* Search for the first occurrence of the given argument, testing
* for equality using the <code>equals()</code> method, and return
* the corresponding index, or -1 if the object is not found.
*
* @param element The element to search for
*/
public int indexOf(Object element) {
if (fast) {
return (list.indexOf(element));
} else {
synchronized (list) {
return (list.indexOf(element));
}
}
}
/**
* Test if this list has no elements.
*/
public boolean isEmpty() {
if (fast) {
return (list.isEmpty());
} else {
synchronized (list) {
return (list.isEmpty());
}
}
}
/**
* Return an iterator over the elements in this list in proper sequence.
* <p>
* <b>Thread safety</b><br />
* The iterator returned is thread-safe ONLY in FAST mode.
* In slow mode there is no way to synchronize, or make the iterator thread-safe.
* <p>
* In fast mode iteration and modification may occur in parallel on different threads,
* however there is a restriction. Modification must be EITHER via the Iterator
* interface methods OR the List interface. If a mixture of modification
* methods is used a ConcurrentModificationException is thrown from the iterator
* modification method. If the List modification methods are used the changes are
* NOT visible in the iterator (it shows the list contents at the time the iterator
* was created).
*
* @return the iterator
*/
public Iterator iterator() {
if (fast) {
return new ListIter(0);
} else {
return list.iterator();
}
}
/**
* Search for the last occurrence of the given argument, testing
* for equality using the <code>equals()</code> method, and return
* the corresponding index, or -1 if the object is not found.
*
* @param element The element to search for
*/
public int lastIndexOf(Object element) {
if (fast) {
return (list.lastIndexOf(element));
} else {
synchronized (list) {
return (list.lastIndexOf(element));
}
}
}
/**
* Return an iterator of the elements of this list, in proper sequence.
* <p>
* <b>Thread safety</b><br />
* The iterator returned is thread-safe ONLY in FAST mode.
* In slow mode there is no way to synchronize, or make the iterator thread-safe.
* <p>
* In fast mode iteration and modification may occur in parallel on different threads,
* however there is a restriction. Modification must be EITHER via the Iterator
* interface methods OR the List interface. If a mixture of modification
* methods is used a ConcurrentModificationException is thrown from the iterator
* modification method. If the List modification methods are used the changes are
* NOT visible in the iterator (it shows the list contents at the time the iterator
* was created).
*
* @return the list iterator
*/
public ListIterator listIterator() {
if (fast) {
return new ListIter(0);
} else {
return list.listIterator();
}
}
/**
* Return an iterator of the elements of this list, in proper sequence,
* starting at the specified position.
* <p>
* <b>Thread safety</b><br />
* The iterator returned is thread-safe ONLY in FAST mode.
* In slow mode there is no way to synchronize, or make the iterator thread-safe.
* <p>
* In fast mode iteration and modification may occur in parallel on different threads,
* however there is a restriction. Modification must be EITHER via the Iterator
* interface methods OR the List interface. If a mixture of modification
* methods is used a ConcurrentModificationException is thrown from the iterator
* modification method. If the List modification methods are used the changes are
* NOT visible in the iterator (it shows the list contents at the time the iterator
* was created).
*
* @param index The starting position of the iterator to return
* @return the list iterator
* @exception IndexOutOfBoundsException if the index is out of range
*/
public ListIterator listIterator(int index) {
if (fast) {
return new ListIter(index);
} else {
return list.listIterator(index);
}
}
/**
* Remove the element at the specified position in the list, and shift
* any subsequent elements down one position.
*
* @param index Index of the element to be removed
*
* @exception IndexOutOfBoundsException if the index is out of range
*/
public Object remove(int index) {
if (fast) {
synchronized (this) {
ArrayList temp = (ArrayList) list.clone();
Object result = temp.remove(index);
list = temp;
return (result);
}
} else {
synchronized (list) {
return (list.remove(index));
}
}
}
/**
* Remove the first occurrence of the specified element from the list,
* and shift any subsequent elements down one position.
*
* @param element Element to be removed
*/
public boolean remove(Object element) {
if (fast) {
synchronized (this) {
ArrayList temp = (ArrayList) list.clone();
boolean result = temp.remove(element);
list = temp;
return (result);
}
} else {
synchronized (list) {
return (list.remove(element));
}
}
}
/**
* Remove from this collection all of its elements that are contained
* in the specified collection.
*
* @param collection Collection containing elements to be removed
*
* @exception UnsupportedOperationException if this optional operation
* is not supported by this list
*/
public boolean removeAll(Collection collection) {
if (fast) {
synchronized (this) {
ArrayList temp = (ArrayList) list.clone();
boolean result = temp.removeAll(collection);
list = temp;
return (result);
}
} else {
synchronized (list) {
return (list.removeAll(collection));
}
}
}
/**
* Remove from this collection all of its elements except those that are
* contained in the specified collection.
*
* @param collection Collection containing elements to be retained
*
* @exception UnsupportedOperationException if this optional operation
* is not supported by this list
*/
public boolean retainAll(Collection collection) {
if (fast) {
synchronized (this) {
ArrayList temp = (ArrayList) list.clone();
boolean result = temp.retainAll(collection);
list = temp;
return (result);
}
} else {
synchronized (list) {
return (list.retainAll(collection));
}
}
}
/**
* Replace the element at the specified position in this list with
* the specified element. Returns the previous object at that position.
* <br><br>
* <strong>IMPLEMENTATION NOTE</strong> - This operation is specifically
* documented to not be a structural change, so it is safe to be performed
* without cloning.
*
* @param index Index of the element to replace
* @param element The new element to be stored
*
* @exception IndexOutOfBoundsException if the index is out of range
*/
public Object set(int index, Object element) {
if (fast) {
return (list.set(index, element));
} else {
synchronized (list) {
return (list.set(index, element));
}
}
}
/**
* Return the number of elements in this list.
*/
public int size() {
if (fast) {
return (list.size());
} else {
synchronized (list) {
return (list.size());
}
}
}
/**
* Return a view of the portion of this list between fromIndex
* (inclusive) and toIndex (exclusive). The returned list is backed
* by this list, so non-structural changes in the returned list are
* reflected in this list. The returned list supports
* all of the optional list operations supported by this list.
*
* @param fromIndex The starting index of the sublist view
* @param toIndex The index after the end of the sublist view
*
* @exception IndexOutOfBoundsException if an index is out of range
*/
public List subList(int fromIndex, int toIndex) {
if (fast) {
return new SubList(fromIndex, toIndex);
} else {
return list.subList(fromIndex, toIndex);
}
}
/**
* Return an array containing all of the elements in this list in the
* correct order.
*/
public Object[] toArray() {
if (fast) {
return (list.toArray());
} else {
synchronized (list) {
return (list.toArray());
}
}
}
/**
* Return an array containing all of the elements in this list in the
* correct order. The runtime type of the returned array is that of
* the specified array. If the list fits in the specified array, it is
* returned therein. Otherwise, a new array is allocated with the
* runtime type of the specified array, and the size of this list.
*
* @param array Array defining the element type of the returned list
*
* @exception ArrayStoreException if the runtime type of <code>array</code>
* is not a supertype of the runtime type of every element in this list
*/
public Object[] toArray(Object array[]) {
if (fast) {
return (list.toArray(array));
} else {
synchronized (list) {
return (list.toArray(array));
}
}
}
/**
* Return a String representation of this object.
*/
public String toString() {
StringBuffer sb = new StringBuffer("FastArrayList[");
sb.append(list.toString());
sb.append("]");
return (sb.toString());
}
/**
* Trim the capacity of this <code>ArrayList</code> instance to be the
* list's current size. An application can use this operation to minimize
* the storage of an <code>ArrayList</code> instance.
*/
public void trimToSize() {
if (fast) {
synchronized (this) {
ArrayList temp = (ArrayList) list.clone();
temp.trimToSize();
list = temp;
}
} else {
synchronized (list) {
list.trimToSize();
}
}
}
private class SubList implements List {
private int first;
private int last;
private List expected;
public SubList(int first, int last) {
this.first = first;
this.last = last;
this.expected = list;
}
private List get(List l) {
if (list != expected) {
throw new ConcurrentModificationException();
}
return l.subList(first, last);
}
public void clear() {
if (fast) {
synchronized (FastArrayList.this) {
ArrayList temp = (ArrayList) list.clone();
get(temp).clear();
last = first;
list = temp;
expected = temp;
}
} else {
synchronized (list) {
get(expected).clear();
}
}
}
public boolean remove(Object o) {
if (fast) {
synchronized (FastArrayList.this) {
ArrayList temp = (ArrayList) list.clone();
boolean r = get(temp).remove(o);
if (r) last--;
list = temp;
expected = temp;
return r;
}
} else {
synchronized (list) {
return get(expected).remove(o);
}
}
}
public boolean removeAll(Collection o) {
if (fast) {
synchronized (FastArrayList.this) {
ArrayList temp = (ArrayList) list.clone();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -