Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.virtuosotechnologies.lib.collections
Class RotatingArrayList  view RotatingArrayList download RotatingArrayList.java

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byjava.util.AbstractList
          extended bycom.virtuosotechnologies.lib.collections.RotatingArrayList
All Implemented Interfaces:
java.lang.Cloneable, java.util.Collection, java.lang.Iterable, java.util.List, java.io.Serializable

public class RotatingArrayList
extends java.util.AbstractList
implements java.util.List, java.lang.Cloneable, java.io.Serializable

Rotating resizeable array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.

This class is roughly equivalent to ArrayList (and is based on the ArrayList implementation) with one important distinction. The add(index, object) method operates in amortized constant time rather than linear time when inserting at the beginning or end of a list, and the remove(index) method also operates in amortized constant time when removing from the beginning or end. This enables efficient use of this class in double-ended queues.


Nested Class Summary
 
Nested classes inherited from class java.util.AbstractList
 
Field Summary
private  java.lang.Object[] elementData_
          The array buffer into which the elements of the ArrayList are stored.
private  int first_
          Array index of the first element.
private  int last_
          Array index of the last element, plus 1.
private  int size_
          The size of the RotatingArrayList (the number of elements it contains).
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
RotatingArrayList()
          Constructs an empty list.
RotatingArrayList(java.util.Collection c)
          Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
RotatingArrayList(int initialCapacity)
          Constructs an empty list with the specified initial capacity.
 
Method Summary
 void add(int index, java.lang.Object element)
          Inserts the specified element at the specified position in this list.
 boolean add(java.lang.Object o)
          Appends the specified element to the end of this list.
 boolean addAll(java.util.Collection c)
          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.
 boolean addAll(int index, java.util.Collection c)
          Inserts all of the elements in the specified Collection into this list, starting at the specified position.
 boolean addAllFirst(java.util.Collection c)
          Appends all of the elements in the specified Collection to the beginning of this list, in the order that they are returned by the specified Collection's Iterator.
 boolean addAllLast(java.util.Collection c)
          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.
 boolean addFirst(java.lang.Object o)
          Appends the specified element to the front of this list.
 boolean addLast(java.lang.Object o)
          Appends the specified element to the back of this list.
 void clear()
          Removes all of the elements from this list.
 java.lang.Object clone()
          Returns a shallow copy of this RotatingArrayList instance.
 boolean contains(java.lang.Object elem)
          Returns true if this list contains the specified element.
private  void copyIntoArray_(java.lang.Object[] aArray)
          Copies list into a given array
 void ensureCapacity(int minCapacity)
          Increases the capacity of this ArraRotatingArrayList 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)
          Returns the element at the specified position in this list.
 java.lang.Object getFirst()
          Returns the element at the front of the list.
 java.lang.Object getLast()
          Returns the element at the back of the list.
 int indexOf(java.lang.Object elem)
          Searches for the first occurence of the given argument, testing for equality using the equals method.
 boolean isEmpty()
          Tests if this list has no elements.
 int lastIndexOf(java.lang.Object elem)
          Returns the index of the last occurrence of the specified object in this list.
private  void RangeCheck(int index)
          Check if the given index is in range.
private  void readObject(java.io.ObjectInputStream s)
          Reconstitute the RotatingArrayList instance from a stream (that is, deserialize it).
 java.lang.Object remove(int index)
          Removes the element at the specified position in this list.
 java.lang.Object removeFirst()
          Removes the element from the front of this list.
 void removeFirst(int num)
          Removes from this List the first num elements.
 java.lang.Object removeLast()
          Removes the element from the back of this list.
 void removeLast(int num)
          Removes from this List the last num elements.
 void removeRange(int fromIndex, int toIndex)
          Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.
private  int rotateIndex_(int index)
          Private helper.
 java.lang.Object set(int index, java.lang.Object element)
          Replaces the element at the specified position in this list with the specified element.
 java.lang.Object setFirst(java.lang.Object element)
          Replaces the element at the front of this list with the specified element.
 java.lang.Object setLast(java.lang.Object element)
          Replaces the element at the back of this list with the specified element.
 int size()
          Returns the number of elements in this list.
 java.lang.Object[] toArray()
          Returns an array containing all of the elements in this list in the correct order.
 java.lang.Object[] toArray(java.lang.Object[] a)
          Returns an array containing all of the elements in this list in the correct order.
 void trimToSize()
          Trims the capacity of this RotatingArrayList instance to be the list's current size.
private  void writeObject(java.io.ObjectOutputStream s)
          Save the state of the RotatingArrayList instance to a stream (that is, serialize it).
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, remove, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, remove, removeAll, retainAll, subList
 

Field Detail

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 RotatingArrayList (the number of elements it contains).


first_

private transient int first_
Array index of the first element.


last_

private transient int last_
Array index of the last element, plus 1. But it wraps around, so it is never == elementData_.length.

Constructor Detail

RotatingArrayList

public RotatingArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.


RotatingArrayList

public RotatingArrayList()
Constructs an empty list.


RotatingArrayList

public RotatingArrayList(java.util.Collection c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. The RotatingArrayList instance has an initial capacity of 110% the size of the specified collection.

Method Detail

rotateIndex_

private int rotateIndex_(int index)
Private helper.


copyIntoArray_

private void copyIntoArray_(java.lang.Object[] aArray)
Copies list into a given array


trimToSize

public void trimToSize()
Trims the capacity of this RotatingArrayList instance to be the list's current size. An application can use this operation to minimize the storage of a RotatingArrayList instance.


ensureCapacity

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


size

public int size()
Returns the number of elements in this list.

Specified by:
size in interface java.util.List

isEmpty

public boolean isEmpty()
Tests if this list has no elements.

Specified by:
isEmpty in interface java.util.List

contains

public boolean contains(java.lang.Object elem)
Returns true if this list contains the specified element.

Specified by:
contains in interface java.util.List

indexOf

public int indexOf(java.lang.Object elem)
Searches for the first occurence of the given argument, testing for equality using the equals method.

Specified by:
indexOf in interface java.util.List

lastIndexOf

public int lastIndexOf(java.lang.Object elem)
Returns the index of the last occurrence of the specified object in this list.

Specified by:
lastIndexOf in interface java.util.List

clone

public java.lang.Object clone()
Returns a shallow copy of this RotatingArrayList instance. (The elements themselves are not copied.)


toArray

public java.lang.Object[] toArray()
Returns an array containing all of the elements in this list in the correct order.

Specified by:
toArray in interface java.util.List

toArray

public java.lang.Object[] toArray(java.lang.Object[] a)
Returns 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.

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.

Specified by:
toArray in interface java.util.List

get

public java.lang.Object get(int index)
Returns the element at the specified position in this list.

Specified by:
get in interface java.util.List

getFirst

public java.lang.Object getFirst()
Returns the element at the front of the list.


getLast

public java.lang.Object getLast()
Returns the element at the back of the list.


set

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

Specified by:
set in interface java.util.List

setFirst

public java.lang.Object setFirst(java.lang.Object element)
Replaces the element at the front of this list with the specified element.


setLast

public java.lang.Object setLast(java.lang.Object element)
Replaces the element at the back of this list with the specified element.


add

public boolean add(java.lang.Object o)
Appends the specified element to the end of this list.

Specified by:
add in interface java.util.List

addLast

public boolean addLast(java.lang.Object o)
Appends the specified element to the back of this list.


addFirst

public boolean addFirst(java.lang.Object o)
Appends the specified element to the front of this list.


add

public void add(int index,
                java.lang.Object element)
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Specified by:
add in interface java.util.List

removeFirst

public java.lang.Object removeFirst()
Removes the element from the front of this list. Shifts any subsequent elements to the left (subtracts one from their indices).


removeLast

public java.lang.Object removeLast()
Removes the element from the back of this list.


remove

public java.lang.Object remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Specified by:
remove in interface java.util.List

clear

public void clear()
Removes all of the elements from this list. The list will be empty after this call returns.

Specified by:
clear in interface java.util.List

addAll

public boolean addAll(java.util.Collection c)
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. The behavior of this operation is undefined if the specified Collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this list, and this list is nonempty.)

Specified by:
addAll in interface java.util.List

addAllLast

public boolean addAllLast(java.util.Collection c)
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. The behavior of this operation is undefined if the specified Collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this list, and this list is nonempty.)


addAllFirst

public boolean addAllFirst(java.util.Collection c)
Appends all of the elements in the specified Collection to the beginning of this list, in the order that they are returned by the specified Collection's Iterator. The behavior of this operation is undefined if the specified Collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this list, and this list is nonempty.)


addAll

public boolean addAll(int index,
                      java.util.Collection c)
Inserts all of the elements in the specified Collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified Collection's iterator.

Specified by:
addAll in interface java.util.List

removeLast

public void removeLast(int num)
Removes from this List the last num elements. This call shortens the list by num elements. (If num==0, this operation has no effect.)


removeFirst

public void removeFirst(int num)
Removes from this List the first num elements. Shifts all remaining elements to the left (reduces their index). This call shortens the list by num elements. (If num==0, this operation has no effect.)


removeRange

public void removeRange(int fromIndex,
                        int toIndex)
Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by (toIndex - fromIndex) elements. (If toIndex<=fromIndex, this operation has no effect.)

AbstractList.removeRange() is protected, but RotatingArrayList makes this method public since it may be useful to clients.


RangeCheck

private void RangeCheck(int index)
Check if the given index is in range. If not, throw an appropriate runtime exception.


writeObject

private void writeObject(java.io.ObjectOutputStream s)
                  throws java.io.IOException
Save the state of the RotatingArrayList instance to a stream (that is, serialize it).


readObject

private void readObject(java.io.ObjectInputStream s)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
Reconstitute the RotatingArrayList instance from a stream (that is, deserialize it).