de.grogra.xl.util
Class ObjectList<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by de.grogra.xl.util.ObjectList<E>
All Implemented Interfaces:
ObjectConsumer<E>, VoidToObjectGenerator<E>, java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>, java.util.RandomAccess
Direct Known Subclasses:
EventSupport.Queue, TreeIterator

public class ObjectList<E>
extends java.util.AbstractList<E>
implements java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable, ObjectConsumer<E>, VoidToObjectGenerator<E>

A ObjectList represents a list of Object values. It provides list- and stack-oriented methods for insertion, addition, and removal, of values. The methods are not thread-safe.

Author:
Ole Kniemeyer
See Also:
Serialized Form

Field Summary
 java.lang.Object[] elements
          The array holding the elements.
 int size
          The size of this list.
 boolean useEquals
          Determines whether comparisons between objects should be based on the equals-method or on the equality operator ==.
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
ObjectList()
          Constructs a new ObjectList.
ObjectList(E[] elements)
          Constructs a new ObjectList whose elements are a copy of elements.
ObjectList(int capacity)
          Constructs a new ObjectList with a given initial capacity.
ObjectList(int capacity, boolean useEquals)
          Constructs a new ObjectList with a given initial capacity.
 
Method Summary
 boolean add(E o)
          Adds o as last element to this list.
 void add(int index, E o)
          Inserts o at position index to this list.
 boolean addAll(java.util.Collection<? extends E> v)
          Appends all elements of v to this list.
 ObjectList<E> addAll(java.lang.Object[] v, int begin, int length)
          Appends length components of v to this list, starting at index begin.
 ObjectList<E> addAll(ObjectList<? extends E> v)
          Appends all elements of v to this list.
 void addIfNotContained(E o)
          Adds o as last element to this list if is not yet contained as reported by contains(Object).
 void addInOrder(java.lang.Comparable<? super E> o)
          Inserts o into this ordered list.
 void addInOrder(E o, java.util.Comparator<? super E> c)
          Inserts o into this ordered list, based on c.
static void arraycopy(java.lang.Object[] src, int srcIndex, java.lang.Object[] dest, int destIndex, int length)
           
 void clear()
          Removes all of the elements from this list.
static void clear(java.lang.Object[] array, int index, int length)
           
 java.lang.Object clone()
           
 void consume(E value)
          Receives a value of type T.
 boolean contains(java.lang.Object o)
          Returns true iff this list contains the given element o.
 void ensureCapacity(int capacity)
          Ensures a capacity of the internal array of at least capacity.
 boolean equals(java.lang.Object o)
           
 void evaluateObject(ObjectConsumer<? super E> cons)
          This generator method yields all values to cons.
 E get(int index)
          Returns the list element at index.
 int hashCode()
           
 int indexOf(java.lang.Object o)
          Returns the index of o in this list.
 boolean isEmpty()
          Returns if this list is empty, i.e., if its size is zero.
 int lastIndexOf(java.lang.Object o)
          Returns the last index of o in this list.
 E peek(int index)
          Returns the list element at index as seen from the top, i.e., at absolute position size - index.
 E pop()
          Removes and returns the object at the top of this list.
 ObjectList<E> push(E o)
          Pushes o on top of this list, i.e., as last element.
 ObjectList<E> push(E o1, E o2)
          Pushes o1 and o2 on top of this list, i.e., as last elements.
 ObjectList<E> push(E o1, E o2, E o3)
          Pushes o1 ... o3 on top of this list, i.e., as last elements.
 E remove(int index)
          Removes the element at position index.
 boolean remove(java.lang.Object o)
          Removes the element o.
 E removeAt(int index)
          Removes the element at position index.
 E set(int index, E o)
          Sets the element at position index to o.
 void setSize(int size)
          Sets the size of this list to the given value.
 int size()
          Returns the size of this list.
 java.lang.Object[] toArray()
          Returns an array containing the elements of this list.
<T> T[]
toArray(T[] array)
          Returns an array containing the elements of this list.
 void trimToSize()
          Trims the capacity of this list to be its current size.
 void values(ObjectConsumer<? super E> cons)
          This method is an alias for evaluateObject(de.grogra.xl.lang.ObjectConsumer).
 
Methods inherited from class java.util.AbstractList
addAll, iterator, listIterator, listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, 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, removeAll, retainAll
 

Field Detail

elements

public transient java.lang.Object[] elements
The array holding the elements. Only the components from 0 to size - 1 are valid. Direct operation on this array has to be designed carefully to avoid inconsistencies.


size

public int size
The size of this list. Direct operation on this field has to be designed carefully to avoid inconsistencies.


useEquals

public boolean useEquals
Determines whether comparisons between objects should be based on the equals-method or on the equality operator ==.

Constructor Detail

ObjectList

public ObjectList()
Constructs a new ObjectList.


ObjectList

public ObjectList(E[] elements)
Constructs a new ObjectList whose elements are a copy of elements.

Parameters:
elements - the initial elements of the list

ObjectList

public ObjectList(int capacity)
Constructs a new ObjectList with a given initial capacity.

Parameters:
capacity - the initial capacity

ObjectList

public ObjectList(int capacity,
                  boolean useEquals)
Constructs a new ObjectList with a given initial capacity.

Parameters:
capacity - the initial capacity
useEquals - the value for useEquals
Method Detail

add

public boolean add(E o)
Adds o as last element to this list.

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.List<E>
Overrides:
add in class java.util.AbstractList<E>
Parameters:
o - the value to add
Returns:
true

add

public void add(int index,
                E o)
Inserts o at position index to this list. If index is not less than size, the list is enlarged and filled with null-values before.

Specified by:
add in interface java.util.List<E>
Overrides:
add in class java.util.AbstractList<E>
Parameters:
index - the insert position
o - the value to insert

addAll

public boolean addAll(java.util.Collection<? extends E> v)
Appends all elements of v to this list.

Specified by:
addAll in interface java.util.Collection<E>
Specified by:
addAll in interface java.util.List<E>
Overrides:
addAll in class java.util.AbstractCollection<E>
Parameters:
v - the list of elements to add
Returns:
true iff this list changed as a result of the invocation

addAll

public ObjectList<E> addAll(java.lang.Object[] v,
                            int begin,
                            int length)
Appends length components of v to this list, starting at index begin.

Parameters:
v - the array of elements to add
begin - the array index to begin with
length - the number of elements to add
Returns:
this list

addAll

public ObjectList<E> addAll(ObjectList<? extends E> v)
Appends all elements of v to this list.

Parameters:
v - the list of elements to add
Returns:
this list

addIfNotContained

public void addIfNotContained(E o)
Adds o as last element to this list if is not yet contained as reported by contains(Object).

Parameters:
o - the value to add

addInOrder

public void addInOrder(java.lang.Comparable<? super E> o)
Inserts o into this ordered list. This list has to be sorted in ascending order as defined by o. o will then be added at a position according to this order.

Parameters:
o - the value to add

addInOrder

public void addInOrder(E o,
                       java.util.Comparator<? super E> c)
Inserts o into this ordered list, based on c. This list has to be sorted in ascending order as defined by c. o will then be added at a position according to this order.

Parameters:
o - the value to add
c - the comparator which defines the order

arraycopy

public static void arraycopy(java.lang.Object[] src,
                             int srcIndex,
                             java.lang.Object[] dest,
                             int destIndex,
                             int length)

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.Collection<E>
Specified by:
clear in interface java.util.List<E>
Overrides:
clear in class java.util.AbstractList<E>

clear

public static void clear(java.lang.Object[] array,
                         int index,
                         int length)

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

consume

public void consume(E value)
Description copied from interface: ObjectConsumer
Receives a value of type T.

Specified by:
consume in interface ObjectConsumer<E>

contains

public boolean contains(java.lang.Object o)
Returns true iff this list contains the given element o.

Specified by:
contains in interface java.util.Collection<E>
Specified by:
contains in interface java.util.List<E>
Overrides:
contains in class java.util.AbstractCollection<E>
Parameters:
o - a value
Returns:
true iff o is contained

ensureCapacity

public void ensureCapacity(int capacity)
Ensures a capacity of the internal array of at least capacity.

Parameters:
capacity - the desired minimum capacity

equals

public boolean equals(java.lang.Object o)
Specified by:
equals in interface java.util.Collection<E>
Specified by:
equals in interface java.util.List<E>
Overrides:
equals in class java.util.AbstractList<E>

evaluateObject

public void evaluateObject(ObjectConsumer<? super E> cons)
This generator method yields all values to cons.

Specified by:
evaluateObject in interface VoidToObjectGenerator<E>
Parameters:
cons - the consumer which receives the values

get

public E get(int index)
Returns the list element at index. If index is not less than size, null is returned.

Specified by:
get in interface java.util.List<E>
Specified by:
get in class java.util.AbstractList<E>
Parameters:
index - the position
Returns:
the value at index

hashCode

public int hashCode()
Specified by:
hashCode in interface java.util.Collection<E>
Specified by:
hashCode in interface java.util.List<E>
Overrides:
hashCode in class java.util.AbstractList<E>

indexOf

public int indexOf(java.lang.Object o)
Returns the index of o in this list.

Specified by:
indexOf in interface java.util.List<E>
Overrides:
indexOf in class java.util.AbstractList<E>
Parameters:
o - a value
Returns:
the index of o, or -1 of o is not contained

isEmpty

public final boolean isEmpty()
Returns if this list is empty, i.e., if its size is zero.

Specified by:
isEmpty in interface java.util.Collection<E>
Specified by:
isEmpty in interface java.util.List<E>
Overrides:
isEmpty in class java.util.AbstractCollection<E>
Returns:
true iff this list is empty

lastIndexOf

public int lastIndexOf(java.lang.Object o)
Returns the last index of o in this list.

Specified by:
lastIndexOf in interface java.util.List<E>
Overrides:
lastIndexOf in class java.util.AbstractList<E>
Parameters:
o - a value
Returns:
the index of o, or -1 of o is not contained

peek

public E peek(int index)
Returns the list element at index as seen from the top, i.e., at absolute position size - index. Thus, the topmost element has index 1.

Parameters:
index - the position as seen from the top
Returns:
the value at that position

pop

public E pop()
Removes and returns the object at the top of this list.

Returns:
the removed object from the top of this list

push

public final ObjectList<E> push(E o)
Pushes o on top of this list, i.e., as last element.

Parameters:
o - the value to push
Returns:
this list

push

public final ObjectList<E> push(E o1,
                                E o2)
Pushes o1 and o2 on top of this list, i.e., as last elements. The effect is the same as the invocation list.push(o1).push(o2).

Parameters:
o1 - the first value to push
o2 - the second value to push
Returns:
this list

push

public final ObjectList<E> push(E o1,
                                E o2,
                                E o3)
Pushes o1 ... o3 on top of this list, i.e., as last elements. The effect is the same as the invocation list.push(o1).push(o2).push(o3).

Parameters:
o1 - the first value to push
o2 - the second value to push
o3 - the third value to push
Returns:
this list

remove

public E remove(int index)
Removes the element at position index.

Specified by:
remove in interface java.util.List<E>
Overrides:
remove in class java.util.AbstractList<E>
Parameters:
index - the position of the element to be removed
Returns:
the value of the removed element

remove

public boolean remove(java.lang.Object o)
Removes the element o. The last occurence of o in this list is removed.

Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.List<E>
Overrides:
remove in class java.util.AbstractCollection<E>
Parameters:
o - the element to be removed
Returns:
true iff o was found and removed from the list

removeAt

public E removeAt(int index)
Removes the element at position index.

Parameters:
index - the position of the element to be removed
Returns:
the value of the removed element

set

public E set(int index,
             E o)
Sets the element at position index to o. If index is not less than size, the list is enlarged and filled with null-values before.

Specified by:
set in interface java.util.List<E>
Overrides:
set in class java.util.AbstractList<E>
Parameters:
index - the position
o - the new value
Returns:
the old value at index

setSize

public void setSize(int size)
Sets the size of this list to the given value. If the new size is greater than the old size, the new elements are initialized with null-values.

Parameters:
size - the new size

size

public final int size()
Returns the size of this list.

Specified by:
size in interface java.util.Collection<E>
Specified by:
size in interface java.util.List<E>
Specified by:
size in class java.util.AbstractCollection<E>
Returns:
the size

toArray

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

Specified by:
toArray in interface java.util.Collection<E>
Specified by:
toArray in interface java.util.List<E>
Overrides:
toArray in class java.util.AbstractCollection<E>
Returns:
an array copy of this list

toArray

public <T> T[] toArray(T[] array)
Returns an array containing the elements of this list. The type of the returned array is that of the specified array. If this list fits in the specified array, it is returned therein. Otherwise, a new array is allocated whose length is the size of this list's size, the values of this list are copied into the new array, and this array is returned.

If there is room for an additional element in the array, a null-value is written behind the last copied element.

Specified by:
toArray in interface java.util.Collection<E>
Specified by:
toArray in interface java.util.List<E>
Overrides:
toArray in class java.util.AbstractCollection<E>
Parameters:
array - an array to use
Returns:
an array copy of this list

trimToSize

public void trimToSize()
Trims the capacity of this list to be its current size.


values

public void values(ObjectConsumer<? super E> cons)
This method is an alias for evaluateObject(de.grogra.xl.lang.ObjectConsumer).