Package civitas.celestis.util.array
Class AtomicArray<E>
java.lang.Object
civitas.celestis.util.array.AtomicArray<E>
- Type Parameters:
E- The type of element to reference
- All Implemented Interfaces:
BaseArray<E>,SafeArray<E>,Serializable,Iterable<E>
A type-safe array which stores atomic references as opposed to the values themselves.
Atomic references are thread-safe as long as the underlying elements are not concurrently
modifiable, or have other thread-unsafe properties.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionAtomicArray(int length) Creates a new atomic array.AtomicArray(SafeArray<? extends E> a) Creates a new atomic array. -
Method Summary
Modifier and TypeMethodDescriptionE[]array()Returns a primitive array containing the elements of this array in their proper order.booleanReturns whether this array contains the provided objectobj.booleancontainsAll(Iterable<?> i) Returns whether this array contains multiple objects.booleanChecks for equality between this array and the provided objectobj.voidFills this array with the provided valuev.voidFills every empty slot of this array with the provided valuev.voidFills every slot of this array between the range of[s, e)with the provided valuev.voidforEach(BiConsumer<? super Integer, ? super E> a) Executes the provided actionaonce for each element of this array.voidExecutes the provided actionaonce for each element of this array.get(int i) Returns theith element of this array.getOrDefault(int i, E e) Returns theith element of this array, but returns the provided fallback valueeinstead of the value at the specified index isnull.inthashCode()Returns the hash code of this instance.iterator()Returns an iterator over every element of this array.intlength()Returns the length of this array.list()Returns an unmodifiable list containing the elements of this array in their proper order.<F> SafeArray<F>Applies the provided mapper functionfto each element of this array, then returns a new array containing the return values of the functionf.mapToDouble(ToDoubleFunction<? super E> f) Applies the provided mapper functionfto each element of this array, then returns a new double array containing the return values of the functionf.mapToFloat(ToFloatFunction<? super E> f) Applies the provided mapper functionfto each element of this array, then returns a new float array containing the return values of the functionf.mapToInt(ToIntFunction<? super E> f) Applies the provided mapper functionfto each element of this array, then returns a new integer array containing the return values of the functionf.mapToLong(ToLongFunction<? super E> f) Applies the provided mapper functionfto each element of this array, then returns a new long array containing the return values of the functionf.<F,G> SafeArray<G> merge(SafeArray<F> a, BiFunction<? super E, ? super F, ? extends G> f) Between this array and the provided arraya, this applies the merger functionfto each corresponding pair of elements, then returns a new array containing the return values of the merger functionf.static <E> AtomicArray<E>of(E... values) Creates a new atomic array from the provided array of values.voidreplaceAll(E oldValue, E newValue) Replaces all instances of the old value to the new value.voidreplaceFirst(E oldValue, E newValue) Replaces only the first instance of the old value to the new value.voidreplaceLast(E oldValue, E newValue) Replaces only the last instance of the old value to the new value.resize(int size) Returns a resized array whose elements are mapped from that of this array's elements.voidSets theith element of this array.voidSets a sub-array of this array to the values of the provided sub-arraya.voidshuffle()Shuffles this array, randomizing its elements' order.voidsort()Sorts this array by its natural ascending order.voidsort(Comparator<? super E> c) Sorts this array using the provided comparator functionc.stream()Returns a stream whose source is the elements of this array.subArray(int s, int e) Returns a sub-array of this array which represents a portion of this array between the range of[s, e).toString()Serializes this array into a string.tuple()Returns a tuple containing the elements of this array in their proper order.voidupdate(int i, UnaryOperator<E> f) Updates theith element of this array with the provided update functionf.voidupdate(BiFunction<? super Integer, ? super E, E> f) Applies the provided update functionfto each element of this array, then assigns the return value of the function to the corresponding index of this array.voidupdate(UnaryOperator<E> f) Applies the provided update functionfto each element of this array, then assigns the return value of the function to the corresponding index of this array.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
spliterator
-
Constructor Details
-
AtomicArray
public AtomicArray(int length) Creates a new atomic array.- Parameters:
length- The length of which to initialize this array to
-
AtomicArray
Creates a new atomic array.- Parameters:
a- The array of which to copy values from
-
-
Method Details
-
of
Creates a new atomic array from the provided array of values.- Type Parameters:
E- The type of element to reference- Parameters:
values- The values of which to reference- Returns:
- A new atomic array referencing the provided values
-
length
public int length()Returns the length of this array. -
contains
Returns whether this array contains the provided objectobj. -
containsAll
Returns whether this array contains multiple objects.- Specified by:
containsAllin interfaceSafeArray<E>- Parameters:
i- The iterable object of which to check for containment- Returns:
trueif this array contains every element of the iterable object
-
get
Returns theith element of this array.- Specified by:
getin interfaceSafeArray<E>- Parameters:
i- The index of the element to get- Returns:
- The
ith element of this array - Throws:
IndexOutOfBoundsException- When the indexiis out of bounds
-
getOrDefault
Returns theith element of this array, but returns the provided fallback valueeinstead of the value at the specified index isnull.- Specified by:
getOrDefaultin interfaceSafeArray<E>- Parameters:
i- The index of the element to gete- The fallback value to default to when the value isnull- Returns:
- The
ith element of this array if present, the fallback valueeotherwise - Throws:
IndexOutOfBoundsException- When the indexiis out of bounds
-
set
Sets theith element of this array.- Specified by:
setin interfaceSafeArray<E>- Parameters:
i- The index of the element to sete- The element of which to set to- Throws:
IndexOutOfBoundsException- When the indexiis out of bounds
-
update
Updates theith element of this array with the provided update functionf.- Specified by:
updatein interfaceSafeArray<E>- Parameters:
i- The index of the element to updatef- The update function of which to apply to the element- Throws:
IndexOutOfBoundsException- When the indexiis out of bounds
-
fill
Fills this array with the provided valuev. Every element will be reassigned. -
fillEmpty
Fills every empty slot of this array with the provided valuev. All occurrences ofnullwill be replaced with the provided value. -
fillRange
Fills every slot of this array between the range of[s, e)with the provided valuev. -
update
Applies the provided update functionfto each element of this array, then assigns the return value of the function to the corresponding index of this array. -
update
Applies the provided update functionfto each element of this array, then assigns the return value of the function to the corresponding index of this array. The first parameter is the index of the value which was provided. -
replaceAll
Replaces all instances of the old value to the new value.- Specified by:
replaceAllin interfaceSafeArray<E>- Parameters:
oldValue- The old value to replacenewValue- The new value to replace to
-
replaceFirst
Replaces only the first instance of the old value to the new value. (starting from index0, and incrementing upwards)- Specified by:
replaceFirstin interfaceSafeArray<E>- Parameters:
oldValue- The old value to replacenewValue- The new value to replace to
-
replaceLast
Replaces only the last instance of the old value to the new value. (starting from the last index, and decrementing downwards)- Specified by:
replaceLastin interfaceSafeArray<E>- Parameters:
oldValue- The old value to replacenewValue- The new value to replace to
-
subArray
Returns a sub-array of this array which represents a portion of this array between the range of[s, e). Changes in the sub-array will be reflected to this array. For example, callinga.subArray(1, 3).fill(null);will have the same effect as setting the values of indices1, 2of the original array tonull.- Specified by:
subArrayin interfaceSafeArray<E>- Parameters:
s- The starting index at which to start creating the sub-array (inclusive)e- The ending index at which to stop creating the sub-array (exclusive)- Returns:
- The sub-array representing the index range of
[s, e) - Throws:
IndexOutOfBoundsException- When the range is invalid, or is out of bounds
-
setRange
public void setRange(int s, int e, @Nonnull SafeArray<? extends E> a) throws IndexOutOfBoundsException Sets a sub-array of this array to the values of the provided sub-arraya.- Specified by:
setRangein interfaceSafeArray<E>- Parameters:
s- The starting index at which to start copying values from (inclusive)e- The ending index at which to stop copying values from (exclusive)a- The sub-array containing the values to assign to this array- Throws:
IndexOutOfBoundsException- When the range is invalid, or is out of bounds
-
resize
Returns a resized array whose elements are mapped from that of this array's elements. If the new array is larger than this array, the unmappable part will not be populated, leaving it uninitialized asnull. -
shuffle
public void shuffle()Shuffles this array, randomizing its elements' order. -
sort
Sorts this array by its natural ascending order. This operation will only succeed if the elementEis an instance ofComparable.- Specified by:
sortin interfaceBaseArray<E>- Specified by:
sortin interfaceSafeArray<E>- Throws:
UnsupportedOperationException- When at least one element cannot be cast toComparable
-
sort
Sorts this array using the provided comparator functionc. -
map
Applies the provided mapper functionfto each element of this array, then returns a new array containing the return values of the functionf. -
mapToDouble
Applies the provided mapper functionfto each element of this array, then returns a new double array containing the return values of the functionf.- Specified by:
mapToDoublein interfaceSafeArray<E>- Parameters:
f- The function of which to apply to each element of this array- Returns:
- The resulting array
- See Also:
-
mapToFloat
Applies the provided mapper functionfto each element of this array, then returns a new float array containing the return values of the functionf.- Specified by:
mapToFloatin interfaceSafeArray<E>- Parameters:
f- The function of which to apply to each element of this array- Returns:
- The resulting array
- See Also:
-
mapToLong
Applies the provided mapper functionfto each element of this array, then returns a new long array containing the return values of the functionf. -
mapToInt
Applies the provided mapper functionfto each element of this array, then returns a new integer array containing the return values of the functionf. -
merge
@Nonnull public <F,G> SafeArray<G> merge(@Nonnull SafeArray<F> a, @Nonnull BiFunction<? super E, ? super F, throws IllegalArgumentException? extends G> f) Between this array and the provided arraya, this applies the merger functionfto each corresponding pair of elements, then returns a new array containing the return values of the merger functionf. In other words, this merges the two arrays into a new array using the provided merger functionf.- Specified by:
mergein interfaceSafeArray<E>- Type Parameters:
F- The type of element to merge this array withG- The type of element to merge the two arrays to- Parameters:
a- The array of which to merge this array withf- The merger function to handle the merging of the two arrays- Returns:
- The resulting array
- Throws:
IllegalArgumentException- When the provided arraya's length is different from that of this array's length
-
iterator
Returns an iterator over every element of this array. -
forEach
Executes the provided actionaonce for each element of this array. A reference to the element is provided as the first parameter of the action. -
forEach
Executes the provided actionaonce for each element of this array. The index of the element is provided as the first parameter of the action, and a reference to the element is provided as the second parameter of the action. -
array
Returns a primitive array containing the elements of this array in their proper order. -
stream
Returns a stream whose source is the elements of this array. -
list
Returns an unmodifiable list containing the elements of this array in their proper order. -
tuple
Returns a tuple containing the elements of this array in their proper order. UnlikeSafeArray.list(), this operation cannot fail as tuples inherently support the usage ofnullas their values. -
equals
Checks for equality between this array and the provided objectobj. -
toString
Serializes this array into a string. -
hashCode
public int hashCode()Returns the hash code of this instance.
-