Module xl.core

Class ByteList

java.lang.Object
de.grogra.xl.util.ByteList
All Implemented Interfaces:
ByteConsumer, VoidToByteGenerator, Serializable, Cloneable

public class ByteList extends Object implements Cloneable, Serializable, ByteConsumer, VoidToByteGenerator
A ByteList represents a list of byte 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 Summary

    Fields
    Modifier and Type
    Field
    Description
    byte[]
    The array holding the elements.
    int
    The size of this list.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new ByteList.
    ByteList(byte[] elements)
    Constructs a new ByteList whose elements are a copy of elements.
    ByteList(int capacity)
    Constructs a new ByteList with a given initial capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(byte o)
    Adds o as last element to this list.
    void
    add(int index, byte o)
    Inserts o at position index to this list.
    addAll(byte[] v, int begin, int length)
    Appends length components of v to this list, starting at index begin.
    Appends all elements of v to this list.
    void
    Adds o as last element to this list if is not yet contained as reported by contains(byte).
    static void
    arraycopy(byte[] src, int srcIndex, byte[] dest, int destIndex, int length)
     
    int
    binarySearch(byte value)
    Searches this list for the specified value using the binary search algorithm.
    static int
    binarySearch(byte[] array, byte value)
     
    static int
    binarySearch(byte[] array, byte value, int fromIndex, int toIndex)
     
    void
    Removes all of the elements from this list.
    static void
    clear(byte[] array, int index, int length)
     
     
    void
    consume(byte value)
    Receives a value of type byte.
    boolean
    contains(byte 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
     
    void
    This generator method yields all values to cons.
    byte
    get(int index)
    Returns the list element at index.
    int
     
    int
    indexOf(byte o)
    Returns the index of o in this list.
    final boolean
    Returns if this list is empty, i.e., if its size is zero.
    int
    lastIndexOf(byte o)
    Returns the last index of o in this list.
    byte
    peek(int index)
    Returns the list element at index as seen from the top, i.e., at absolute position size - index.
    byte
    pop()
    Removes and returns the object at the top of this list.
    final ByteList
    push(byte o)
    Pushes o on top of this list, i.e., as last element.
    final ByteList
    push(byte o1, byte o2)
    Pushes o1 and o2 on top of this list, i.e., as last elements.
    final ByteList
    push(byte o1, byte o2, byte o3)
    Pushes o1 ... o3 on top of this list, i.e., as last elements.
    boolean
    remove(byte o)
    Removes the element o.
    byte
    removeAt(int index)
    Removes the element at position index.
    byte
    set(int index, byte o)
    Sets the element at position index to o.
    void
    setSize(int size)
    Sets the size of this list to the given value.
    final int
    Returns the size of this list.
    byte[]
    Returns an array containing the elements of this list.
    byte[]
    toArray(byte[] array)
    Returns an array containing the elements of this list.
     
    void
    Trims the capacity of this list to be its current size.
    void
    void
     

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • elements

      public transient byte[] 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.
  • Constructor Details

    • ByteList

      public ByteList(int capacity)
      Constructs a new ByteList with a given initial capacity.
      Parameters:
      capacity - the initial capacity
    • ByteList

      public ByteList()
      Constructs a new ByteList.
    • ByteList

      public ByteList(byte[] elements)
      Constructs a new ByteList whose elements are a copy of elements.
      Parameters:
      elements - the initial elements of the list
  • 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 ByteList push(byte 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 ByteList push(byte o1, byte 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 ByteList push(byte o1, byte o2, byte 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(byte o)
      Adds o as last element to this list.
      Parameters:
      o - the value to add
      Returns:
      true
    • add

      public void add(int index, byte o)
      Inserts o at position index to this list. If index is not less than size, the list is enlarged and filled with 0-values before.
      Parameters:
      index - the insert position
      o - the value to insert
    • addIfNotContained

      public void addIfNotContained(byte o)
      Adds o as last element to this list if is not yet contained as reported by contains(byte).
      Parameters:
      o - the value to add
    • addAll

      public ByteList addAll(ByteList v)
      Appends all elements of v to this list.
      Parameters:
      v - the list of elements to add
      Returns:
      this list
    • addAll

      public ByteList addAll(byte[] 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 byte 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 boolean remove(byte o)
      Removes the element o. The last occurence of o in this list is removed.
      Parameters:
      o - the element to be removed
      Returns:
      true iff o was found and removed from the list
    • set

      public byte set(int index, byte o)
      Sets the element at position index to o. If index is not less than size, the list is enlarged and filled with 0-values before.
      Parameters:
      index - the position
      o - the new value
      Returns:
      the old value at index
    • get

      public byte get(int index)
      Returns the list element at index. If index is not less than size, 0 is returned.
      Parameters:
      index - the position
      Returns:
      the value at index
    • peek

      public byte 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(byte o)
      Returns true iff this list contains the given element o.
      Parameters:
      o - a value
      Returns:
      true iff o is contained
    • indexOf

      public int indexOf(byte o)
      Returns the index of o in this list.
      Parameters:
      o - a value
      Returns:
      the index of o, or -1 of o is not contained
    • lastIndexOf

      public int lastIndexOf(byte o)
      Returns the last index of o in this list.
      Parameters:
      o - a value
      Returns:
      the index of o, or -1 of o is not contained
    • binarySearch

      public int binarySearch(byte value)
      Searches this list for the specified value using the binary search algorithm. This list has to be sorted in ascending order.
      Parameters:
      value - the value to be searched for
      Returns:
      index of the searched value, if it is contained in this list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the value would be inserted into the list: the index of the first element greater than the value, or size(), if all elements in the list are less than the specified value.
    • binarySearch

      public static int binarySearch(byte[] array, byte value)
    • binarySearch

      public static int binarySearch(byte[] array, byte value, int fromIndex, int toIndex)
    • writeTo

      public void writeTo(ByteBuffer out)
    • clear

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

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

      public static void clear(byte[] array, int index, int length)
    • pop

      public byte 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 0-values.
      Parameters:
      size - the new size
    • size

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

      public final boolean isEmpty()
      Returns if this list is empty, i.e., if its size is zero.
      Returns:
      true iff this list is empty
    • toArray

      public byte[] toArray()
      Returns an array containing the elements of this list.
      Returns:
      an array copy of this list
    • toArray

      public byte[] toArray(byte[] 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 0-value is written behind the last copied element.

      Parameters:
      array - an array to use
      Returns:
      an array copy of this list
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • consume

      public void consume(byte value)
      Description copied from interface: ByteConsumer
      Receives a value of type byte.
      Specified by:
      consume in interface ByteConsumer
    • evaluateByte

      public void evaluateByte(ByteConsumer cons)
      This generator method yields all values to cons.
      Specified by:
      evaluateByte in interface VoidToByteGenerator
      Parameters:
      cons - the consumer which receives the values
    • values

      public void values(ByteConsumer cons)