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[] 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(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 <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 <E> void
iterate(Consumer<? super E> iterator, E... elements)
static <E> void
iterate(Consumer<? super E> iterator, E[] elements, int offset, int length)
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 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 <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 int[]
shuffle(int... data)
static <E> E[]
shuffle(E[] array, int startIndex, int endIndex)
static int[]
shuffle(FastRandom random, int... data)
static <T> T[]
shuffle(T... data)
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(Object[] array, int offset, int length)
static void
validateRange0toUpperBound(int upperBound, int offset, int length)
static int
validIndex(int index, Object[] array)
-
-
-
Method Detail
-
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
public static final int validateArrayRange(Object[] array, int offset, int length)
-
checkBounds
public static final void checkBounds(Object[] array, int start, int bound)
-
hasNoContent
public static boolean hasNoContent(Object[] array)
Returns if the passed array is either null or has the length 0.- Parameters:
array
-
-
fill
public static final <T> T[] fill(T[] array, Supplier<? extends T> supplier)
-
fill
public static final <T> T[] fill(T[] array, int offset, int bound, Supplier<? extends T> supplier)
-
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
public static boolean equals(Object[] array1, Object[] array2)
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
@SafeVarargs public static final <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. 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
@SafeVarargs public static final <T> T[] combine(T[]... arrays)
-
combine
public static final <T,S extends T> T[] combine(Class<T> componentType, S[]... arrays)
-
_intAdd
public static final int[] _intAdd(int[] a1, int... a2)
-
merge
@SafeVarargs public static final <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
.- Type Parameters:
T
-- Parameters:
a1
-a2
-
-
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
public static final <E> boolean containsId(Collection<E> c, E element)
-
containS
public static final <E> boolean containS(Collection<E> c, E element)
-
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 ArrayIndexOutOfBoundsException
Removed 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
@SafeVarargs public static final <E> E[] removeDuplicates(E... elements)
-
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 IndexBoundsException
At 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
@SafeVarargs public static final <T> T[] shuffle(T... data)
-
shuffle
public static final <E> E[] shuffle(E[] array, int startIndex, int endIndex)
-
shuffle
public static final int[] shuffle(int... data)
-
shuffle
public static final int[] shuffle(FastRandom random, int... data)
-
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
-
-
containsNull
public static final boolean containsNull(Object[] data, int offset, int length)
-
copyRange
public static <E> E[] copyRange(E[] elements, int offset, int length)
-
filter
public static <E> E[] filter(E[] elements, Predicate<? super E> predicate)
-
filterTo
public static <E,C extends Consumer<? super E>> C filterTo(E[] elements, C target, Predicate<? super E> predicate)
-
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 IllegalArgumentException
Orders 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
public static final <T> boolean applies(T[] array, Predicate<? super T> predicate)
-
search
public static final <T> T search(T[] array, Predicate<? super T> predicate)
-
count
public static final <T> int count(T[] array, Predicate<? super T> predicate)
-
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
public static <E> Class<E> componentType(E[] array)
-
arrayHashCode
public static final int arrayHashCode(Object[] data, int size)
-
contains
public static final boolean contains(int[] values, int value)
-
iterate
@SafeVarargs public static <E> void iterate(Consumer<? super E> iterator, E... elements)
-
iterate
public static <E> void iterate(Consumer<? super E> iterator, E[] elements, int offset, int length)
-
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)
-
-