Class ArrayUtil

java.lang.Object
com.pnfsoftware.jeb.util.collect.ArrayUtil

public class ArrayUtil extends Object
Utility methods for arrays.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final boolean[]
     
    static final byte[]
     
    static final char[]
     
    static final double[]
     
    static final float[]
     
    static final int[]
     
    static final long[]
     
    static final Object[]
    An empty immutable Object[] array.
    static final short[]
     
    static final String[]
    An empty immutable String[] array.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static List<Boolean>
    asList(boolean[] array)
    A variant of Arrays.asList(Object...) that accepts primitives.
    static List<Byte>
    asList(byte[] array)
    A variant of Arrays.asList(Object...) that accepts primitives.
    static List<Character>
    asList(char[] array)
    A variant of Arrays.asList(Object...) that accepts primitives.
    static List<Double>
    asList(double[] array)
    A variant of Arrays.asList(Object...) that accepts primitives.
    static List<Float>
    asList(float[] array)
    A variant of Arrays.asList(Object...) that accepts primitives.
    static List<Integer>
    asList(int[] array)
    A variant of Arrays.asList(Object...) that accepts primitives.
    static List<Long>
    asList(long[] array)
    A variant of Arrays.asList(Object...) that accepts primitives.
    static List<Short>
    asList(short[] array)
    A variant of Arrays.asList(Object...) that accepts primitives.
    static <T> List<T>
    asView(T[] a)
    Provide a continuous read-only view of an array.
    static <T> List<T>
    asView(T[]... arrays)
    Provide a continuous read-only view of an array of arrays.
    static <T> List<T>
    asView(T[] a, T[] b)
    Provide a continuous read-only view of two arrays.
    static int[]
    bytesToIntegers(byte[] input)
    Convert a byte array to an int array with the same exact values (that means the bytes are sign extended).
    static void
    checkOffsetAndCount(int arrayLength, int offset, int count)
     
    static int
    compareBytes(byte[] array1, int pos1, byte[] array2, int pos2, int size)
    Compare arrays of bytes.
    static void
    copyBytes(byte[] dst, int dstOffset, byte[] src, int srcOffset, int size)
    Wrapper around Java's arraycopy().
    static boolean
    equalsBytes(byte[] array1, int pos1, byte[] array2, int pos2, int size)
    An extended version of Arrays.equals(boolean[], boolean[]).
    static int
    find(boolean[] array, boolean value)
     
    static int
    find(byte[] array, byte value)
     
    static int
    find(char[] array, char value)
     
    static int
    find(double[] array, double value)
     
    static int
    find(float[] array, float value)
     
    static int
    find(int[] array, int value)
     
    static int
    find(long[] array, long value)
     
    static int
    find(short[] array, short value)
     
    static int
    findByEquality(Object[] array, Object value)
     
    static int
    findByReference(Object[] array, Object value)
     
    static <T> T
    getSafe(T[] array, int index, T defaultValue)
    Retrieve a value from an array, or default value if index is out of bound or value from array is null.
    static <T> T
    getSafe2(T[][] array, int index, int index2, T defaultValue)
    Retrieve a value from a 2 dimension array, or default value if index is out of bound (from any array) or value from array is null.
    static <T> T
    getSafe3(T[][][] array, int index, int index2, int index3, T defaultValue)
     
    static byte[]
    integersToBytes(int[] input)
    Convert an int array into a byte array by casting all integers to bytes.
    static boolean
    isSled(byte[] array, byte value)
    Check if an array contains a same repeated value.
    static void
    swap(boolean[] array)
    Flip the order of an array of booleans.
    static void
    swap(boolean[] array, int offset, int size)
    Flip the order of an array of booleans.
    static void
    swap(byte[] array)
    Flip the order of an array of bytes.
    static void
    swap(byte[] array, int offset, int size)
    Flip the order of an array of bytes.
    static void
    swap(char[] array)
    Flip the order of an array of chars.
    static void
    swap(char[] array, int offset, int size)
    Flip the order of an array of chars.
    static void
    swap(double[] array)
    Flip the order of an array of doubles.
    static void
    swap(double[] array, int offset, int size)
    Flip the order of an array of doubles.
    static void
    swap(float[] array)
    Flip the order of an array of floats.
    static void
    swap(float[] array, int offset, int size)
    Flip the order of an array of floats.
    static void
    swap(int[] array)
    Flip the order of an array of ints.
    static void
    swap(int[] array, int offset, int size)
    Flip the order of an array of ints.
    static void
    swap(long[] array)
    Flip the order of an array of longs.
    static void
    swap(long[] array, int offset, int size)
    Flip the order of an array of longs.
    static void
    swap(short[] array)
    Flip the order of an array of shorts.
    static void
    swap(short[] array, int offset, int size)
    Flip the order of an array of shorts.
    static <T> void
    swap(T[] array)
    Flip the order of an array of generic objects.
    static <T> void
    swap(T[] array, int offset, int size)
    Flip the order of an array of generic objects.
    static int[]
    Convert an array of unsigned bytes to an array of integers.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • NO_OBJECT

      public static final Object[] NO_OBJECT
      An empty immutable Object[] array.
    • NO_STRING

      public static final String[] NO_STRING
      An empty immutable String[] array.
    • NO_BOOLEAN

      public static final boolean[] NO_BOOLEAN
    • NO_BYTE

      public static final byte[] NO_BYTE
    • NO_CHAR

      public static final char[] NO_CHAR
    • NO_SHORT

      public static final short[] NO_SHORT
    • NO_INT

      public static final int[] NO_INT
    • NO_LONG

      public static final long[] NO_LONG
    • NO_FLOAT

      public static final float[] NO_FLOAT
    • NO_DOUBLE

      public static final double[] NO_DOUBLE
  • Constructor Details

    • ArrayUtil

      public ArrayUtil()
  • Method Details

    • copyBytes

      public static void copyBytes(byte[] dst, int dstOffset, byte[] src, int srcOffset, int size)
      Wrapper around Java's arraycopy().

      Copy bytes from a source to a destination. If the destination and source arrays are the same, potential overlaps are properly handled. This method does not validate bounds or arguments: NullPointerException and ArrayIndexOutOfBoundsException can be raised.

      Parameters:
      dst - destination array
      dstOffset - start offset in the destination array
      src - source array source array, potentially the same as the destination array
      srcOffset - start offset in the source array
      size - number of bytes to be copied
    • bytesToIntegers

      public static int[] bytesToIntegers(byte[] input)
      Convert a byte array to an int array with the same exact values (that means the bytes are sign extended).
      Parameters:
      input - byte array
      Returns:
      int array
    • unsignedBytesToIntegers

      public static int[] unsignedBytesToIntegers(byte[] input)
      Convert an array of unsigned bytes to an array of integers.
      Parameters:
      input - byte array, for which the bytes are treated as unsigned
      Returns:
      int array
    • integersToBytes

      public static byte[] integersToBytes(int[] input)
      Convert an int array into a byte array by casting all integers to bytes.
      Parameters:
      input - int array
      Returns:
      byte array
    • isSled

      public static boolean isSled(byte[] array, byte value)
      Check if an array contains a same repeated value.
      Parameters:
      array - the array to be checked
      value - the sled value
      Returns:
      true if all bytes in the array are equal to the provided value
    • compareBytes

      public static int compareBytes(byte[] array1, int pos1, byte[] array2, int pos2, int size)
      Compare arrays of bytes. This method raises an ArrayIndexOutOfBoundsException exception if either one of the input arrays does not contain enough bytes to be compared.
      Parameters:
      array1 - first array
      pos1 - where to start in the first array
      array2 - second array
      pos2 - where to start in the second array
      size - number of bytes to be compared
      Returns:
      standard -1/0/+1 comparison result
    • equalsBytes

      public static boolean equalsBytes(byte[] array1, int pos1, byte[] array2, int pos2, int size)
      An extended version of Arrays.equals(boolean[], boolean[]).
      Parameters:
      array1 -
      pos1 -
      array2 -
      pos2 -
      size -
      Returns:
    • asList

      public static List<Boolean> asList(boolean[] array)
      A variant of Arrays.asList(Object...) that accepts primitives.
      Parameters:
      array -
      Returns:
    • asList

      public static List<Byte> asList(byte[] array)
      A variant of Arrays.asList(Object...) that accepts primitives.
      Parameters:
      array -
      Returns:
    • asList

      public static List<Short> asList(short[] array)
      A variant of Arrays.asList(Object...) that accepts primitives.
      Parameters:
      array -
      Returns:
    • asList

      public static List<Character> asList(char[] array)
      A variant of Arrays.asList(Object...) that accepts primitives.
      Parameters:
      array -
      Returns:
    • asList

      public static List<Integer> asList(int[] array)
      A variant of Arrays.asList(Object...) that accepts primitives.
      Parameters:
      array -
      Returns:
    • asList

      public static List<Long> asList(long[] array)
      A variant of Arrays.asList(Object...) that accepts primitives.
      Parameters:
      array -
      Returns:
    • asList

      public static List<Float> asList(float[] array)
      A variant of Arrays.asList(Object...) that accepts primitives.
      Parameters:
      array -
      Returns:
    • asList

      public static List<Double> asList(double[] array)
      A variant of Arrays.asList(Object...) that accepts primitives.
      Parameters:
      array -
      Returns:
    • swap

      public static void swap(boolean[] array, int offset, int size)
      Flip the order of an array of booleans.
    • swap

      public static void swap(boolean[] array)
      Flip the order of an array of booleans.
    • swap

      public static void swap(byte[] array, int offset, int size)
      Flip the order of an array of bytes.
    • swap

      public static void swap(byte[] array)
      Flip the order of an array of bytes.
    • swap

      public static void swap(short[] array, int offset, int size)
      Flip the order of an array of shorts.
    • swap

      public static void swap(short[] array)
      Flip the order of an array of shorts.
    • swap

      public static void swap(char[] array, int offset, int size)
      Flip the order of an array of chars.
    • swap

      public static void swap(char[] array)
      Flip the order of an array of chars.
    • swap

      public static void swap(int[] array, int offset, int size)
      Flip the order of an array of ints.
    • swap

      public static void swap(int[] array)
      Flip the order of an array of ints.
    • swap

      public static void swap(long[] array, int offset, int size)
      Flip the order of an array of longs.
    • swap

      public static void swap(long[] array)
      Flip the order of an array of longs.
    • swap

      public static void swap(float[] array, int offset, int size)
      Flip the order of an array of floats.
    • swap

      public static void swap(float[] array)
      Flip the order of an array of floats.
    • swap

      public static void swap(double[] array, int offset, int size)
      Flip the order of an array of doubles.
    • swap

      public static void swap(double[] array)
      Flip the order of an array of doubles.
    • swap

      public static <T> void swap(T[] array, int offset, int size)
      Flip the order of an array of generic objects.
    • swap

      public static <T> void swap(T[] array)
      Flip the order of an array of generic objects.
    • find

      public static int find(boolean[] array, boolean value)
    • find

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

      public static int find(char[] array, char value)
    • find

      public static int find(short[] array, short value)
    • find

      public static int find(int[] array, int value)
    • find

      public static int find(long[] array, long value)
    • find

      public static int find(float[] array, float value)
    • find

      public static int find(double[] array, double value)
    • findByReference

      public static int findByReference(Object[] array, Object value)
    • findByEquality

      public static int findByEquality(Object[] array, Object value)
    • checkOffsetAndCount

      public static void checkOffsetAndCount(int arrayLength, int offset, int count)
    • getSafe

      public static <T> T getSafe(T[] array, int index, T defaultValue)
      Retrieve a value from an array, or default value if index is out of bound or value from array is null.
      Type Parameters:
      T - Any Object
      Parameters:
      array - the array
      index - the index of the array
      defaultValue - default value to return in case of error
      Returns:
      the element of the array at the index or the default value
    • getSafe2

      public static <T> T getSafe2(T[][] array, int index, int index2, T defaultValue)
      Retrieve a value from a 2 dimension array, or default value if index is out of bound (from any array) or value from array is null. This is an equivalent of getSafe(getSafe(array, index, new T[]{}), index2, default)
      Type Parameters:
      T - Any Object
      Parameters:
      array - the array
      index - the index of the first array
      index2 - the index of the second array
      defaultValue - default value to return in case of error
      Returns:
      the element of the array at the index or the default value
    • getSafe3

      public static <T> T getSafe3(T[][][] array, int index, int index2, int index3, T defaultValue)
    • asView

      public static <T> List<T> asView(T[] a)
      Provide a continuous read-only view of an array.
      Type Parameters:
      T -
      Parameters:
      an - array (non-null)
      Returns:
      a read-only list consisting of the elements of the first array
    • asView

      public static <T> List<T> asView(T[] a, T[] b)
      Provide a continuous read-only view of two arrays.
      Type Parameters:
      T -
      Parameters:
      a - first array (non-null)
      b - second array (non-null)
      Returns:
      a read-only list consisting of the elements of the first array, followed by the elements of the second array
    • asView

      @SafeVarargs public static <T> List<T> asView(T[]... arrays)
      Provide a continuous read-only view of an array of arrays. This method is well suited for 3+ arrays.
      Type Parameters:
      T -
      Parameters:
      arrays - the arrays
      Returns:
      a read-only list consisting of the elements of the first array, followed by the elements of the second array, etc.