saker.util Documentation TaskDoc JavaDoc Packages
package saker.util
public class ArrayUtils
Utility class containg functions related to arrays.
Methods
public static <T> T[]
appended(T[] array, T element)
Creates a new array by appending the given element at the end of it.
public static int
Computes the deep hash code of the argument array.
public static int
Computes the hash code of the argument array.
public static int
arrayIndexOf(Object[] array, int start, int end, Object object)
Finds the index of an object in the specified array range.
public static int
arrayIndexOf(Object[] array, Object object)
Finds the index of an object in the specified array.
public static int
arrayLastIndexOf(Object[] array, int start, int end, Object object)
Finds the last index of an object in the specified array range.
public static int
arrayLastIndexOf(Object[] array, Object object)
Finds the last index of an object in the specified array.
public static boolean
arrayRangeEquals(Object[] first, int firstoffset, Object[] second, int secondoffset, int length)
Compares the elements in the argument array ranges for equality.
public static int
arrayRangeHashCode(byte[] array, int offset, int length)
Computes the hashcode of an array using the elements in the given range.
public static List<?>
Creates a new List that is backed by the argument array.
public static List<?>
arrayReflectionList(Object array, int offset, int length)
Creates a new List that is backed by the specified range of the argument array.
public static String
Converts the array to a string representation.
public static void
Converts the array to a string representation and appends it to the argument string builder.
public static boolean
arraysDeepEqual(Object array1, Object array2)
Checks the deep equality of the two argument arrays.
public static boolean
arraysEqual(Object array1, Object array2)
Checks the equality of the two argument arrays.
public static <T> T[]
cloneReverse(T[] array)
Clones the array and reverses it.
public static <T> T[]
concat(T[] first, T[] second)
Concatenates the specified arrays.
public static <T> void
forEach(T[] array, Consumer<super T> action)
Invokes the argument action for every element in the specified array.
public static <T> T[]
inserted(T[] array, int index, T element)
Creates a new array by inserting the given element at the specified position.
public static int
mismatch(byte[] first, int firstoffset, byte[] second, int secondoffset, int length)
Finds the index of the first mismatching bytes in the specified byte array ranges.
public static <T> T[]
prepended(T[] array, T element)
Creates a new array by prepending the given element at the start of it.
public static boolean
regionEquals(byte[] first, int firstoffset, byte[] second, int secondoffset, int length)
Checks if the specified range of byte arrays equal to each other.
public static <T> T[]
removedAtIndex(T[] array, int index)
Removes the element at the specified index from the given array.
public static void
requireArrayRange(byte[] array, int offset, int length)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayRange(char[] array, int offset, int length)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayRange(double[] array, int offset, int length)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayRange(float[] array, int offset, int length)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayRange(int[] array, int offset, int length)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayRange(long[] array, int offset, int length)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayRange(Object[] array, int offset, int length)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayRange(short[] array, int offset, int length)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayRange(boolean[] array, int offset, int length)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayRangeLength(int arraylength, int offset, int length)
Validation method to check if a specified range lies within the argument array given by its length.
public static void
requireArrayStartEndRange(byte[] array, int start, int end)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayStartEndRange(char[] array, int start, int end)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayStartEndRange(double[] array, int start, int end)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayStartEndRange(float[] array, int start, int end)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayStartEndRange(int[] array, int start, int end)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayStartEndRange(long[] array, int start, int end)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayStartEndRange(Object[] array, int start, int end)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayStartEndRange(short[] array, int start, int end)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayStartEndRange(boolean[] array, int start, int end)
Validation method to check if a specified range lies within the argument array.
public static void
requireArrayStartEndRangeLength(int arraylength, int start, int end)
Validation method to check if a specified range lies within the argument array given by its length.
public static void
reverse(Object[] array)
Reverses the array in place.
public static void
reverse(Object[] array, int start, int end)
Reverses the specified region of the array in place.
public static <T> T[] appended(T[] array, T element) throws NullPointerException
Creates a new array by appending the given element at the end of it.

This method will create a new array that has the length array.length + 1 and will set the element at array.length to the argument.

It is recommended not to call this function repeatedly (i.e. in a loop), as a new array is created each time it is called.

TThe element type.
arrayThe array to append the element to.
elementThe element to append.
The new array with the additional element.
NullPointerExceptionIf the array is null.
Computes the deep hash code of the argument array.

The function delegates the array hash code call to the appropriate Arrays.hashCode overload if the array has a primitive component type, else it is delegated to Arrays.deepHashCode(Object[]).

arrayThe array.
The computed hash code.
NullPointerExceptionIf the argument is null.
IllegalArgumentExceptionIf the argument is not an array.
Computes the hash code of the argument array.

The function delegates the array hash code call to the appropriate Arrays.hashCode overload with the appropriate array type.

The method doesn't compute a deep hash code, meaning, that if the argument is an object array, and there are nested arrays in it (i.e. multi dimensional arrays), the element hash codes will not be computed for them. Use arrayDeepHashCode(Object) to compute the deep hash code.

arrayThe array.
The computed hash code.
NullPointerExceptionIf the argument is null.
IllegalArgumentExceptionIf the argument is not an array.
public static int arrayIndexOf(Object[] array, int start, int end, Object object) throws NullPointerException, IndexOutOfBoundsException
Finds the index of an object in the specified array range.
arrayThe array to search in.
startThe start index of the search range. (inclusive)
endThe end index of the search range. (exclusive)
objectThe object to search for.
The index of the object in the array or -1 if not found. (The index is relative to the start of the array, not the start of the range.)
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the range is exceeds the array boundaries. (May be only thrown if the elements outside the boundaries are actually accessed.)
public static int arrayIndexOf(Object[] array, Object object) throws NullPointerException
Finds the index of an object in the specified array.
arrayThe array to search in.
objectThe object to search for.
The index of the object in the array or -1 if not found.
NullPointerExceptionIf the array is null.
public static int arrayLastIndexOf(Object[] array, int start, int end, Object object) throws NullPointerException, IndexOutOfBoundsException
Finds the last index of an object in the specified array range.
arrayThe array to search in.
startThe start index of the search range. (inclusive)
endThe end index of the search range. (exclusive)
objectThe object to search for.
The index of the object in the array or -1 if not found. (The index is relative to the start of the array, not the start of the range.)
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the range is exceeds the array boundaries. (May be only thrown if the elements outside the boundaries are actually accessed.)
public static int arrayLastIndexOf(Object[] array, Object object) throws NullPointerException
Finds the last index of an object in the specified array.
arrayThe array to search in.
objectThe object to search for.
The last index of the object in the array or -1 if not found.
NullPointerExceptionIf the array is null.
public static boolean arrayRangeEquals(Object[] first, int firstoffset, Object[] second, int secondoffset, int length) throws IndexOutOfBoundsException
Compares the elements in the argument array ranges for equality.

This method works the same way as Arrays.equals(Object[], Object[]), but compares subranges of the arrays instead of the entirety of them.

If any of the arrays are null, this method will return true, of the another array is null as well, else false.

firstThe first array.
firstoffsetThe starting offset index of the range in the first array. (inclusive)
secondThe second array.
secondoffsetThe starting offset index of the range in the second array. (inclusive)
lengthThe number of elements to compare.
true if all elements in the specified arrays are equal.
IndexOutOfBoundsExceptionIf a specified range is out of bounds.
public static int arrayRangeHashCode(byte[] array, int offset, int length) throws IndexOutOfBoundsException
Computes the hashcode of an array using the elements in the given range.

This method computes the hashcode the same ways as Arrays.hashCode(byte[]), but works only on a range of the elements.

arrayThe array.
offsetThe staring offset index of the range. (inclusive)
lengthThe number of elements in the range.
The computed hashcode.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
Creates a new List that is backed by the argument array.

The array may have a primitive component type, it will be handled accordingly. The underlying implementation uses the Array class to access the elements.

The returned list is modifiable, and modifications will propagate back to the argument array.

arrayThe array to wrap into a list.
The list, or null if the argument is null.
IllegalArgumentExceptionIf the argument is not an array.
public static List<?> arrayReflectionList(Object array, int offset, int length) throws IllegalArgumentException, IndexOutOfBoundsException
Creates a new List that is backed by the specified range of the argument array.

The array may have a primitive component type, it will be handled accordingly. The underlying implementation uses the Array class to access the elements.

The returned list is modifiable, and modifications will propagate back to the argument array.

arrayThe array to wrap into a list.
offsetThe offset where the returned list should start.
lengthThe number of elements in the range.
The list, or null if the argument is null.
IllegalArgumentExceptionIf the argument is not an array.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
Converts the array to a string representation.
arrayThe array.
The string representation of the array.
IllegalArgumentExceptionIf the argument is not an array.
Converts the array to a string representation and appends it to the argument string builder.
arrayThe array.
sbThe string builder.
NullPointerExceptionIf the string builder is null.
IllegalArgumentExceptionIf the argument is not an array.
public static boolean arraysDeepEqual(Object array1, Object array2) throws IllegalArgumentException
Checks the deep equality of the two argument arrays.

This method handles primitive array types, and delegates those calls to the appropriate overload of Arrays.equals. If the arrays have reference component types, Arrays.deepEquals(Object[], Object[]) is used to check equality.

If both arguments are null, true is returned. If only one, false.

array1The first array.
array2The second array.
true if the arrays equal.
IllegalArgumentExceptionIf any of the arguments are not arrays.
public static boolean arraysEqual(Object array1, Object array2) throws IllegalArgumentException
Checks the equality of the two argument arrays.

This method handles primitive array types, and delegates those calls to the appropriate overload of Arrays.equals.

This method does shallow equals, meaning that any of the arguments contain multi dimensional arrays, this method doesn't examine their contents. To check deep equality, use arraysDeepEqual(Object, Object).

If both arguments are null, true is returned. If only one, false.

array1The first array.
array2The second array.
true if the arrays equal.
IllegalArgumentExceptionIf any of the arguments are not arrays.
public static <T> T[] cloneReverse(T[] array)
Clones the array and reverses it.
arrayThe array.
The cloned and reversed array or null if the argument is null.
public static <T> T[] concat(T[] first, T[] second) throws NullPointerException
Concatenates the specified arrays.

The method creates a new array that contains the elements from the first and the second in this order.

The component type of the resulting array is either the same as the first or the second array arguments.

TThe element type.
firstThe first array.
secondThe second array.
A new array that contains the elements from the specified arrays.
NullPointerExceptionIf any of the arguments are null.
public static <T> void forEach(T[] array, Consumer<super T> action) throws NullPointerException
Invokes the argument action for every element in the specified array.

The action is invoked in the same order as the elements occurr in the array.

arrayThe array.
actionThe action to invoke for each element.
NullPointerExceptionIf any of the arguments are null.
public static <T> T[] inserted(T[] array, int index, T element) throws NullPointerException, IndexOutOfBoundsException
Creates a new array by inserting the given element at the specified position.

This method will create a new array that has the length array.length + 1 and will insert the element at the specified position.

It is recommended not to call this function repeatedly (i.e. in a loop), as a new array is created each time it is called.

If the insertion index equals to array.length, this method works the same way as appended(T[], T).

TThe element type.
arrayThe array to insert the element for.
indexThe index at which to insert the element.
elementThe element to insert.
The new array with the inserted element.
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the index is negative or greater than array.length.
public static int mismatch(byte[] first, int firstoffset, byte[] second, int secondoffset, int length) throws NullPointerException, IndexOutOfBoundsException
Finds the index of the first mismatching bytes in the specified byte array ranges.

The method will iterate over the bytes in the specified ranges, and will return the relative index of the first one that doesn't equal. If all bytes equal in the specified ranges, the method will return -1.

The returned relative index is to be interpreted against the specified starting offsets instead of the start of the arrays.

This method uses iterating implementation on JDK 8, and uses vectorized implementation on JDK 9+.

firstThe first array of bytes.
firstoffsetThe starting offset of the range in the first array.
secondThe second array of bytes.
secondoffsetThe starting offset of the range in the second array.
lengthThe number of bytes to compare.
The relative index of the first mismatching byte or -1 if the ranges equal.
NullPointerExceptionIf any of the arrays are null.
IndexOutOfBoundsExceptionIf any of the specified range is out of bounds.
public static <T> T[] prepended(T[] array, T element) throws NullPointerException
Creates a new array by prepending the given element at the start of it.

This method will create a new array that has the length array.length + 1 and will set the element at 0 to the argument.

It is recommended not to call this function repeatedly (i.e. in a loop), as a new array is created each time it is called.

TThe element type.
arrayThe array to prepend the element to.
elementThe element to prepend.
The new array with the additional element.
NullPointerExceptionIf the array is null.
public static boolean regionEquals(byte[] first, int firstoffset, byte[] second, int secondoffset, int length) throws NullPointerException, IndexOutOfBoundsException
Checks if the specified range of byte arrays equal to each other.

This method will use the vectorized comparison support on JDK implementations which support it. (Usually JDK9+)

firstThe first array.
firstoffsetThe offset in the first array where the range starts.
secondThe second array.
secondoffsetThe offset in the second array where the range starts.
lengthThe number of bytes to compare. The length of both ranges.
true if the specified ranges contain the same bytes.
NullPointerExceptionIf any of the arguments are null.
IndexOutOfBoundsExceptionIf any of the specified range is out of bounds.
public static <T> T[] removedAtIndex(T[] array, int index) throws NullPointerException, IndexOutOfBoundsException
Removes the element at the specified index from the given array.

This method will create a new array that doesn't contain the element at the specified index.

TThe component type of the array.
arrayThe array.
indexThe index of the element to remove.
The new array that doesn't contain the specified element.
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the index is out of bounds for the array.
public static void requireArrayRange(byte[] array, int offset, int length) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
offsetThe starting offset index of the range. (inclusive)
lengthThe length of the range.
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayRange(char[] array, int offset, int length) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
offsetThe starting offset index of the range. (inclusive)
lengthThe length of the range.
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayRange(double[] array, int offset, int length) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
offsetThe starting offset index of the range. (inclusive)
lengthThe length of the range.
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayRange(float[] array, int offset, int length) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
offsetThe starting offset index of the range. (inclusive)
lengthThe length of the range.
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayRange(int[] array, int offset, int length) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
offsetThe starting offset index of the range. (inclusive)
lengthThe length of the range.
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayRange(long[] array, int offset, int length) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
offsetThe starting offset index of the range. (inclusive)
lengthThe length of the range.
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayRange(Object[] array, int offset, int length) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
offsetThe starting offset index of the range. (inclusive)
lengthThe length of the range.
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayRange(short[] array, int offset, int length) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
offsetThe starting offset index of the range. (inclusive)
lengthThe length of the range.
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayRange(boolean[] array, int offset, int length) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
offsetThe starting offset index of the range. (inclusive)
lengthThe length of the range.
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayRangeLength(int arraylength, int offset, int length) throws IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array given by its length.
arraylengthThe length of the associated array.
offsetThe starting offset index of the range. (inclusive)
lengthThe length of the range.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayStartEndRange(byte[] array, int start, int end) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
startThe starting index of the range. (inclusive)
endThe end index of the range. (exclusive)
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayStartEndRange(char[] array, int start, int end) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
startThe starting index of the range. (inclusive)
endThe end index of the range. (exclusive)
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayStartEndRange(double[] array, int start, int end) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
startThe starting index of the range. (inclusive)
endThe end index of the range. (exclusive)
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayStartEndRange(float[] array, int start, int end) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
startThe starting index of the range. (inclusive)
endThe end index of the range. (exclusive)
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayStartEndRange(int[] array, int start, int end) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
startThe starting index of the range. (inclusive)
endThe end index of the range. (exclusive)
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayStartEndRange(long[] array, int start, int end) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
startThe starting index of the range. (inclusive)
endThe end index of the range. (exclusive)
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayStartEndRange(Object[] array, int start, int end) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
startThe starting index of the range. (inclusive)
endThe end index of the range. (exclusive)
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayStartEndRange(short[] array, int start, int end) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
startThe starting index of the range. (inclusive)
endThe end index of the range. (exclusive)
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayStartEndRange(boolean[] array, int start, int end) throws NullPointerException, IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array.
arrayThe array.
startThe starting index of the range. (inclusive)
endThe end index of the range. (exclusive)
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void requireArrayStartEndRangeLength(int arraylength, int start, int end) throws IndexOutOfBoundsException
Validation method to check if a specified range lies within the argument array given by its length.
arraylengthThe length of the associated array.
startThe starting index of the range. (inclusive)
endThe end index of the range. (exclusive)
IndexOutOfBoundsExceptionIf the specified range is out of bounds.
public static void reverse(Object[] array) throws NullPointerException
Reverses the array in place.
arrayThe array to reverse.
NullPointerExceptionIf the array is null.
public static void reverse(Object[] array, int start, int end) throws NullPointerException, IndexOutOfBoundsException
Reverses the specified region of the array in place.
arrayThe array to reverse.
startThe starting index of the reversed region. (inclusive)
endThe end index of the reversed region. (exclusive)
NullPointerExceptionIf the array is null.
IndexOutOfBoundsExceptionIf the specified range is out of bounds.