net.walend.collection
Class AbstractArrayList

java.lang.Object
  |
  +--net.walend.collection.AbstractArrayList
All Implemented Interfaces:
Collection, HasState, List
Direct Known Subclasses:
ArrayList, MutableArrayList

public abstract class AbstractArrayList
extends java.lang.Object
implements List

An implementation of List backed by an array.

This class implements the net.walend.collection.List interface, based very heavily on the java.util.ArrayList class by Josh Bloch. Any mistakes I'm sure are my own and not his.

Since:
20010908
Author:
David Walend

Nested Class Summary
protected  class AbstractArrayList.ListItr
           
 
Field Summary
private  java.lang.Object[] elementData
          The array buffer into which the elements of the ArrayList are stored.
private  Identitor identitor
           
protected  int modCount
          The number of times this list has been structurally modified.
private  int size
          The size of the ArrayList (the number of elements it contains).
 
Fields inherited from interface net.walend.collection.List
EMPTY
 
Constructor Summary
protected AbstractArrayList()
           
protected AbstractArrayList(Collection collection)
           
protected AbstractArrayList(java.util.Collection collection)
           
protected AbstractArrayList(Identitor ident)
           
protected AbstractArrayList(Identitor ident, int initialCapacity)
           
protected AbstractArrayList(int initialCapacity)
           
 
Method Summary
protected  boolean add(int index, java.lang.Object ob)
          Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
protected  boolean add(java.lang.Object object)
          Adds a Object to the MutableCollection.
protected  boolean addAll(Collection c)
          Adds all the Objects in c to this MutableCollection by using the add() method.
protected  boolean addAll(int index, Collection col)
          Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
protected  void checkRange(int index)
           
protected  void checkRangeForAdd(int index)
           
protected  void clear()
          Removes all of the Objects in this collection.
 boolean contains(java.lang.Object object)
          Returns true if this collection contains the specified Object.
 boolean containsAll(Collection c)
          Returns true if this collection contains all of the Objects in the specified collection.
protected  void ensureCapacity(int minCapacity)
          Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
 java.lang.Object get(int index)
          Return the object at position index.
 Identitor getIdentitor()
          Returns the Identitor for this Collection.
 java.util.Collection getJavaCollection()
          Return a java.util.Collection of these Objects.
 java.util.List getJavaList()
          Return a java.util.Collection of these Objects.
 java.lang.Class getPrincipleInterface()
          Returns the class's principle interface for state comparisons.
 int indexOf(java.lang.Object ob)
          Returns the index of the first occurrence of ob, or -1 if ob is not in the list.
 int indexOfSubList(List subList)
          Returns the index of the subList if this List contains the subList starting at any index, or -1.
 boolean isEmpty()
          Returns true if this collection contains no elements.
 java.util.Iterator iterator()
          Returns an iterator over the Objects in this collection.
 int lastIndexOf(java.lang.Object ob)
          Returns the index of the last occurrence of ob, or -1 if ob is not in the list.
 java.util.ListIterator listIterator()
          Returns an Iterator over this list that iterates through the list in order.
 java.util.ListIterator listIterator(int start)
          Returns an Iterator over this list that iterates through the list in order, starting with the Object at index start.
protected  java.lang.Object remove(int index)
          Removes and returns the object at index in this list.
protected  boolean remove(java.lang.Object object)
          Removes a Object from the MutableCollection.
protected  boolean removeAll(Collection c)
          Removes all the Objects in c from this MutableCollection by using the remove() method.
protected  boolean retainAll(Collection c)
          Retains only the Objects in this collection that are contained in c by using the contains() and remove() methods.
 boolean sameContentsAs(Collection c)
          Returns true if this Collection's contents are equal to c's.
 boolean sameListAs(List list)
          Returns true if this contains the same objects in the same order as list.
 boolean sameStateAs(HasState victem)
          If two HasStates have the same internal state, return true.
protected  java.lang.Object set(int index, java.lang.Object ob)
          Replaces the element at the specified position in this list with the specified element
 int size()
          Returns the number of elements in this collection.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

identitor

private Identitor identitor

elementData

private transient java.lang.Object[] elementData
The array buffer into which the elements of the ArrayList are stored. The capacity of the ArrayList is the length of this array buffer.


size

private int size
The size of the ArrayList (the number of elements it contains).


modCount

protected transient int modCount
The number of times this list has been structurally modified. Structural modifications are those that change the size of the list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.

This field is used by the iterator and list iterator implementation returned by the iterator and listIterator methods. If the value of this field changes unexpectedly, the iterator (or list iterator) will throw a ConcurrentModificationException in response to the next, remove, previous, set or add operations. This provides fail-fast behavior, rather than non-deterministic behavior in the face of concurrent modification during iteration.

Use of this field by subclasses is optional. If a subclass wishes to provide fail-fast iterators (and list iterators), then it merely has to increment this field in its add(int, Object) and remove(int) methods (and any other methods that it overrides that result in structural modifications to the list). A single call to add(int, Object) or remove(int) must add no more than one to this field, or the iterators (and list iterators) will throw bogus ConcurrentModificationExceptions. If an implementation does not wish to provide fail-fast iterators, this field may be ignored.

Constructor Detail

AbstractArrayList

protected AbstractArrayList()

AbstractArrayList

protected AbstractArrayList(int initialCapacity)

AbstractArrayList

protected AbstractArrayList(Collection collection)

AbstractArrayList

protected AbstractArrayList(java.util.Collection collection)

AbstractArrayList

protected AbstractArrayList(Identitor ident)

AbstractArrayList

protected AbstractArrayList(Identitor ident,
                            int initialCapacity)
Method Detail

getIdentitor

public Identitor getIdentitor()
Returns the Identitor for this Collection.

Specified by:
getIdentitor in interface Collection

size

public int size()
Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Specified by:
size in interface Collection

isEmpty

public boolean isEmpty()
Returns true if this collection contains no elements.

Specified by:
isEmpty in interface Collection

contains

public boolean contains(java.lang.Object object)
Returns true if this collection contains the specified Object.

Specified by:
contains in interface Collection

iterator

public java.util.Iterator iterator()
Returns an iterator over the Objects in this collection.

Specified by:
iterator in interface Collection

containsAll

public boolean containsAll(Collection c)
Returns true if this collection contains all of the Objects in the specified collection. This method uses the contains() method.

Specified by:
containsAll in interface Collection

sameContentsAs

public boolean sameContentsAs(Collection c)
Returns true if this Collection's contents are equal to c's.

Specified by:
sameContentsAs in interface Collection

getJavaCollection

public java.util.Collection getJavaCollection()
Return a java.util.Collection of these Objects.

Specified by:
getJavaCollection in interface Collection

checkRange

protected void checkRange(int index)

checkRangeForAdd

protected void checkRangeForAdd(int index)

get

public java.lang.Object get(int index)
Return the object at position index.

Specified by:
get in interface List
Throws:
java.lang.IndexOutOfBoundsException - if the index is not in the list.

indexOf

public int indexOf(java.lang.Object ob)
Returns the index of the first occurrence of ob, or -1 if ob is not in the list.

Specified by:
indexOf in interface List

lastIndexOf

public int lastIndexOf(java.lang.Object ob)
Returns the index of the last occurrence of ob, or -1 if ob is not in the list.

Specified by:
lastIndexOf in interface List

listIterator

public java.util.ListIterator listIterator()
Returns an Iterator over this list that iterates through the list in order.

Specified by:
listIterator in interface List

listIterator

public java.util.ListIterator listIterator(int start)
Returns an Iterator over this list that iterates through the list in order, starting with the Object at index start.

Specified by:
listIterator in interface List
Throws:
java.lang.IndexOutOfBoundsException - if the index is not in the list.

indexOfSubList

public int indexOfSubList(List subList)
Returns the index of the subList if this List contains the subList starting at any index, or -1.

Specified by:
indexOfSubList in interface List

sameListAs

public boolean sameListAs(List list)
Returns true if this contains the same objects in the same order as list.

Specified by:
sameListAs in interface List

getJavaList

public java.util.List getJavaList()
Return a java.util.Collection of these Objects.

Specified by:
getJavaList in interface List

ensureCapacity

protected void ensureCapacity(int minCapacity)
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity - the desired minimum capacity.

add

protected boolean add(java.lang.Object object)
Adds a Object to the MutableCollection. Sets will only add it to the Set if it's not already there.

Returns:
true if the MutableCollection changes, false if not.

remove

protected boolean remove(java.lang.Object object)
Removes a Object from the MutableCollection.

Returns:
true if the MutableCollection changes, false if not.

addAll

protected boolean addAll(Collection c)
Adds all the Objects in c to this MutableCollection by using the add() method. Returns true if this MutableCollection changes.


removeAll

protected boolean removeAll(Collection c)
Removes all the Objects in c from this MutableCollection by using the remove() method. Returns true if this Colleciton changes.


retainAll

protected boolean retainAll(Collection c)
Retains only the Objects in this collection that are contained in c by using the contains() and remove() methods. Returns true if this Colleciton changes.


clear

protected void clear()
Removes all of the Objects in this collection.


add

protected boolean add(int index,
                      java.lang.Object ob)
Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Returns:
true
Throws:
java.lang.IndexOutOfBoundsException - if the index is not in the list.

addAll

protected boolean addAll(int index,
                         Collection col)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Returns:
true
Throws:
java.lang.IndexOutOfBoundsException - if the index is not in the list.

remove

protected java.lang.Object remove(int index)
Removes and returns the object at index in this list. Shifts any following elements forward in the list.

Returns:
the object removed from the list.
Throws:
java.lang.IndexOutOfBoundsException - if the index is not in the list.

set

protected java.lang.Object set(int index,
                               java.lang.Object ob)
Replaces the element at the specified position in this list with the specified element

Returns:
the object that was at index.
Throws:
java.lang.IndexOutOfBoundsException - if the index is not in the list.

getPrincipleInterface

public java.lang.Class getPrincipleInterface()
Description copied from interface: HasState
Returns the class's principle interface for state comparisons. If two objects have different principle interfaces, they never have the same state.

Specified by:
getPrincipleInterface in interface HasState

sameStateAs

public boolean sameStateAs(HasState victem)
Description copied from interface: HasState
If two HasStates have the same internal state, return true.

For objects with subobjects, Generally this method should only return true if the internal objects are equal. Implement a contentsHaveSameState() method to determine if the contents have the same state.

Specified by:
sameStateAs in interface HasState

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright (c) 2000, 2001, David Walend