Package com.pnfsoftware.jeb.util.collect
Class ArrayUtil
java.lang.Object
com.pnfsoftware.jeb.util.collect.ArrayUtil
Utility methods for arrays.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final boolean[]An empty immutableboolean[]array.static final byte[]An empty immutablebyte[]array.static final char[]An empty immutablechar[]array.static final double[]An empty immutabledouble[]array.static final float[]An empty immutablefloat[]array.static final int[]An empty immutableint[]array.static final long[]An empty immutablelong[]array.static final Object[]An empty immutableObject[]array.static final short[]An empty immutableshort[]array.static final String[]An empty immutableString[]array. -
Method Summary
Modifier and TypeMethodDescriptionasList(boolean[] array) A variant ofArrays.asList(Object...)that accepts primitives.asList(byte[] array) A variant ofArrays.asList(Object...)that accepts primitives.asList(char[] array) A variant ofArrays.asList(Object...)that accepts primitives.asList(double[] array) A variant ofArrays.asList(Object...)that accepts primitives.asList(float[] array) A variant ofArrays.asList(Object...)that accepts primitives.asList(int[] array) A variant ofArrays.asList(Object...)that accepts primitives.asList(long[] array) A variant ofArrays.asList(Object...)that accepts primitives.asList(short[] array) A variant ofArrays.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 voidcheckOffsetAndCount(int arrayLength, int offset, int count) Validate an array sub-range.static intcompareBytes(byte[] array1, int pos1, byte[] array2, int pos2, int size) Compare arrays of bytes.static voidcopyBytes(byte[] dst, int dstOffset, byte[] src, int srcOffset, int size) Wrapper aroundJava's arraycopy().static booleanequalsBytes(byte[] array1, int pos1, byte[] array2, int pos2, int size) An extended version ofArrays.equals(boolean[], boolean[]).static intfind(boolean[] array, boolean value) Find the first index of a value in an array.static intfind(byte[] array, byte value) Find the first index of a value in an array.static intfind(char[] array, char value) Find the first index of a value in an array.static intfind(double[] array, double value) Find the first index of a value in an array.static intfind(float[] array, float value) Find the first index of a value in an array.static intfind(int[] array, int value) Find the first index of a value in an array.static intfind(long[] array, long value) Find the first index of a value in an array.static intfind(short[] array, short value) Find the first index of a value in an array.static intfindByEquality(Object[] array, Object value) Find the first index of an object byObject.equals(Object).static intfindByReference(Object[] array, Object value) Find the first index of an object by reference equality.static <T> TgetSafe(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> TgetSafe2(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> TgetSafe3(T[][][] array, int index, int index2, int index3, T defaultValue) Retrieve a value from a 3-dimensional array, or a default value if an index is out of bound or an intermediate array/value is null.static byte[]integersToBytes(int[] input) Convert an int array into a byte array by casting all integers to bytes.static booleanisSled(byte[] array, byte value) Check if an array contains a same repeated value.static voidswap(boolean[] array) Flip the order of an array of booleans.static voidswap(boolean[] array, int offset, int size) Flip the order of an array of booleans.static voidswap(byte[] array) Flip the order of an array of bytes.static voidswap(byte[] array, int offset, int size) Flip the order of an array of bytes.static voidswap(char[] array) Flip the order of an array of chars.static voidswap(char[] array, int offset, int size) Flip the order of an array of chars.static voidswap(double[] array) Flip the order of an array of doubles.static voidswap(double[] array, int offset, int size) Flip the order of an array of doubles.static voidswap(float[] array) Flip the order of an array of floats.static voidswap(float[] array, int offset, int size) Flip the order of an array of floats.static voidswap(int[] array) Flip the order of an array of ints.static voidswap(int[] array, int offset, int size) Flip the order of an array of ints.static voidswap(long[] array) Flip the order of an array of longs.static voidswap(long[] array, int offset, int size) Flip the order of an array of longs.static voidswap(short[] array) Flip the order of an array of shorts.static voidswap(short[] array, int offset, int size) Flip the order of an array of shorts.static <T> voidswap(T[] array) Flip the order of an array of generic objects.static <T> voidswap(T[] array, int offset, int size) Flip the order of an array of generic objects.static int[]unsignedBytesToIntegers(byte[] input) Convert an array of unsigned bytes to an array of integers.
-
Field Details
-
NO_OBJECT
An empty immutableObject[]array. -
NO_STRING
An empty immutableString[]array. -
NO_BOOLEAN
public static final boolean[] NO_BOOLEANAn empty immutableboolean[]array. -
NO_BYTE
public static final byte[] NO_BYTEAn empty immutablebyte[]array. -
NO_CHAR
public static final char[] NO_CHARAn empty immutablechar[]array. -
NO_SHORT
public static final short[] NO_SHORTAn empty immutableshort[]array. -
NO_INT
public static final int[] NO_INTAn empty immutableint[]array. -
NO_LONG
public static final long[] NO_LONGAn empty immutablelong[]array. -
NO_FLOAT
public static final float[] NO_FLOATAn empty immutablefloat[]array. -
NO_DOUBLE
public static final double[] NO_DOUBLEAn empty immutabledouble[]array.
-
-
Method Details
-
copyBytes
public static void copyBytes(byte[] dst, int dstOffset, byte[] src, int srcOffset, int size) Wrapper aroundJava'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:
NullPointerExceptionandArrayIndexOutOfBoundsExceptioncan be raised.- Parameters:
dst- destination arraydstOffset- start offset in the destination arraysrc- source array source array, potentially the same as the destination arraysrcOffset- start offset in the source arraysize- 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 checkedvalue- 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 anArrayIndexOutOfBoundsExceptionexception if either one of the input arrays does not contain enough bytes to be compared.- Parameters:
array1- first arraypos1- where to start in the first arrayarray2- second arraypos2- where to start in the second arraysize- 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 ofArrays.equals(boolean[], boolean[]).- Parameters:
array1- first arraypos1- start offset in the first arrayarray2- second arraypos2- start offset in the second arraysize- number of bytes to compare- Returns:
- true if the requested ranges are valid and equal
-
asList
A variant ofArrays.asList(Object...)that accepts primitives.- Parameters:
array- input array- Returns:
- boxed list containing the array values
-
asList
A variant ofArrays.asList(Object...)that accepts primitives.- Parameters:
array- input array- Returns:
- boxed list containing the array values
-
asList
A variant ofArrays.asList(Object...)that accepts primitives.- Parameters:
array- input array- Returns:
- boxed list containing the array values
-
asList
A variant ofArrays.asList(Object...)that accepts primitives.- Parameters:
array- input array- Returns:
- boxed list containing the array values
-
asList
A variant ofArrays.asList(Object...)that accepts primitives.- Parameters:
array- input array- Returns:
- boxed list containing the array values
-
asList
A variant ofArrays.asList(Object...)that accepts primitives.- Parameters:
array- input array- Returns:
- boxed list containing the array values
-
asList
A variant ofArrays.asList(Object...)that accepts primitives.- Parameters:
array- input array- Returns:
- boxed list containing the array values
-
asList
A variant ofArrays.asList(Object...)that accepts primitives.- Parameters:
array- input array- Returns:
- boxed list containing the array values
-
swap
public static void swap(boolean[] array, int offset, int size) Flip the order of an array of booleans.- Parameters:
array- array to reverseoffset- first index to reversesize- number of elements to reverse
-
swap
public static void swap(boolean[] array) Flip the order of an array of booleans.- Parameters:
array- array to reverse
-
swap
public static void swap(byte[] array, int offset, int size) Flip the order of an array of bytes.- Parameters:
array- array to reverseoffset- first index to reversesize- number of elements to reverse
-
swap
public static void swap(byte[] array) Flip the order of an array of bytes.- Parameters:
array- array to reverse
-
swap
public static void swap(short[] array, int offset, int size) Flip the order of an array of shorts.- Parameters:
array- array to reverseoffset- first index to reversesize- number of elements to reverse
-
swap
public static void swap(short[] array) Flip the order of an array of shorts.- Parameters:
array- array to reverse
-
swap
public static void swap(char[] array, int offset, int size) Flip the order of an array of chars.- Parameters:
array- array to reverseoffset- first index to reversesize- number of elements to reverse
-
swap
public static void swap(char[] array) Flip the order of an array of chars.- Parameters:
array- array to reverse
-
swap
public static void swap(int[] array, int offset, int size) Flip the order of an array of ints.- Parameters:
array- array to reverseoffset- first index to reversesize- number of elements to reverse
-
swap
public static void swap(int[] array) Flip the order of an array of ints.- Parameters:
array- array to reverse
-
swap
public static void swap(long[] array, int offset, int size) Flip the order of an array of longs.- Parameters:
array- array to reverseoffset- first index to reversesize- number of elements to reverse
-
swap
public static void swap(long[] array) Flip the order of an array of longs.- Parameters:
array- array to reverse
-
swap
public static void swap(float[] array, int offset, int size) Flip the order of an array of floats.- Parameters:
array- array to reverseoffset- first index to reversesize- number of elements to reverse
-
swap
public static void swap(float[] array) Flip the order of an array of floats.- Parameters:
array- array to reverse
-
swap
public static void swap(double[] array, int offset, int size) Flip the order of an array of doubles.- Parameters:
array- array to reverseoffset- first index to reversesize- number of elements to reverse
-
swap
public static void swap(double[] array) Flip the order of an array of doubles.- Parameters:
array- array to reverse
-
swap
public static <T> void swap(T[] array, int offset, int size) Flip the order of an array of generic objects.- Type Parameters:
T- element type- Parameters:
array- array to reverseoffset- first index to reversesize- number of elements to reverse
-
swap
public static <T> void swap(T[] array) Flip the order of an array of generic objects.- Type Parameters:
T- element type- Parameters:
array- array to reverse
-
find
public static int find(boolean[] array, boolean value) Find the first index of a value in an array.- Parameters:
array- input arrayvalue- value to find- Returns:
- zero-based index, or -1 if absent
-
find
public static int find(byte[] array, byte value) Find the first index of a value in an array.- Parameters:
array- input arrayvalue- value to find- Returns:
- zero-based index, or -1 if absent
-
find
public static int find(char[] array, char value) Find the first index of a value in an array.- Parameters:
array- input arrayvalue- value to find- Returns:
- zero-based index, or -1 if absent
-
find
public static int find(short[] array, short value) Find the first index of a value in an array.- Parameters:
array- input arrayvalue- value to find- Returns:
- zero-based index, or -1 if absent
-
find
public static int find(int[] array, int value) Find the first index of a value in an array.- Parameters:
array- input arrayvalue- value to find- Returns:
- zero-based index, or -1 if absent
-
find
public static int find(long[] array, long value) Find the first index of a value in an array.- Parameters:
array- input arrayvalue- value to find- Returns:
- zero-based index, or -1 if absent
-
find
public static int find(float[] array, float value) Find the first index of a value in an array.- Parameters:
array- input arrayvalue- value to find- Returns:
- zero-based index, or -1 if absent
-
find
public static int find(double[] array, double value) Find the first index of a value in an array.- Parameters:
array- input arrayvalue- value to find- Returns:
- zero-based index, or -1 if absent
-
findByReference
Find the first index of an object by reference equality.- Parameters:
array- input arrayvalue- value to find- Returns:
- zero-based index, or -1 if absent
-
findByEquality
Find the first index of an object byObject.equals(Object).- Parameters:
array- input arrayvalue- value to find- Returns:
- zero-based index, or -1 if absent
-
checkOffsetAndCount
public static void checkOffsetAndCount(int arrayLength, int offset, int count) Validate an array sub-range.- Parameters:
arrayLength- array lengthoffset- sub-range start offsetcount- sub-range element 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- AnyObject- Parameters:
array- the arrayindex- the index of the arraydefaultValue- 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- AnyObject- Parameters:
array- the arrayindex- the index of the first arrayindex2- the index of the second arraydefaultValue- 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) Retrieve a value from a 3-dimensional array, or a default value if an index is out of bound or an intermediate array/value is null.- Type Parameters:
T- AnyObject- Parameters:
array- the arrayindex- the index of the first arrayindex2- the index of the second arrayindex3- the index of the third arraydefaultValue- default value to return in case of error- Returns:
- the element of the array at the index or the default value
-
asView
Provide a continuous read-only view of an array.- Type Parameters:
T- element type- Parameters:
a- array (non-null)- Returns:
- a read-only list consisting of the elements of the first array
-
asView
Provide a continuous read-only view of two arrays.- Type Parameters:
T- element type- 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
Provide a continuous read-only view of an array of arrays. This method is well suited for 3+ arrays.- Type Parameters:
T- element type- 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.
-