Package org.atlanmod.commons.collect
Class MoreArrays
- java.lang.Object
-
- org.atlanmod.commons.collect.MoreArrays
-
@ParametersAreNonnullByDefault public final class MoreArrays extends Object
Static utility methods related to arrays.
-
-
Field Summary
Fields Modifier and Type Field Description static intNO_INDEXThe index value when an element is not found in a list or array:-1.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T[]add(T[] array, int index, T element)Adds the specifiedelementat the specifiedpositionin thearray.static byte[]addAll(byte[] array1, byte... array2)Adds allarray2at the end of thearray1.static <T> T[]addAll(T[] array, int index, Collection<? extends T> elements)Adds allelementsat the end of thearray.static <T> T[]append(T[] array, T element)Adds the specifiedelementat the last position in thearray.static <T> booleancontains(T[] array, T value)Checks if thevalueis in the givenarray.static <T> intindexOf(T[] array, T value)Finds the index of the givenvaluein thearray.static <T> intlastIndexOf(T[] array, T value)Finds the last index of the givenvaluein thearray.static <T> T[]newArray(Class<?> type, int length)Creates a new array with the specified componenttypeandlength.static <T> T[]remove(T[] array, int index)Removes the element at the specifiedindexfrom the specifiedarray.static <T> T[]resize(T[] array, int length)Resizes thearraywith the givenlength.static <T> T[]set(T[] array, int index, T value)Sets the specifiedelementat theindexin thearray.static Byte[]toObject(byte... primitiveArray)Converts an array of primitive typesbyteto an array ofByte.static byte[]toPrimitive(Byte... boxedArray)Converts an array ofByteto an array of primitive typesbyte.
-
-
-
Field Detail
-
NO_INDEX
public static final int NO_INDEX
The index value when an element is not found in a list or array:-1.- See Also:
- Constant Field Values
-
-
Method Detail
-
newArray
@Nonnull public static <T> T[] newArray(Class<?> type, @Nonnegative int length)
Creates a new array with the specified componenttypeandlength.- Type Parameters:
T- the type of the new array- Parameters:
type- the class of the new arraylength- the length of the new array- Returns:
- a new array
- Throws:
NullPointerException- if thetypeisnullIllegalArgumentException- if thelengthis negative
-
set
@Nonnull public static <T> T[] set(T[] array, @Nonnegative int index, @Nullable T value)Sets the specifiedelementat theindexin thearray.This method is similar to:
array[index] = value; return array;- Type Parameters:
T- the component type of the array- Parameters:
array- the array to set the element toindex- the position of the objectvalue- the object to set- Returns:
- the
arraycontaining the existing elements and the new element
-
resize
@Nonnull public static <T> T[] resize(T[] array, @Nonnegative int length)Resizes thearraywith the givenlength.- Type Parameters:
T- the type of the new array- Parameters:
array- the array to resizelength- the new length of the array- Returns:
- a new array containing the existing elements with the new size
- Throws:
NullPointerException- if thetypeisnullIllegalArgumentException- if thelengthis lower thanarray.length
-
append
@Nonnull public static <T> T[] append(T[] array, @Nullable T element)Adds the specifiedelementat the last position in thearray.- Type Parameters:
T- the component type of the array- Parameters:
array- the array to add the element toelement- the object to add- Returns:
- a new array containing the existing elements and the new element
- Throws:
NullPointerException- if thearrayisnull
-
add
@Nonnull public static <T> T[] add(T[] array, @Nonnegative int index, @Nullable T element)Adds the specifiedelementat the specifiedpositionin thearray.- Type Parameters:
T- the component type of the array- Parameters:
array- the array to add the element toindex- the position of the new objectelement- the object to add- Returns:
- a new array containing the existing elements and the new element
- Throws:
NullPointerException- if thearrayisnullIndexOutOfBoundsException- if theindexis out of range (index < 0 || index > array.length)
-
addAll
@Nonnull public static <T> T[] addAll(T[] array, int index, Collection<? extends T> elements)Adds allelementsat the end of thearray.- Type Parameters:
T- the component type of the array- Parameters:
array- the array to add the element toelements- the objects to add- Returns:
- a new array containing the existing elements and the new elements
- Throws:
NullPointerException- if any argument isnull
-
addAll
@Nonnull public static byte[] addAll(byte[] array1, byte... array2)Adds allarray2at the end of thearray1.- Parameters:
array1- the array to add the element toarray2- the values to add- Returns:
- a new array of bytes containing all the existing elements in arrays 1 and 2.
- Throws:
NullPointerException- if any argument isnull
-
remove
@Nonnull public static <T> T[] remove(T[] array, @Nonnegative int index)Removes the element at the specifiedindexfrom the specifiedarray.- Type Parameters:
T- the component type of the array- Parameters:
array- the array to remove the element fromindex- the position of the element to be removed- Returns:
- a new array containing the existing elements except the element at the specified position.
- Throws:
NullPointerException- if thearrayisnullIndexOutOfBoundsException- if theindexis out of range (index < 0 || index > array.length)
-
contains
public static <T> boolean contains(T[] array, @Nullable T value)Checks if thevalueis in the givenarray.- Parameters:
array- the array to search throughvalue- the object to find- Returns:
trueif the array contains the object,falseotherwise- Throws:
NullPointerException- if thearrayisnull
-
indexOf
public static <T> int indexOf(T[] array, @Nullable T value)Finds the index of the givenvaluein thearray.- Parameters:
array- the array to search through for the objectvalue- the value to find, may benull- Returns:
- the index of the value within the array,
-1if not found - Throws:
NullPointerException- if thearrayisnull
-
lastIndexOf
public static <T> int lastIndexOf(T[] array, @Nullable T value)Finds the last index of the givenvaluein thearray.- Parameters:
array- the array to traverse for looking for the objectvalue- the object to find, may benull- Returns:
- the last index of the object within the array,
-1if not found - Throws:
NullPointerException- if thearrayisnull
-
toPrimitive
@Nonnull public static byte[] toPrimitive(Byte... boxedArray)
Converts an array ofByteto an array of primitive typesbyte.- Parameters:
boxedArray- the array ofByteto convert.- Returns:
- an array of
bytecontaining the same elements asboxedArray, in the same order, converted to primitives.
-
toObject
public static Byte[] toObject(byte... primitiveArray)
Converts an array of primitive typesbyteto an array ofByte.- Parameters:
primitiveArray- the array ofbyteto convert.- Returns:
- an array of
Bytecontaining the same elements asprimitiveArray, in the same order, converted to boxed types.
-
-