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 SummaryConstructors Constructor Description XArrays()
- 
Method SummaryModifier 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> booleanapplies(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 >= 0orreverseArraycopy(Object[],int,Object[],int,int)forlength < 0and 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 intarrayHashCode(Object[] data, int size)static voidcheckBounds(int arrayLength, int start, int bound)static voidcheckBounds(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 booleancontains(int[] values, int value)static <E> booleancontains(E[] array, E element)This method checks ifarraycontainselementby object identitystatic <E> booleancontains(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> booleancontainS(Collection<E> c, E element)static <E> booleancontainsId(Collection<E> c, E element)static booleancontainsNull(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> intcount(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> booleaneqContains(E[] array, E element)static booleanequals(byte[] a, byte[] a2, int length)static <E> booleanequals(E[] array1, int startIndex1, E[] array2, int startIndex2, int length, Equalator<? super E> comparator)static booleanequals(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 booleanhasNoContent(Object[] array)Returns if the passed array is either null or has the length 0.static intindexOf(boolean value, boolean[] values)static intindexOf(byte[] data, byte[] subject)static intindexOf(byte[] data, byte[] subject, int startIndex)static intindexOf(byte value, byte[] values)static intindexOf(char value, char[] values)static intindexOf(double value, double[] values)static intindexOf(float value, float[] values)static intindexOf(int value, int[] values)static intindexOf(long value, long[] values)static intindexOf(short value, short[] values)static <T, S> intindexOf(S sample, T[] array, BiPredicate<T,S> predicate)static <T> intindexOf(T element, T[] array)static voiditerate(int[] elements, int offset, int length, _intProcedure iterator)static voiditerate(int[] elements, _intProcedure iterator)static voiditerate(long[] elements, int offset, int length, _longProcedure iterator)static voiditerate(long[] elements, _longProcedure iterator)static <E> voiditerate(E[] elements, int offset, int length, Consumer<? super E> iterator)static <E, I extends Consumer<? super E>>
 Iiterate(E[] elements, I iterator)static intmax(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 intmin(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 intremoveAllFromArray(int[] array, int start, int bound, int e)static <E> intremoveAllFromArray(E[] array, int start, int bound, E e)Removed all occurances ofefrom arrayarray.static <E> intremoveAllFromArray(E[] array, int start, int bound, E e, Equalator<? super E> equalator)static <E> intremoveAllFromArray(E[] array, int start, int bound, XGettingCollection<? extends E> elements, Equalator<? super E> equalator)static <E> intremoveAllFromArray(XGettingCollection<? extends E> elements, E[] array, int start, int bound)static <E> E[]removeDuplicates(E... elements)static <T> TremoveFromIndex(T[] elements, int size, int i)static <E> intreplaceAllInArray(E[] data, int startLow, int boundHigh, E oldElement, E newElement)static <E> intreplaceAllInArray(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> Tsearch(T[] array, Predicate<? super T> predicate)static voidset_booleanInBytes(byte[] bytes, int index, boolean value)static voidset_byteInBytes(byte[] bytes, int index, byte value)static voidset_charInBytes(byte[] bytes, int index, char value)static voidset_doubleInBytes(byte[] bytes, int index, double value)static voidset_floatInBytes(byte[] bytes, int index, float value)static voidset_intInBytes(byte[] bytes, int index, int value)static voidset_longInBytes(byte[] bytes, int index, long value)static voidset_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 intsmoothCapacityDecrease(int oldCapacity)static intsmoothCapacityIncrease(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 intvalidateArrayIndex(int arrayLength, int index)static intvalidateArrayRange(int arrayLength, int offset, int length)static intvalidateArrayRange(Object[] array, int offset, int length)static voidvalidateRange0toUpperBound(int upperBound, int offset, int length)static intvalidIndex(int index, Object[] array)
- 
Constructor Details- 
XArrayspublic XArrays()
 
- 
- 
Method Details- 
validateRange0toUpperBoundpublic static final void validateRange0toUpperBound(int upperBound, int offset, int length)
- 
validateArrayIndexpublic static final int validateArrayIndex(int arrayLength, int index)
- 
validIndexpublic static final int validIndex(int index, Object[] array) throws ArrayIndexOutOfBoundsException- Throws:
- ArrayIndexOutOfBoundsException
 
- 
validateArrayRange
- 
validateArrayRangepublic static final int validateArrayRange(int arrayLength, int offset, int length)
- 
checkBounds
- 
checkBoundspublic static final void checkBounds(int arrayLength, int start, int bound)
- 
hasNoContentReturns if the passed array is either null or has the length 0.- Parameters:
- array-
 
- 
fill
- 
fill
- 
uncheckedFillpublic static final <T> T[] uncheckedFill(T[] array, int offset, int bound, Supplier<? extends T> supplier)
- 
fillpublic static final <T> T[] fill(T[] array, T fillElement, int fromIndex, int toIndex)
- 
fillpublic static final int[] fill(int[] array, int fillElement)
- 
fillpublic static final char[] fill(char[] array, char fillElement)
- 
fillpublic static final byte[] fill(byte[] array, byte fillElement)
- 
fillpublic static final float[] fill(float[] array, float fillElement)
- 
fillpublic static final double[] fill(double[] array, double fillElement)
- 
clearpublic static final <T> T[] clear(T[] array)
- 
replicatepublic static final <T> T[] replicate(T subject, int times)
- 
subArraypublic static final <T> T[] subArray(T[] array, int offset, int length)
- 
subArraypublic static final byte[] subArray(byte[] array, int offset, int length)
- 
subArraypublic static final char[] subArray(char[] array, int offset, int length)
- 
equalsCompares 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-
 
- 
equalspublic static final <E> boolean equals(E[] array1, int startIndex1, E[] array2, int startIndex2, int length, Equalator<? super E> comparator)
- 
equalspublic static final boolean equals(byte[] a, byte[] a2, int length)
- 
addpublic static <T> T[] add(T[] array, T element)
- 
removepublic static <T> T[] remove(T[] array, int i)
- 
addadds 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-
 
- 
addpublic static final int[] add(int[] a1, int... a2)
- 
addpublic static final long[] add(long[] a1, long... a2)
- 
combine
- 
combine
- 
_intAddpublic static final int[] _intAdd(int[] a1, int... a2)
- 
mergeMerges 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
- 
ensureContainedpublic static final <T> T[] ensureContained(T[] ts, T t)
- 
containspublic static final <E> boolean contains(E[] array, E element)This method checks ifarraycontainselementby object identity- Type Parameters:
- E- any type
- Parameters:
- array- the array to be searched in
- element- the element to be searched (by identity)
- Returns:
- trueif- arraycontains- elementby object identity, else false
 
- 
eqContainspublic static final <E> boolean eqContains(E[] array, E element)
- 
containspublic static final <T, S extends T> boolean contains(T[] array, S element, Equalator<? super T> cmp)
- 
containsId
- 
containS
- 
containspublic static final <E> boolean contains(Collection<? super E> c, E sample, Equalator<? super E> equalator)
- 
removeAllFromArraypublic static <E> int removeAllFromArray(E[] array, int start, int bound, E e) throws ArrayIndexOutOfBoundsExceptionRemoved all occurances ofefrom 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
 
- 
removeAllFromArraypublic static int removeAllFromArray(int[] array, int start, int bound, int e) throws ArrayIndexOutOfBoundsException- Throws:
- ArrayIndexOutOfBoundsException
 
- 
removeDuplicates
- 
removeAllFromArraypublic static <E> int removeAllFromArray(XGettingCollection<? extends E> elements, E[] array, int start, int bound) throws ArrayIndexOutOfBoundsException- Throws:
- ArrayIndexOutOfBoundsException
 
- 
removeAllFromArraypublic static <E> int removeAllFromArray(E[] array, int start, int bound, E e, Equalator<? super E> equalator) throws ArrayIndexOutOfBoundsException- Throws:
- ArrayIndexOutOfBoundsException
 
- 
removeAllFromArraypublic static <E> int removeAllFromArray(E[] array, int start, int bound, XGettingCollection<? extends E> elements, Equalator<? super E> equalator) throws ArrayIndexOutOfBoundsException- Throws:
- ArrayIndexOutOfBoundsException
 
- 
reversepublic static final <T> T[] reverse(T[] array)
- 
toReversedpublic static final <T> T[] toReversed(T[] array)
- 
toReversedpublic static final <T> T[] toReversed(T[] array, int offset, int length)
- 
copypublic static final <T> T[] copy(T[] array)
- 
copyTopublic 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
 
- 
copyTopublic static final <T, E extends T> T[] copyTo(XGettingCollection<E> source, T[] target, int targetOffset) throws IndexBoundsException- Throws:
- IndexBoundsException
 
- 
shuffle
- 
shufflepublic static final <E> E[] shuffle(E[] array, int startIndex, int endIndex)
- 
shufflepublic static final int[] shuffle(int... data)
- 
shuffle
- 
shufflepublic static final long[] shuffle(long... data)
- 
shuffle
- 
arraycopypublic 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 >= 0orreverseArraycopy(Object[],int,Object[],int,int)forlength < 0and 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-
 
- 
enlargepublic static <T> T[] enlarge(T[] array, int newLength)
- 
shrinkpublic static <T> T[] shrink(T[] array, int newLength)
- 
removeFromIndexpublic static <T> T removeFromIndex(T[] elements, int size, int i)
- 
containsNull
- 
copyRangepublic static <E> E[] copyRange(E[] elements, int offset, int length)
- 
filter
- 
filterTo
- 
replaceAllInArraypublic static <E> int replaceAllInArray(E[] data, int startLow, int boundHigh, E oldElement, E newElement)
- 
replaceAllInArraypublic static <E> int replaceAllInArray(E[] data, int startLow, int boundHigh, E sample, Equalator<? super E> equalator, E newElement)
- 
andpublic static <T> T[] and(T[] a1, T[] a2)
- 
orpublic static <T> T[] or(T[] a1, T[] a2)
- 
notpublic static <T> T[] not(T[] a1, T[] a2)
- 
orderByIndicespublic 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
 
- 
minpublic static final int min(int... data)
- 
maxpublic static final int max(int... data)
- 
applies
- 
search
- 
count
- 
reverseArraycopypublic 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 atsrcPosin negative direction (-lengthand copies them one by one todest, starting atdestPosin 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 the- srcarray could not be stored into the- destarray because of a type mismatch.
- NullPointerException- if either- srcor- destis- null.
 
- 
reverseArraycopypublic static int[] reverseArraycopy(int[] src, int srcPos, int[] dest, int destPos, int length)
- 
convertArraypublic static final <T> T[] convertArray(Object[] objects, Class<T> type) throws ClassCastException- Throws:
- ClassCastException
 
- 
componentType
- 
arrayHashCode
- 
containspublic static final boolean contains(int[] values, int value)
- 
iterate
- 
iterate
- 
iterate
- 
iterate
- 
iterate
- 
iterate
- 
indexOf
- 
indexOfpublic static final <T> int indexOf(T element, T[] array)
- 
indexOfpublic static final int indexOf(byte value, byte[] values)
- 
indexOfpublic static final int indexOf(boolean value, boolean[] values)
- 
indexOfpublic static final int indexOf(short value, short[] values)
- 
indexOfpublic static final int indexOf(char value, char[] values)
- 
indexOfpublic static final int indexOf(int value, int[] values)
- 
indexOfpublic static final int indexOf(float value, float[] values)
- 
indexOfpublic static final int indexOf(long value, long[] values)
- 
indexOfpublic static final int indexOf(double value, double[] values)
- 
indexOfpublic static final int indexOf(byte[] data, byte[] subject)
- 
indexOfpublic static final int indexOf(byte[] data, byte[] subject, int startIndex)
- 
rebuildpublic static final byte[] rebuild(byte[] oldArray, int newLength)
- 
rebuildpublic static final boolean[] rebuild(boolean[] oldArray, int newLength)
- 
rebuildpublic static final short[] rebuild(short[] oldArray, int newLength)
- 
rebuildpublic static final char[] rebuild(char[] oldArray, int newLength)
- 
rebuildpublic static final int[] rebuild(int[] oldArray, int newLength)
- 
rebuildpublic static final float[] rebuild(float[] oldArray, int newLength)
- 
rebuildpublic static final long[] rebuild(long[] oldArray, int newLength)
- 
rebuildpublic static final double[] rebuild(double[] oldArray, int newLength)
- 
rebuildpublic static final <E> E[] rebuild(E[] oldArray, int newLength)
- 
set_byteInBytespublic static final void set_byteInBytes(byte[] bytes, int index, byte value)
- 
set_booleanInBytespublic static final void set_booleanInBytes(byte[] bytes, int index, boolean value)
- 
set_shortInBytespublic static final void set_shortInBytes(byte[] bytes, int index, short value)
- 
set_charInBytespublic static final void set_charInBytes(byte[] bytes, int index, char value)
- 
set_intInBytespublic static final void set_intInBytes(byte[] bytes, int index, int value)
- 
set_floatInBytespublic static final void set_floatInBytes(byte[] bytes, int index, float value)
- 
set_longInBytespublic static final void set_longInBytes(byte[] bytes, int index, long value)
- 
set_doubleInBytespublic static final void set_doubleInBytes(byte[] bytes, int index, double value)
- 
smoothCapacityIncreasepublic static final int smoothCapacityIncrease(int oldCapacity)
- 
smoothCapacityDecreasepublic static final int smoothCapacityDecrease(int oldCapacity)
 
-