jsr166e.extra
Class ReadMostlyVector<E>

java.lang.Object
  extended by jsr166e.extra.ReadMostlyVector<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess

public class ReadMostlyVector<E>
extends Object
implements List<E>, RandomAccess, Cloneable, Serializable

A class with the same methods and array-based characteristics as Vector but with reduced contention and improved throughput when invocations of read-only methods by multiple threads are most common.

The iterators returned by this class's iterator and listIterator methods are best-effort in the presence of concurrent modifications, and do NOT throw ConcurrentModificationException. An iterator's next() method returns consecutive elements as they appear in the underlying array upon each access. Alternatively, method snapshotIterator() may be used for deterministic traversals, at the expense of making a copy, and unavailability of method Iterator.remove.

Otherwise, this class supports all methods, under the same documented specifications, as Vector. Consult Vector for detailed specifications. Additionally, this class provides methods addIfAbsent(E) and addAllAbsent(java.util.Collection).

Author:
Doug Lea
See Also:
Serialized Form

Constructor Summary
ReadMostlyVector()
          Creates an empty vector with an underlying array of size 10.
ReadMostlyVector(Collection<? extends E> c)
          Creates a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.
ReadMostlyVector(int initialCapacity)
          Creates an empty vector with the given initial capacity.
ReadMostlyVector(int initialCapacity, int capacityIncrement)
          Creates an empty vector with the given initial capacity and capacity increment.
 
Method Summary
 boolean add(E e)
           
 void add(int index, E element)
           
 boolean addAll(Collection<? extends E> c)
           
 boolean addAll(int index, Collection<? extends E> c)
           
 int addAllAbsent(Collection<? extends E> c)
          Appends all of the elements in the specified collection that are not already contained in this list, to the end of this list, in the order that they are returned by the specified collection's iterator.
 void addElement(E obj)
          See Vector.addElement(E)
 boolean addIfAbsent(E e)
          Appends the element, if not present.
 int capacity()
          See Vector.capacity()
 void clear()
           
 ReadMostlyVector<E> clone()
           
 boolean contains(Object o)
           
 boolean containsAll(Collection<?> c)
           
 void copyInto(Object[] anArray)
          See Vector.copyInto(java.lang.Object[])
 E elementAt(int index)
          See Vector.elementAt(int)
 Enumeration<E> elements()
          See Vector.elements()
 void ensureCapacity(int minCapacity)
          See Vector.ensureCapacity(int)
 boolean equals(Object o)
           
 E firstElement()
          See Vector.firstElement()
 E get(int index)
           
 int hashCode()
           
 int indexOf(Object o)
           
 int indexOf(Object o, int index)
          See Vector.indexOf(Object, int)
 void insertElementAt(E obj, int index)
          See Vector.insertElementAt(E, int)
 boolean isEmpty()
           
 Iterator<E> iterator()
           
 E lastElement()
          See Vector.lastElement()
 int lastIndexOf(Object o)
           
 int lastIndexOf(Object o, int index)
          See Vector.lastIndexOf(Object, int)
 ListIterator<E> listIterator()
           
 ListIterator<E> listIterator(int index)
           
 E remove(int index)
           
 boolean remove(Object o)
           
 boolean removeAll(Collection<?> c)
           
 void removeAllElements()
          See Vector.removeAllElements()
 boolean removeElement(Object obj)
          See Vector.removeElement(java.lang.Object)
 void removeElementAt(int index)
          See Vector.removeElementAt(int)
 boolean retainAll(Collection<?> c)
           
 E set(int index, E element)
           
 void setElementAt(E obj, int index)
          See Vector.setElementAt(E, int)
 void setSize(int newSize)
          See Vector.setSize(int)
 int size()
           
 Iterator<E> snapshotIterator()
          Returns an iterator operating over a snapshot copy of the elements of this collection created upon construction of the iterator.
 List<E> subList(int fromIndex, int toIndex)
           
 Object[] toArray()
           
<T> T[]
toArray(T[] a)
           
 String toString()
           
 void trimToSize()
          See Vector.trimToSize()
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ReadMostlyVector

public ReadMostlyVector(int initialCapacity,
                        int capacityIncrement)
Creates an empty vector with the given initial capacity and capacity increment.

Parameters:
initialCapacity - the initial capacity of the underlying array
capacityIncrement - if non-zero, the number to add when resizing to accommodate additional elements. If zero, the array size is doubled when resized.
Throws:
IllegalArgumentException - if initial capacity is negative

ReadMostlyVector

public ReadMostlyVector(int initialCapacity)
Creates an empty vector with the given initial capacity.

Parameters:
initialCapacity - the initial capacity of the underlying array
Throws:
IllegalArgumentException - if initial capacity is negative

ReadMostlyVector

public ReadMostlyVector()
Creates an empty vector with an underlying array of size 10.


ReadMostlyVector

public ReadMostlyVector(Collection<? extends E> c)
Creates a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Parameters:
c - the collection of initially held elements
Throws:
NullPointerException - if the specified collection is null
Method Detail

add

public boolean add(E e)
Specified by:
add in interface Collection<E>
Specified by:
add in interface List<E>

add

public void add(int index,
                E element)
Specified by:
add in interface List<E>

addAll

public boolean addAll(Collection<? extends E> c)
Specified by:
addAll in interface Collection<E>
Specified by:
addAll in interface List<E>

addAll

public boolean addAll(int index,
                      Collection<? extends E> c)
Specified by:
addAll in interface List<E>

clear

public void clear()
Specified by:
clear in interface Collection<E>
Specified by:
clear in interface List<E>

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection<E>
Specified by:
contains in interface List<E>

containsAll

public boolean containsAll(Collection<?> c)
Specified by:
containsAll in interface Collection<E>
Specified by:
containsAll in interface List<E>

equals

public boolean equals(Object o)
Specified by:
equals in interface Collection<E>
Specified by:
equals in interface List<E>
Overrides:
equals in class Object

get

public E get(int index)
Specified by:
get in interface List<E>

hashCode

public int hashCode()
Specified by:
hashCode in interface Collection<E>
Specified by:
hashCode in interface List<E>
Overrides:
hashCode in class Object

indexOf

public int indexOf(Object o)
Specified by:
indexOf in interface List<E>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface List<E>

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface List<E>

lastIndexOf

public int lastIndexOf(Object o)
Specified by:
lastIndexOf in interface List<E>

listIterator

public ListIterator<E> listIterator()
Specified by:
listIterator in interface List<E>

listIterator

public ListIterator<E> listIterator(int index)
Specified by:
listIterator in interface List<E>

remove

public E remove(int index)
Specified by:
remove in interface List<E>

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection<E>
Specified by:
remove in interface List<E>

removeAll

public boolean removeAll(Collection<?> c)
Specified by:
removeAll in interface Collection<E>
Specified by:
removeAll in interface List<E>

retainAll

public boolean retainAll(Collection<?> c)
Specified by:
retainAll in interface Collection<E>
Specified by:
retainAll in interface List<E>

set

public E set(int index,
             E element)
Specified by:
set in interface List<E>

size

public int size()
Specified by:
size in interface Collection<E>
Specified by:
size in interface List<E>

subList

public List<E> subList(int fromIndex,
                       int toIndex)
Specified by:
subList in interface List<E>

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>

toArray

public <T> T[] toArray(T[] a)
Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>

toString

public String toString()
Overrides:
toString in class Object

addIfAbsent

public boolean addIfAbsent(E e)
Appends the element, if not present.

Parameters:
e - element to be added to this list, if absent
Returns:
true if the element was added

addAllAbsent

public int addAllAbsent(Collection<? extends E> c)
Appends all of the elements in the specified collection that are not already contained in this list, to the end of this list, in the order that they are returned by the specified collection's iterator.

Parameters:
c - collection containing elements to be added to this list
Returns:
the number of elements added
Throws:
NullPointerException - if the specified collection is null
See Also:
addIfAbsent(Object)

snapshotIterator

public Iterator<E> snapshotIterator()
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 remove method.

Returns:
an iterator over the elements in this list in proper sequence

firstElement

public E firstElement()
See Vector.firstElement()


lastElement

public E lastElement()
See Vector.lastElement()


indexOf

public int indexOf(Object o,
                   int index)
See Vector.indexOf(Object, int)


lastIndexOf

public int lastIndexOf(Object o,
                       int index)
See Vector.lastIndexOf(Object, int)


setSize

public void setSize(int newSize)
See Vector.setSize(int)


copyInto

public void copyInto(Object[] anArray)
See Vector.copyInto(java.lang.Object[])


trimToSize

public void trimToSize()
See Vector.trimToSize()


ensureCapacity

public void ensureCapacity(int minCapacity)
See Vector.ensureCapacity(int)


elements

public Enumeration<E> elements()
See Vector.elements()


capacity

public int capacity()
See Vector.capacity()


elementAt

public E elementAt(int index)
See Vector.elementAt(int)


setElementAt

public void setElementAt(E obj,
                         int index)
See Vector.setElementAt(E, int)


removeElementAt

public void removeElementAt(int index)
See Vector.removeElementAt(int)


insertElementAt

public void insertElementAt(E obj,
                            int index)
See Vector.insertElementAt(E, int)


addElement

public void addElement(E obj)
See Vector.addElement(E)


removeElement

public boolean removeElement(Object obj)
See Vector.removeElement(java.lang.Object)


removeAllElements

public void removeAllElements()
See Vector.removeAllElements()


clone

public ReadMostlyVector<E> clone()
Overrides:
clone in class Object


Copyright © 2009-2012. All Rights Reserved.