Package one.microstream.collections
Class XArrays
java.lang.Object
one.microstream.collections.XArrays
public final class XArrays extends Object
Numerous utility methods for working with arrays, all of which are either missing in the JDK or botched up
in one way or another. For both writing AND reading code, there must be a LOT of util methods, so that the
source code can be as short but comprehensible possible. Keeping the number of util methods at the bare minimum
might make things simpler for the JDK provider, but not for the JDK user. There need to be a rich set of proper
general purpose tools, not the need to write almost every util method anew in every project.
-
Constructor Summary
Constructors Constructor Description XArrays()
-
Method Summary
Modifier and Type Method Description static int[]
_intAdd(int[] a1, int... a2)
static int[]
add(int[] a1, int... a2)
static long[]
add(long[] a1, long... a2)
static <T> T[]
add(T[] array, T element)
static <T> T[]
add(T[] a1, T... a2)
adds all elements of the first array and all elements of the second array into one result array.static <T> T[]
and(T[] a1, T[] a2)
static <T> boolean
applies(T[] array, Predicate<? super T> predicate)
static <T, U extends T>
T[]arraycopy(U[] src, int srcPos, T[] dest, int destPos, int length)
Convenience method, calling eitherSystem.arraycopy(Object,int,Object,int,int)
forlength >= 0
orreverseArraycopy(Object[],int,Object[],int,int)
forlength < 0
and returnsdest
.
If length is known to be positive and performance badly matters or negative length shall be treated as an error, useSystem.arraycopy(Object,int,Object,int,int)
directly.static int
arrayHashCode(Object[] data, int size)
static void
checkBounds(int arrayLength, int start, int bound)
static void
checkBounds(Object[] array, int start, int bound)
static <T> T[]
clear(T[] array)
static <T, S extends T>
T[]combine(Class<T> componentType, S[]... arrays)
static <T> T[]
combine(T[]... arrays)
static <E> Class<E>
componentType(E[] array)
static boolean
contains(int[] values, int value)
static <E> boolean
contains(E[] array, E element)
This method checks ifarray
containselement
by object identitystatic <E> boolean
contains(Collection<? super E> c, E sample, Equalator<? super E> equalator)
static <T, S extends T>
booleancontains(T[] array, S element, Equalator<? super T> cmp)
static <E> boolean
containS(Collection<E> c, E element)
static <E> boolean
containsId(Collection<E> c, E element)
static boolean
containsNull(Object[] data, int offset, int length)
static <T> T[]
convertArray(Object[] objects, Class<T> type)
static <T> T[]
copy(T[] array)
static <E> E[]
copyRange(E[] elements, int offset, int length)
static <T, E extends T>
T[]copyTo(XGettingCollection<E> source, T[] target)
At least for Java 1.8, the types seem to not be checked.static <T, E extends T>
T[]copyTo(XGettingCollection<E> source, T[] target, int targetOffset)
static <T> int
count(T[] array, Predicate<? super T> predicate)
static <T> T[]
enlarge(T[] array, int newLength)
static <T> T[]
ensureContained(T[] ts, T t)
static <T> T[]
ensureContained(T[] a1, T... a2)
static <E> boolean
eqContains(E[] array, E element)
static boolean
equals(byte[] a, byte[] a2, int length)
static <E> boolean
equals(E[] array1, int startIndex1, E[] array2, int startIndex2, int length, Equalator<? super E> comparator)
static boolean
equals(Object[] array1, Object[] array2)
Compares two Object arrays by reference of their content.static byte[]
fill(byte[] array, byte fillElement)
static char[]
fill(char[] array, char fillElement)
static double[]
fill(double[] array, double fillElement)
static float[]
fill(float[] array, float fillElement)
static int[]
fill(int[] array, int fillElement)
static <T> T[]
fill(T[] array, int offset, int bound, Supplier<? extends T> supplier)
static <T> T[]
fill(T[] array, Supplier<? extends T> supplier)
static <T> T[]
fill(T[] array, T fillElement, int fromIndex, int toIndex)
static <E> E[]
filter(E[] elements, Predicate<? super E> predicate)
static <E, C extends Consumer<? super E>>
CfilterTo(E[] elements, C target, Predicate<? super E> predicate)
static boolean
hasNoContent(Object[] array)
Returns if the passed array is either null or has the length 0.static int
indexOf(boolean value, boolean[] values)
static int
indexOf(byte[] data, byte[] subject)
static int
indexOf(byte[] data, byte[] subject, int startIndex)
static int
indexOf(byte value, byte[] values)
static int
indexOf(char value, char[] values)
static int
indexOf(double value, double[] values)
static int
indexOf(float value, float[] values)
static int
indexOf(int value, int[] values)
static int
indexOf(long value, long[] values)
static int
indexOf(short value, short[] values)
static <T, S> int
indexOf(S sample, T[] array, BiPredicate<T,S> predicate)
static <T> int
indexOf(T element, T[] array)
static void
iterate(int[] elements, int offset, int length, _intProcedure iterator)
static void
iterate(int[] elements, _intProcedure iterator)
static void
iterate(long[] elements, int offset, int length, _longProcedure iterator)
static void
iterate(long[] elements, _longProcedure iterator)
static <E> void
iterate(E[] elements, int offset, int length, Consumer<? super E> iterator)
static <E, I extends Consumer<? super E>>
Iiterate(E[] elements, I iterator)
static int
max(int... data)
static <T> T[]
merge(T[] a1, T... a2)
Merges the both passed arrays by taking all elements froma1
(even duplicates) and adds all elements ofa2
(also duplicates as well) that are not already contained ina1
.static int
min(int... data)
static <T> T[]
not(T[] a1, T[] a2)
static <T> T[]
or(T[] a1, T[] a2)
static <S, T extends S>
S[]orderByIndices(T[] elements, int[] indices, int indicesOffset, S[] target)
Orders the passed elements by the passed indices.static boolean[]
rebuild(boolean[] oldArray, int newLength)
static byte[]
rebuild(byte[] oldArray, int newLength)
static char[]
rebuild(char[] oldArray, int newLength)
static double[]
rebuild(double[] oldArray, int newLength)
static float[]
rebuild(float[] oldArray, int newLength)
static int[]
rebuild(int[] oldArray, int newLength)
static long[]
rebuild(long[] oldArray, int newLength)
static short[]
rebuild(short[] oldArray, int newLength)
static <E> E[]
rebuild(E[] oldArray, int newLength)
static <T> T[]
remove(T[] array, int i)
static int
removeAllFromArray(int[] array, int start, int bound, int e)
static <E> int
removeAllFromArray(E[] array, int start, int bound, E e)
Removed all occurances ofe
from arrayarray
.static <E> int
removeAllFromArray(E[] array, int start, int bound, E e, Equalator<? super E> equalator)
static <E> int
removeAllFromArray(E[] array, int start, int bound, XGettingCollection<? extends E> elements, Equalator<? super E> equalator)
static <E> int
removeAllFromArray(XGettingCollection<? extends E> elements, E[] array, int start, int bound)
static <E> E[]
removeDuplicates(E... elements)
static <T> T
removeFromIndex(T[] elements, int size, int i)
static <E> int
replaceAllInArray(E[] data, int startLow, int boundHigh, E oldElement, E newElement)
static <E> int
replaceAllInArray(E[] data, int startLow, int boundHigh, E sample, Equalator<? super E> equalator, E newElement)
static <T> T[]
replicate(T subject, int times)
static <T> T[]
reverse(T[] array)
static int[]
reverseArraycopy(int[] src, int srcPos, int[] dest, int destPos, int length)
static <T, U extends T>
T[]reverseArraycopy(U[] src, int srcPos, T[] dest, int destPos, int length)
Reverse order counterpart toSystem.arraycopy(Object,int,Object,int,int)
.static <T> T
search(T[] array, Predicate<? super T> predicate)
static void
set_booleanInBytes(byte[] bytes, int index, boolean value)
static void
set_byteInBytes(byte[] bytes, int index, byte value)
static void
set_charInBytes(byte[] bytes, int index, char value)
static void
set_doubleInBytes(byte[] bytes, int index, double value)
static void
set_floatInBytes(byte[] bytes, int index, float value)
static void
set_intInBytes(byte[] bytes, int index, int value)
static void
set_longInBytes(byte[] bytes, int index, long value)
static void
set_shortInBytes(byte[] bytes, int index, short value)
static <T> T[]
shrink(T[] array, int newLength)
static int[]
shuffle(int... data)
static long[]
shuffle(long... data)
static <E> E[]
shuffle(E[] array, int startIndex, int endIndex)
static int[]
shuffle(FastRandom random, int... data)
static long[]
shuffle(FastRandom random, long... data)
static <T> T[]
shuffle(T... data)
static int
smoothCapacityDecrease(int oldCapacity)
static int
smoothCapacityIncrease(int oldCapacity)
static byte[]
subArray(byte[] array, int offset, int length)
static char[]
subArray(char[] array, int offset, int length)
static <T> T[]
subArray(T[] array, int offset, int length)
static <T> T[]
toReversed(T[] array)
static <T> T[]
toReversed(T[] array, int offset, int length)
static <T> T[]
uncheckedFill(T[] array, int offset, int bound, Supplier<? extends T> supplier)
static int
validateArrayIndex(int arrayLength, int index)
static int
validateArrayRange(int arrayLength, int offset, int length)
static int
validateArrayRange(Object[] array, int offset, int length)
static void
validateRange0toUpperBound(int upperBound, int offset, int length)
static int
validIndex(int index, Object[] array)
-
Constructor Details
-
XArrays
public XArrays()
-
-
Method Details
-
validateRange0toUpperBound
public static final void validateRange0toUpperBound(int upperBound, int offset, int length) -
validateArrayIndex
public static final int validateArrayIndex(int arrayLength, int index) -
validIndex
public static final int validIndex(int index, Object[] array) throws ArrayIndexOutOfBoundsException- Throws:
ArrayIndexOutOfBoundsException
-
validateArrayRange
-
validateArrayRange
public static final int validateArrayRange(int arrayLength, int offset, int length) -
checkBounds
-
checkBounds
public static final void checkBounds(int arrayLength, int start, int bound) -
hasNoContent
Returns if the passed array is either null or has the length 0.- Parameters:
array
-
-
fill
-
fill
-
uncheckedFill
public static final <T> T[] uncheckedFill(T[] array, int offset, int bound, Supplier<? extends T> supplier) -
fill
public static final <T> T[] fill(T[] array, T fillElement, int fromIndex, int toIndex) -
fill
public static final int[] fill(int[] array, int fillElement) -
fill
public static final char[] fill(char[] array, char fillElement) -
fill
public static final byte[] fill(byte[] array, byte fillElement) -
fill
public static final float[] fill(float[] array, float fillElement) -
fill
public static final double[] fill(double[] array, double fillElement) -
clear
public static final <T> T[] clear(T[] array) -
replicate
public static final <T> T[] replicate(T subject, int times) -
subArray
public static final <T> T[] subArray(T[] array, int offset, int length) -
subArray
public static final byte[] subArray(byte[] array, int offset, int length) -
subArray
public static final char[] subArray(char[] array, int offset, int length) -
equals
Compares two Object arrays by reference of their content.Note that specific equality of each element is situational and thus cannot be a concern of a generic array comparison, just as it cannot be the concern of the element's class directly.
- Parameters:
array1
-array2
-
-
equals
public static final <E> boolean equals(E[] array1, int startIndex1, E[] array2, int startIndex2, int length, Equalator<? super E> comparator) -
equals
public static final boolean equals(byte[] a, byte[] a2, int length) -
add
public static <T> T[] add(T[] array, T element) -
remove
public static <T> T[] remove(T[] array, int i) -
add
adds all elements of the first array and all elements of the second array into one result array. Handles null-arrays correctly. Always creates a new array instance.- Parameters:
a1
-a2
-
-
add
public static final int[] add(int[] a1, int... a2) -
add
public static final long[] add(long[] a1, long... a2) -
combine
-
combine
-
_intAdd
public static final int[] _intAdd(int[] a1, int... a2) -
merge
Merges the both passed arrays by taking all elements froma1
(even duplicates) and adds all elements ofa2
(also duplicates as well) that are not already contained ina1
.- Type Parameters:
T
-- Parameters:
a1
-a2
-
-
ensureContained
-
ensureContained
public static final <T> T[] ensureContained(T[] ts, T t) -
contains
public static final <E> boolean contains(E[] array, E element)This method checks ifarray
containselement
by object identity- Type Parameters:
E
- any type- Parameters:
array
- the array to be searched inelement
- the element to be searched (by identity)- Returns:
true
ifarray
containselement
by object identity, else false
-
eqContains
public static final <E> boolean eqContains(E[] array, E element) -
contains
public static final <T, S extends T> boolean contains(T[] array, S element, Equalator<? super T> cmp) -
containsId
-
containS
-
contains
public static final <E> boolean contains(Collection<? super E> c, E sample, Equalator<? super E> equalator) -
removeAllFromArray
public static <E> int removeAllFromArray(E[] array, int start, int bound, E e) throws ArrayIndexOutOfBoundsExceptionRemoved all occurances ofe
from arrayarray
.- Parameters:
array
- the array containing all elements.start
- the starting offset (inclusive lower bound)bound
- the bounding offset (exclusive upper bound)e
- the element to be removed- Returns:
- the number of removed elements
- Throws:
ArrayIndexOutOfBoundsException
-
removeAllFromArray
public static int removeAllFromArray(int[] array, int start, int bound, int e) throws ArrayIndexOutOfBoundsException- Throws:
ArrayIndexOutOfBoundsException
-
removeDuplicates
-
removeAllFromArray
public static <E> int removeAllFromArray(XGettingCollection<? extends E> elements, E[] array, int start, int bound) throws ArrayIndexOutOfBoundsException- Throws:
ArrayIndexOutOfBoundsException
-
removeAllFromArray
public static <E> int removeAllFromArray(E[] array, int start, int bound, E e, Equalator<? super E> equalator) throws ArrayIndexOutOfBoundsException- Throws:
ArrayIndexOutOfBoundsException
-
removeAllFromArray
public static <E> int removeAllFromArray(E[] array, int start, int bound, XGettingCollection<? extends E> elements, Equalator<? super E> equalator) throws ArrayIndexOutOfBoundsException- Throws:
ArrayIndexOutOfBoundsException
-
reverse
public static final <T> T[] reverse(T[] array) -
toReversed
public static final <T> T[] toReversed(T[] array) -
toReversed
public static final <T> T[] toReversed(T[] array, int offset, int length) -
copy
public static final <T> T[] copy(T[] array) -
copyTo
public static final <T, E extends T> T[] copyTo(XGettingCollection<E> source, T[] target) throws IndexBoundsExceptionAt least for Java 1.8, the types seem to not be checked. Passing a collection of Strings and a Number[] (meaning String extends Number) is not a compiler error. Bug / generics loophole.- Throws:
IndexBoundsException
-
copyTo
public static final <T, E extends T> T[] copyTo(XGettingCollection<E> source, T[] target, int targetOffset) throws IndexBoundsException- Throws:
IndexBoundsException
-
shuffle
-
shuffle
public static final <E> E[] shuffle(E[] array, int startIndex, int endIndex) -
shuffle
public static final int[] shuffle(int... data) -
shuffle
-
shuffle
public static final long[] shuffle(long... data) -
shuffle
-
arraycopy
public static <T, U extends T> T[] arraycopy(U[] src, int srcPos, T[] dest, int destPos, int length)Convenience method, calling eitherSystem.arraycopy(Object,int,Object,int,int)
forlength >= 0
orreverseArraycopy(Object[],int,Object[],int,int)
forlength < 0
and returnsdest
.
If length is known to be positive and performance badly matters or negative length shall be treated as an error, useSystem.arraycopy(Object,int,Object,int,int)
directly. Otherwise, this method is a convenient alternative to handle more flexible bi-directional array copying.- Type Parameters:
T
-U
-- Parameters:
src
-srcPos
-dest
-destPos
-length
-
-
enlarge
public static <T> T[] enlarge(T[] array, int newLength) -
shrink
public static <T> T[] shrink(T[] array, int newLength) -
removeFromIndex
public static <T> T removeFromIndex(T[] elements, int size, int i) -
containsNull
-
copyRange
public static <E> E[] copyRange(E[] elements, int offset, int length) -
filter
-
filterTo
-
replaceAllInArray
public static <E> int replaceAllInArray(E[] data, int startLow, int boundHigh, E oldElement, E newElement) -
replaceAllInArray
public static <E> int replaceAllInArray(E[] data, int startLow, int boundHigh, E sample, Equalator<? super E> equalator, E newElement) -
and
public static <T> T[] and(T[] a1, T[] a2) -
or
public static <T> T[] or(T[] a1, T[] a2) -
not
public static <T> T[] not(T[] a1, T[] a2) -
orderByIndices
public static <S, T extends S> S[] orderByIndices(T[] elements, int[] indices, int indicesOffset, S[] target) throws IllegalArgumentExceptionOrders the passed elements by the passed indices.- Parameters:
elements
- the elements to be sorted according to the passed indices.indices
- the indices defining the order in which the passed elements shall be rearranged.target
- the target array to receive the sorted elements.- Throws:
IllegalArgumentException
-
min
public static final int min(int... data) -
max
public static final int max(int... data) -
applies
-
search
-
count
-
reverseArraycopy
public static <T, U extends T> T[] reverseArraycopy(U[] src, int srcPos, T[] dest, int destPos, int length)Reverse order counterpart toSystem.arraycopy(Object,int,Object,int,int)
.Copies source elements from
src
, starting atsrcPos
in negative direction (-length
and copies them one by one todest
, starting atdestPos
in positive direction (+length
, thus effectively copying the elements in reverse order.- Parameters:
src
- the source array.srcPos
- starting position in the source array (the highest index for reverse iteration).dest
- the destination array.destPos
- starting position in the destination data (the lowest index in the target array).length
- the number of array elements to be copied in reverse order.- Throws:
ArrayIndexOutOfBoundsException
- if copying would cause access of data outside array bounds.ArrayStoreException
- if an element in thesrc
array could not be stored into thedest
array because of a type mismatch.NullPointerException
- if eithersrc
ordest
isnull
.
-
reverseArraycopy
public static int[] reverseArraycopy(int[] src, int srcPos, int[] dest, int destPos, int length) -
convertArray
public static final <T> T[] convertArray(Object[] objects, Class<T> type) throws ClassCastException- Throws:
ClassCastException
-
componentType
-
arrayHashCode
-
contains
public static final boolean contains(int[] values, int value) -
iterate
-
iterate
-
iterate
-
iterate
-
iterate
-
iterate
-
indexOf
-
indexOf
public static final <T> int indexOf(T element, T[] array) -
indexOf
public static final int indexOf(byte value, byte[] values) -
indexOf
public static final int indexOf(boolean value, boolean[] values) -
indexOf
public static final int indexOf(short value, short[] values) -
indexOf
public static final int indexOf(char value, char[] values) -
indexOf
public static final int indexOf(int value, int[] values) -
indexOf
public static final int indexOf(float value, float[] values) -
indexOf
public static final int indexOf(long value, long[] values) -
indexOf
public static final int indexOf(double value, double[] values) -
indexOf
public static final int indexOf(byte[] data, byte[] subject) -
indexOf
public static final int indexOf(byte[] data, byte[] subject, int startIndex) -
rebuild
public static final byte[] rebuild(byte[] oldArray, int newLength) -
rebuild
public static final boolean[] rebuild(boolean[] oldArray, int newLength) -
rebuild
public static final short[] rebuild(short[] oldArray, int newLength) -
rebuild
public static final char[] rebuild(char[] oldArray, int newLength) -
rebuild
public static final int[] rebuild(int[] oldArray, int newLength) -
rebuild
public static final float[] rebuild(float[] oldArray, int newLength) -
rebuild
public static final long[] rebuild(long[] oldArray, int newLength) -
rebuild
public static final double[] rebuild(double[] oldArray, int newLength) -
rebuild
public static final <E> E[] rebuild(E[] oldArray, int newLength) -
set_byteInBytes
public static final void set_byteInBytes(byte[] bytes, int index, byte value) -
set_booleanInBytes
public static final void set_booleanInBytes(byte[] bytes, int index, boolean value) -
set_shortInBytes
public static final void set_shortInBytes(byte[] bytes, int index, short value) -
set_charInBytes
public static final void set_charInBytes(byte[] bytes, int index, char value) -
set_intInBytes
public static final void set_intInBytes(byte[] bytes, int index, int value) -
set_floatInBytes
public static final void set_floatInBytes(byte[] bytes, int index, float value) -
set_longInBytes
public static final void set_longInBytes(byte[] bytes, int index, long value) -
set_doubleInBytes
public static final void set_doubleInBytes(byte[] bytes, int index, double value) -
smoothCapacityIncrease
public static final int smoothCapacityIncrease(int oldCapacity) -
smoothCapacityDecrease
public static final int smoothCapacityDecrease(int oldCapacity)
-