Class ObjectArrayEncoding<E>

Type Parameters:
E - array element type
All Implemented Interfaces:
Encoding<E[]>, NaturalSortAware, Serializable, Comparator<E[]>

public class ObjectArrayEncoding<E> extends ArrayEncoding<E[],E>
Support superclass for non-null object array encodings.

Null values are not supported by this class and there is no default value. However the array elements can be null.

In the binary encoding, array elements are simply concatenated, with each element preceded by a 0x01 byte unless encoded array elements never start with 0x00. After the last array element, a final 0x00 is then appended. This encoding ensures lexicographic ordering (though there is a subtlety here, which is that when omitting the 0x01 bytes, we are relying on the fact that for any Encoding no encoded value can be a prefix of a another value, because that violates the self-delimiting requirement; as a result, it's not possible for two concatenated values [B,C] to sort before a single value [A] if A < B).

See Also:
  • Constructor Details

    • ObjectArrayEncoding

      public ObjectArrayEncoding(Encoding<E> elementEncoding)
  • Method Details

    • read

      public E[] read(ByteReader reader)
      Description copied from interface: Encoding
      Read a value from the given input.
      Parameters:
      reader - byte input
      Returns:
      decoded value (possibly null)
    • write

      public void write(ByteWriter writer, E[] array)
      Description copied from interface: Encoding
      Write a value to the given output.
      Parameters:
      writer - byte output
      array - value to write (possibly null)
    • skip

      public void skip(ByteReader reader)
      Description copied from interface: Encoding
      Read and discard a byte[] encoded value from the given input.

      If the value skipped over is invalid, this method may, but is not required to, throw IllegalArgumentException.

      If the value skipped over is truncated, this method must throw IndexOutOfBoundsException.

      Parameters:
      reader - byte input
    • hasPrefix0x00

      public boolean hasPrefix0x00()
      Description copied from interface: Encoding
      Determine whether any of this encoding's encoded values start with a 0x00 byte. Certain optimizations are possible when this is not the case. It is safe for this method to always return true.

      Note: changing the result of this method may result in an incompatible encoding if this encoding is wrapped in another class.

      Returns:
      true if an encoded value starting with 0x00 exists
    • hasPrefix0xff

      public boolean hasPrefix0xff()
      Description copied from interface: Encoding
      Determine whether any of this encoding's encoded values start with a 0xff byte. Certain optimizations are possible when this is not the case. It is safe for this method to always return true.

      Note: changing the result of this method may result in an incompatible encoding if this encoding is wrapped in another class.

      Returns:
      true if an encoded value starting with 0xff exists
    • getArrayLength

      protected int getArrayLength(E[] array)
      Description copied from class: ArrayEncoding
      Get the length of the given array.
      Specified by:
      getArrayLength in class ArrayEncoding<E[],E>
      Parameters:
      array - non-null array
      Returns:
      array length
    • getArrayElement

      protected E getArrayElement(E[] array, int index)
      Description copied from class: ArrayEncoding
      Get an element from the given array.
      Specified by:
      getArrayElement in class ArrayEncoding<E[],E>
      Parameters:
      array - non-null array
      index - index of target element in array
      Returns:
      array element at index index
    • createArray

      protected E[] createArray(List<E> elements)
      Description copied from class: ArrayEncoding
      Create a new array instance containing the given elements.
      Specified by:
      createArray in class ArrayEncoding<E[],E>
      Parameters:
      elements - content for the new array
      Returns:
      newly created array