Module xl.core

Class ObjectList<E>

All Implemented Interfaces:
ObjectConsumer<E>, VoidToObjectGenerator<E>, Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
Direct Known Subclasses:
EventSupport.Queue, TreeIterator, Visitor2D

public class ObjectList<E> extends AbstractList<E> implements RandomAccess, Cloneable, 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:
  • Field Details

    • elements

      public transient 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 Details

    • ObjectList

      public ObjectList(int capacity)
      Constructs a new ObjectList with a given initial capacity.
      Parameters:
      capacity - the initial capacity
    • 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, boolean useEquals)
      Constructs a new ObjectList with a given initial capacity.
      Parameters:
      capacity - the initial capacity
      useEquals - the value for useEquals
  • Method Details

    • clone

      public Object clone()
      Overrides:
      clone in class Object
    • ensureCapacity

      public void ensureCapacity(int capacity)
      Ensures a capacity of the internal array of at least capacity.
      Parameters:
      capacity - the desired minimum capacity
    • trimToSize

      public void trimToSize()
      Trims the capacity of this list to be its current size.
    • 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
    • add

      public boolean add(E o)
      Adds o as last element to this list.
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface List<E>
      Overrides:
      add in class 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 List<E>
      Overrides:
      add in class AbstractList<E>
      Parameters:
      index - the insert position
      o - the value to insert
    • 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
    • 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
    • addAll

      public ObjectList<E> addAll(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
    • 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
    • remove

      public E remove(int index)
      Removes the element at position index.
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class AbstractList<E>
      Parameters:
      index - the position of the element to be removed
      Returns:
      the value of the removed element
    • remove

      public boolean remove(Object o)
      Removes the element o. The last occurence of o in this list is removed.
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class AbstractCollection<E>
      Parameters:
      o - the element to be removed
      Returns:
      true iff o was found and removed from the list
    • 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 List<E>
      Overrides:
      set in class AbstractList<E>
      Parameters:
      index - the position
      o - the new value
      Returns:
      the old value at index
    • 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 List<E>
      Specified by:
      get in class AbstractList<E>
      Parameters:
      index - the position
      Returns:
      the value at index
    • 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
    • contains

      public boolean contains(Object o)
      Returns true iff this list contains the given element o.
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface List<E>
      Overrides:
      contains in class AbstractCollection<E>
      Parameters:
      o - a value
      Returns:
      true iff o is contained
    • indexOf

      public int indexOf(Object o)
      Returns the index of o in this list.
      Specified by:
      indexOf in interface List<E>
      Overrides:
      indexOf in class AbstractList<E>
      Parameters:
      o - a value
      Returns:
      the index of o, or -1 of o is not contained
    • lastIndexOf

      public int lastIndexOf(Object o)
      Returns the last index of o in this list.
      Specified by:
      lastIndexOf in interface List<E>
      Overrides:
      lastIndexOf in class AbstractList<E>
      Parameters:
      o - a value
      Returns:
      the index of o, or -1 of o is not contained
    • 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 Collection<E>
      Specified by:
      clear in interface List<E>
      Overrides:
      clear in class AbstractList<E>
    • addInOrder

      public void addInOrder(E o, 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
    • addInOrder

      public void addInOrder(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
    • addAll

      public boolean addAll(Collection<? extends E> v)
      Appends all elements of v to this list.
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface List<E>
      Overrides:
      addAll in class AbstractCollection<E>
      Parameters:
      v - the list of elements to add
      Returns:
      true iff this list changed as a result of the invocation
    • arraycopy

      public static void arraycopy(Object[] src, int srcIndex, Object[] dest, int destIndex, int length)
    • clear

      public static void clear(Object[] array, int index, int length)
    • 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
    • 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 Collection<E>
      Specified by:
      size in interface List<E>
      Specified by:
      size in class AbstractCollection<E>
      Returns:
      the size
    • isEmpty

      public final boolean isEmpty()
      Returns if this list is empty, i.e., if its size is zero.
      Specified by:
      isEmpty in interface Collection<E>
      Specified by:
      isEmpty in interface List<E>
      Overrides:
      isEmpty in class AbstractCollection<E>
      Returns:
      true iff this list is empty
    • toArray

      public Object[] toArray()
      Returns an array containing the elements of this list.
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface List<E>
      Overrides:
      toArray in class 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 Collection<E>
      Specified by:
      toArray in interface List<E>
      Overrides:
      toArray in class AbstractCollection<E>
      Parameters:
      array - an array to use
      Returns:
      an array copy of this list
    • equals

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

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

      public void consume(E value)
      Description copied from interface: ObjectConsumer
      Receives a value of type T.
      Specified by:
      consume in interface ObjectConsumer<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
    • values

      public void values(ObjectConsumer<? super E> cons)