org.jagatoo.util.lists
Class ChainedList<E extends DoublyChainable<E>>

java.lang.Object
  extended by org.jagatoo.util.lists.ChainedList<E>
All Implemented Interfaces:
java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>, java.util.Queue<E>

public class ChainedList<E extends DoublyChainable<E>>
extends java.lang.Object
implements java.util.List<E>, java.util.Queue<E>

The ChainedList is a List implementation, that contains DoublyChainable objects. This way it produces zero garbage, while the LinkedList produces a lot of garbage and is therefore unusable for gaming stuff.


Constructor Summary
ChainedList()
           
ChainedList(java.util.Collection<? extends E> c)
          Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
 
Method Summary
 boolean add(E o)
          
 void add(int index, E element)
          
 boolean addAll(java.util.Collection<? extends E> c)
          
 boolean addAll(int index, java.util.Collection<? extends E> c)
          
 void addHead(E o)
          Inserts the given element at the beginning of this list.
 void append(E o)
          Appends the given element to the end of this list.
 void clear()
          
 ChainedList<E> clone()
          Returns a shallow copy of this ChainedList.
 boolean contains(java.lang.Object o)
          
 boolean containsAll(java.util.Collection<?> c)
          
 E element()
          
 E get(int index)
          
 E getHead()
          Returns the head of this ChainedList.
 E getTail()
          Returns the tail of this ChainedList.
 int indexOf(java.lang.Object o)
          
 boolean isEmpty()
          
 java.util.Iterator<E> iterator()
          
 int lastIndexOf(java.lang.Object o)
          
 java.util.ListIterator<E> listIterator()
          
 java.util.ListIterator<E> listIterator(int index)
          
 boolean offer(E o)
          
 E peek()
          
 E poll()
          
 E remove()
          
 E remove(int index)
          
 boolean remove(java.lang.Object o)
          
 boolean removeAll(java.util.Collection<?> c)
          
 E removeHead()
          Removes and returns the first element from this list.
 E removeTail()
          Removes and returns the last element from this list.
 boolean retainAll(java.util.Collection<?> c)
          
 E set(int index, E element)
          
 int size()
          
 java.util.List<E> subList(int fromIndex, int toIndex)
          
 java.lang.Object[] toArray()
          Returns an array containing all of the elements in this list in the correct order.
<T> T[]
toArray(T[] 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.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.List
equals, hashCode
 

Constructor Detail

ChainedList

public ChainedList()

ChainedList

public ChainedList(java.util.Collection<? extends E> c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Parameters:
c - the collection whose elements are to be placed into this list.
Throws:
java.lang.NullPointerException - if the specified collection is null.
Method Detail

size

public int size()

Specified by:
size in interface java.util.Collection<E extends DoublyChainable<E>>
Specified by:
size in interface java.util.List<E extends DoublyChainable<E>>

isEmpty

public boolean isEmpty()

Specified by:
isEmpty in interface java.util.Collection<E extends DoublyChainable<E>>
Specified by:
isEmpty in interface java.util.List<E extends DoublyChainable<E>>

getHead

public E getHead()
Returns the head of this ChainedList.

Returns:
the first element in this list.

getTail

public E getTail()
Returns the tail of this ChainedList.

Returns:
the last element in this list.

removeHead

public E removeHead()
Removes and returns the first element from this list.

Returns:
the first element from this list.
Throws:
java.util.NoSuchElementException - if this list is empty.

removeTail

public E removeTail()
Removes and returns the last element from this list.

Returns:
the last element from this list.
Throws:
java.util.NoSuchElementException - if this list is empty.

addHead

public void addHead(E o)
Inserts the given element at the beginning of this list.

Parameters:
o - the element to be inserted at the beginning of this list.

append

public void append(E o)
Appends the given element to the end of this list. (Identical in function to the add method; included only for consistency.)

Parameters:
o - the element to be inserted at the end of this list.

add

public boolean add(E o)

Specified by:
add in interface java.util.Collection<E extends DoublyChainable<E>>
Specified by:
add in interface java.util.List<E extends DoublyChainable<E>>
Specified by:
add in interface java.util.Queue<E extends DoublyChainable<E>>

add

public void add(int index,
                E element)

Specified by:
add in interface java.util.List<E extends DoublyChainable<E>>

addAll

public boolean addAll(java.util.Collection<? extends E> c)

Specified by:
addAll in interface java.util.Collection<E extends DoublyChainable<E>>
Specified by:
addAll in interface java.util.List<E extends DoublyChainable<E>>

addAll

public boolean addAll(int index,
                      java.util.Collection<? extends E> c)

Specified by:
addAll in interface java.util.List<E extends DoublyChainable<E>>

remove

public boolean remove(java.lang.Object o)

Specified by:
remove in interface java.util.Collection<E extends DoublyChainable<E>>
Specified by:
remove in interface java.util.List<E extends DoublyChainable<E>>

remove

public E remove(int index)

Specified by:
remove in interface java.util.List<E extends DoublyChainable<E>>

removeAll

public boolean removeAll(java.util.Collection<?> c)

Specified by:
removeAll in interface java.util.Collection<E extends DoublyChainable<E>>
Specified by:
removeAll in interface java.util.List<E extends DoublyChainable<E>>

retainAll

public boolean retainAll(java.util.Collection<?> c)

Specified by:
retainAll in interface java.util.Collection<E extends DoublyChainable<E>>
Specified by:
retainAll in interface java.util.List<E extends DoublyChainable<E>>

set

public E set(int index,
             E element)

Specified by:
set in interface java.util.List<E extends DoublyChainable<E>>

get

public E get(int index)

Specified by:
get in interface java.util.List<E extends DoublyChainable<E>>

indexOf

public int indexOf(java.lang.Object o)

Specified by:
indexOf in interface java.util.List<E extends DoublyChainable<E>>

lastIndexOf

public int lastIndexOf(java.lang.Object o)

Specified by:
lastIndexOf in interface java.util.List<E extends DoublyChainable<E>>

clear

public void clear()

Specified by:
clear in interface java.util.Collection<E extends DoublyChainable<E>>
Specified by:
clear in interface java.util.List<E extends DoublyChainable<E>>

contains

public boolean contains(java.lang.Object o)

Specified by:
contains in interface java.util.Collection<E extends DoublyChainable<E>>
Specified by:
contains in interface java.util.List<E extends DoublyChainable<E>>

containsAll

public boolean containsAll(java.util.Collection<?> c)

Specified by:
containsAll in interface java.util.Collection<E extends DoublyChainable<E>>
Specified by:
containsAll in interface java.util.List<E extends DoublyChainable<E>>

subList

public java.util.List<E> subList(int fromIndex,
                                 int toIndex)

Specified by:
subList in interface java.util.List<E extends DoublyChainable<E>>

peek

public E peek()

Specified by:
peek in interface java.util.Queue<E extends DoublyChainable<E>>

element

public E element()

Specified by:
element in interface java.util.Queue<E extends DoublyChainable<E>>

poll

public E poll()

Specified by:
poll in interface java.util.Queue<E extends DoublyChainable<E>>

remove

public E remove()

Specified by:
remove in interface java.util.Queue<E extends DoublyChainable<E>>

offer

public boolean offer(E o)

Specified by:
offer in interface java.util.Queue<E extends DoublyChainable<E>>

listIterator

public java.util.ListIterator<E> listIterator(int index)

Specified by:
listIterator in interface java.util.List<E extends DoublyChainable<E>>

listIterator

public java.util.ListIterator<E> listIterator()

Specified by:
listIterator in interface java.util.List<E extends DoublyChainable<E>>

iterator

public java.util.Iterator<E> iterator()

Specified by:
iterator in interface java.lang.Iterable<E extends DoublyChainable<E>>
Specified by:
iterator in interface java.util.Collection<E extends DoublyChainable<E>>
Specified by:
iterator in interface java.util.List<E extends DoublyChainable<E>>

clone

public ChainedList<E> clone()
Returns a shallow copy of this ChainedList. (The elements themselves are not cloned.)

Overrides:
clone in class java.lang.Object
Returns:
a shallow copy of this ChainedList instance.

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.Collection<E extends DoublyChainable<E>>
Specified by:
toArray in interface java.util.List<E extends DoublyChainable<E>>
Returns:
an array containing all of the elements in this list in the correct order.

toArray

public <T> T[] toArray(T[] 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.Collection<E extends DoublyChainable<E>>
Specified by:
toArray in interface java.util.List<E extends DoublyChainable<E>>
Parameters:
a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of the list.
Throws:
java.lang.ArrayStoreException - if the runtime type of a is not a supertype of the runtime type of every element in this list.
java.lang.NullPointerException - if the specified array is null.