Package org.agrona.collections
Class ArrayUtil
java.lang.Object
org.agrona.collections.ArrayUtil
Utility class for operating on arrays as if they were collections. This is useful for
critical paths where operations like add and remove are seldom used, but iterating
is common and checkcast and indirection are comparatively expensive.
In all cases the array being mutated is assumed to be full.
In all cases reference equality is used.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final boolean[]
Empty boolean array.static final byte[]
Empty byte array.static final char[]
Empty char array.static final double[]
Empty double array.static final float[]
Empty float array.static final int[]
Empty int array.static final long[]
Empty boolean array.static final Object[]
Empty Object array.static final short[]
Empty short array.static final String[]
Empty String array.static final int
Maximum capacity to which an array can grow.static final int
Constant indicating an invalid/unknown array index. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T[]
add
(T[] oldElements, T elementToAdd) Add an element to an array resulting in a new array.static <T> T[]
ensureCapacity
(T[] oldElements, int requiredLength) Ensure an array has the required capacity.static <T> T[]
newArray
(T[] oldElements, int length) Allocate a new array of the same type as another array.static <T> T[]
remove
(T[] oldElements, int index) Remove an element from an array resulting in a new array if the index was inside the array otherwise the old array.static <T> T[]
remove
(T[] oldElements, T elementToRemove) Remove an element from an array resulting in a new array if the element was found otherwise the old array.
-
Field Details
-
UNKNOWN_INDEX
public static final int UNKNOWN_INDEXConstant indicating an invalid/unknown array index.- See Also:
-
EMPTY_BOOLEAN_ARRAY
public static final boolean[] EMPTY_BOOLEAN_ARRAYEmpty boolean array. -
EMPTY_BYTE_ARRAY
public static final byte[] EMPTY_BYTE_ARRAYEmpty byte array. -
EMPTY_CHAR_ARRAY
public static final char[] EMPTY_CHAR_ARRAYEmpty char array. -
EMPTY_SHORT_ARRAY
public static final short[] EMPTY_SHORT_ARRAYEmpty short array. -
EMPTY_INT_ARRAY
public static final int[] EMPTY_INT_ARRAYEmpty int array. -
EMPTY_FLOAT_ARRAY
public static final float[] EMPTY_FLOAT_ARRAYEmpty float array. -
EMPTY_LONG_ARRAY
public static final long[] EMPTY_LONG_ARRAYEmpty boolean array. -
EMPTY_DOUBLE_ARRAY
public static final double[] EMPTY_DOUBLE_ARRAYEmpty double array. -
EMPTY_OBJECT_ARRAY
Empty Object array. -
EMPTY_STRING_ARRAY
Empty String array. -
MAX_CAPACITY
public static final int MAX_CAPACITYMaximum capacity to which an array can grow.- See Also:
-
-
Constructor Details
-
ArrayUtil
private ArrayUtil()
-
-
Method Details
-
add
public static <T> T[] add(T[] oldElements, T elementToAdd) Add an element to an array resulting in a new array.- Type Parameters:
T
- type of the array.- Parameters:
oldElements
- to have the new element added.elementToAdd
- for the new array.- Returns:
- a new array that is one bigger and containing the new element at the end.
-
remove
public static <T> T[] remove(T[] oldElements, T elementToRemove) Remove an element from an array resulting in a new array if the element was found otherwise the old array.Returns its input parameter if the element to remove isn't a member.
- Type Parameters:
T
- type of the array.- Parameters:
oldElements
- to have the element removed from.elementToRemove
- being searched for by identity semantics.- Returns:
- a new array without the element if found otherwise the original array.
-
remove
public static <T> T[] remove(T[] oldElements, int index) Remove an element from an array resulting in a new array if the index was inside the array otherwise the old array.Returns the old elements array if the index isn't inside the array.
- Type Parameters:
T
- type of the array.- Parameters:
oldElements
- to have the element removed from.index
- to remove the element at.- Returns:
- a new array without the element if the index is inside the array otherwise the original array.
-
newArray
public static <T> T[] newArray(T[] oldElements, int length) Allocate a new array of the same type as another array.- Type Parameters:
T
- type of the array.- Parameters:
oldElements
- on which the new array is based.length
- of the new array.- Returns:
- the new array of requested length.
-
ensureCapacity
public static <T> T[] ensureCapacity(T[] oldElements, int requiredLength) Ensure an array has the required capacity. Resizing only if needed.- Type Parameters:
T
- type of the array.- Parameters:
oldElements
- to ensure that are long enough.requiredLength
- to ensure.- Returns:
- an array of the required length.
-