Class ByteList

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<byte[]>
io.crums.util.mem.ByteList
All Implemented Interfaces:
java.lang.Iterable<byte[]>, java.util.Collection<byte[]>, java.util.List<byte[]>, java.util.RandomAccess

public class ByteList
extends java.util.AbstractList<byte[]>
implements java.util.RandomAccess
A read-only, copy-on-read view of a List<byte[]>. Some caveats about breaking some corners of the List contract for sake of usability.
See Also:
equals(Object), contains(Object), indexOf(Object), lastIndexOf(Object)
  • Field Summary

    Fields
    Modifier and Type Field Description
    protected java.util.List<byte[]> source  

    Fields inherited from class java.util.AbstractList

    modCount
  • Constructor Summary

    Constructors
    Constructor Description
    ByteList​(java.util.List<byte[]> source)
    Creates an instance from the given source instance.
  • Method Summary

    Modifier and Type Method Description
    boolean contains​(java.lang.Object o)
    boolean equals​(java.lang.Object o)
    Equality here is implemented using Arrays.equals(byte[], byte[]).
    byte[] get​(int index)
    int hashCode()  
    int indexOf​(java.lang.Object o)
    int lastIndexOf​(java.lang.Object o)
    static ByteList newInstance​(byte[][] arrays)
    Creates a new instance.
    static ByteList newInstance​(byte[][] arrays, boolean copy)
    Creates a new instance.
    int size()  

    Methods inherited from class java.util.AbstractList

    add, add, addAll, clear, iterator, listIterator, listIterator, remove, removeRange, set, subList

    Methods inherited from class java.util.AbstractCollection

    addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.util.Collection

    parallelStream, removeIf, stream, toArray

    Methods inherited from interface java.lang.Iterable

    forEach

    Methods inherited from interface java.util.List

    addAll, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
  • Field Details

    • source

      protected final java.util.List<byte[]> source
  • Constructor Details

    • ByteList

      public ByteList​(java.util.List<byte[]> source)
      Creates an instance from the given source instance. Modifications in the source are visible in this view.
  • Method Details

    • newInstance

      public static ByteList newInstance​(byte[][] arrays)
      Creates a new instance. Defensively copies.
      See Also:
      newInstance(byte[][], boolean)
    • newInstance

      public static ByteList newInstance​(byte[][] arrays, boolean copy)
      Creates a new instance.
      Parameters:
      arrays - non-null source
      copy - if true then the array is defensively copied; if false then modifications in the sub arrays (byte[]) are visible in the new instance
      Returns:
      a List view of arrays.
    • get

      public byte[] get​(int index)
      Specified by:
      get in interface java.util.List<byte[]>
      Specified by:
      get in class java.util.AbstractList<byte[]>
      Returns:
      a copy of the item at the specified index
    • size

      public int size()
      Specified by:
      size in interface java.util.Collection<byte[]>
      Specified by:
      size in interface java.util.List<byte[]>
      Specified by:
      size in class java.util.AbstractCollection<byte[]>
    • contains

      public boolean contains​(java.lang.Object o)

      Note

      This implementation uses Arrays.equals(byte[], byte[]) in lieu of Object.equals(o), so in this sense, it breaks the above formal contract. Strictly, the contract for List.equals(Object) (and List.hashCode()) is also violated.

      Specified by:
      contains in interface java.util.Collection<byte[]>
      Specified by:
      contains in interface java.util.List<byte[]>
      Overrides:
      contains in class java.util.AbstractCollection<byte[]>
    • indexOf

      public int indexOf​(java.lang.Object o)

      Note

      This implementation uses Arrays.equals(byte[], byte[]) in lieu of Object.equals(o), so in this sense, it breaks the above formal contract. Strictly, the contract for List.equals(Object) (and List.hashCode()) is also violated.

      Specified by:
      indexOf in interface java.util.List<byte[]>
      Overrides:
      indexOf in class java.util.AbstractList<byte[]>
    • lastIndexOf

      public int lastIndexOf​(java.lang.Object o)

      Note

      This implementation uses Arrays.equals(byte[], byte[]) in lieu of Object.equals(o), so in this sense, it breaks the above formal contract. Strictly, the contract for List.equals(Object) (and List.hashCode()) is also violated.

      Specified by:
      lastIndexOf in interface java.util.List<byte[]>
      Overrides:
      lastIndexOf in class java.util.AbstractList<byte[]>
    • equals

      public final boolean equals​(java.lang.Object o)
      Equality here is implemented using Arrays.equals(byte[], byte[]). An instance of this class may only equal another instance of itself.

      Programmer's Note

      Technically, per the List.equals(Object) contract, this List should not equal any other List instance but itself. This is because Java array objects implement equality by reference (or rather, don't override Object.equals(Object)), and since this class returns a new byte array on each read, its elements equal no other.

      Generally, if no other class of object thinks it's equal to instances of your class, your class is free to redefine equality (the reflexitivity requirement). You usually only have one shot at it; it's a rare case where Object.equals(Object) can be overridden twice.

      Specified by:
      equals in interface java.util.Collection<byte[]>
      Specified by:
      equals in interface java.util.List<byte[]>
      Overrides:
      equals in class java.util.AbstractList<byte[]>
      See Also:
      hashCode()
    • hashCode

      public final int hashCode()
      Specified by:
      hashCode in interface java.util.Collection<byte[]>
      Specified by:
      hashCode in interface java.util.List<byte[]>
      Overrides:
      hashCode in class java.util.AbstractList<byte[]>
      Returns:
      a hash code consistent with equals(Object)