Package org.agrona.collections
Class ArrayUtil
- java.lang.Object
-
- org.agrona.collections.ArrayUtil
-
public final class ArrayUtil extends java.lang.Object
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
Fields Modifier and Type Field Description static boolean[]
EMPTY_BOOLEAN_ARRAY
Empty boolean array.static byte[]
EMPTY_BYTE_ARRAY
Empty byte array.static char[]
EMPTY_CHAR_ARRAY
Empty char array.static double[]
EMPTY_DOUBLE_ARRAY
Empty double array.static float[]
EMPTY_FLOAT_ARRAY
Empty float array.static int[]
EMPTY_INT_ARRAY
Empty int array.static long[]
EMPTY_LONG_ARRAY
Empty boolean array.static java.lang.Object[]
EMPTY_OBJECT_ARRAY
Empty Object array.static short[]
EMPTY_SHORT_ARRAY
Empty short array.static java.lang.String[]
EMPTY_STRING_ARRAY
Empty String array.static int
MAX_CAPACITY
Maximum capacity to which an array can grow.static int
UNKNOWN_INDEX
Constant indicating an invalid/unknown array index.
-
Constructor Summary
Constructors Modifier Constructor Description private
ArrayUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <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 Detail
-
UNKNOWN_INDEX
public static final int UNKNOWN_INDEX
Constant indicating an invalid/unknown array index.- See Also:
- Constant Field Values
-
EMPTY_BOOLEAN_ARRAY
public static final boolean[] EMPTY_BOOLEAN_ARRAY
Empty boolean array.
-
EMPTY_BYTE_ARRAY
public static final byte[] EMPTY_BYTE_ARRAY
Empty byte array.
-
EMPTY_CHAR_ARRAY
public static final char[] EMPTY_CHAR_ARRAY
Empty char array.
-
EMPTY_SHORT_ARRAY
public static final short[] EMPTY_SHORT_ARRAY
Empty short array.
-
EMPTY_INT_ARRAY
public static final int[] EMPTY_INT_ARRAY
Empty int array.
-
EMPTY_FLOAT_ARRAY
public static final float[] EMPTY_FLOAT_ARRAY
Empty float array.
-
EMPTY_LONG_ARRAY
public static final long[] EMPTY_LONG_ARRAY
Empty boolean array.
-
EMPTY_DOUBLE_ARRAY
public static final double[] EMPTY_DOUBLE_ARRAY
Empty double array.
-
EMPTY_OBJECT_ARRAY
public static final java.lang.Object[] EMPTY_OBJECT_ARRAY
Empty Object array.
-
EMPTY_STRING_ARRAY
public static final java.lang.String[] EMPTY_STRING_ARRAY
Empty String array.
-
MAX_CAPACITY
public static final int MAX_CAPACITY
Maximum capacity to which an array can grow.- See Also:
- Constant Field Values
-
-
Method Detail
-
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.
-
-