Class Array<T>
java.lang.Object
io.github.xrickastley.sevenelements.util.Array<T>
- All Implemented Interfaces:
Iterable<T>,Collection<T>,List<T>
Wraps a
CopyOnWriteArrayList around the JavaScript Array API, bringing the implementation of
JavaScript's array methods to Java.-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionArray()Creates anArrayfrom anotherArray.Creates anArrayfrom the specifiedIterable.Array(Collection<T> collection) Creates anArrayfrom the specifiedCollection.Creates anArrayfrom the specifiedIterator.Creates anArrayfrom the specifiedStream.Creates anArraywith the specified elements. -
Method Summary
Modifier and TypeMethodDescriptionvoidACopyOnWriteArrayListmethod, inserts the specified element at the specified position in thisArray.booleanACopyOnWriteArrayListmethod, appends the specified element to the end of thisArray.booleanaddAll(int index, @NotNull Collection<? extends T> c) ACopyOnWriteArrayListmethod, inserts all the elements in the specified collection into this list, starting at the specified position.booleanaddAll(@NotNull Collection<? extends T> c) ACopyOnWriteArrayListmethod, appends all the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.booleanAlias forevery(java.util.function.Predicate<? super T>), determines whether all the members of thisArraysatisfy the specified predicate.booleanAlias forsome(java.util.function.Predicate<T>), determines whether at least one element of thisArraysatisfy the specified predicate.at(int index) Returns the element located at the specified index.voidclear()AnCopyOnWriteArrayListmethod, removes all the elements from thisArray.Combines two or more arrays.Combines two or more arrays.booleanbooleancontainsAll(@NotNull Collection<?> c) ACopyOnWriteArrayListmethod, returnstrueif thisArraycontains all the elements of the specified collection.booleanDetermines whether any elements of thisArraysatisfy the specified predicate.Changes all array elements tovalueand returns the modified array.Changes all array elements fromstarttoarray.length() - 1intovalueand returns the modified array.Changes all array elements fromstarttoendintovalueand returns the modified array.Returns a newArrayconsisting of all the elements of thisArraythat satisfy the specified predicate.Returns the value of the first element in thisArraythat satisfies the given predicate, andnullotherwise.findAsOptional(Predicate<? super T> predicate) Returns the value of the first element in thisArraythat satisfies the given predicate as anOptional.intReturns the index of the first element in thisArraythat satisfies the given predicate, and-1otherwise.findIndexAsOptional(Predicate<? super T> predicate) Returns the index of the first element in thisArraythat satisfies the given predicate as anOptional.Returns the value of the last element in thisArraythat satisfies the given predicate, andnullotherwise.findLastAsOptional(Predicate<? super T> predicate) Returns the value of the last element in thisArraythat satisfies the given predicate as anOptional.intfindLastIndex(Predicate<? super T> predicate) Returns the index of the last element in thisArraythat satisfies the given predicate, and-1otherwise.get(int index) ACopyOnWriteArrayListmethod and an alias forat(int), returns the element located at the specified index.booleanReturnstrueif thisArraycontains the specified element.intReturns the index of the first occurrence of the specified element in thisArray, or-1if thisArraydoes not contain the element.booleanisEmpty()ACopyOnWriteArrayListmethod, returnstrueif this list contains no elements.iterator()ACopyOnWriteArrayListmethod, returns an iterator over the elements in this list in proper sequence.join()Joins all the elements of thisArrayinto aString, separated by commas.Joins all the elements of thisArrayinto aString, separated by the specified separator string.intlastIndexOf(Object object) Returns the index of the last occurrence of the specified element in thisArray, or-1if thisArraydoes not contain the element.intlength()Returns the length (the number of elements) in thisArray.@NotNull ListIterator<T>@NotNull ListIterator<T>listIterator(int index) <R> Array<R>Returns a newArrayconsisting of the results of applying the given function to the elements of thisArray.private intnormalizeIndex(int index) Normalizes index inputs, resolving negative and positive array indexes.AStreammethod, performs the specified action for each element in an array, then returns theArray.pop()Removes the element at the end of thisArrayand returns it.intAppends new elements to the end of thisArrayand returns the new length of thisArray.<U> Ureduce(BiFunction<U, T, U> accumulator, U initialValue) Performs a reduction on the elements of thisArray, using the providedinitialValueandaccumulatorfunction.<U> UreduceRight(BiFunction<U, T, U> accumulator, U initialValue) A right-sided version ofreduce(java.util.function.BiFunction<U, T, U>, U), performs a reduction on the elements of thisArraystarting from the last element to the first element, using the providedinitialValueandaccumulatorfunction.remove(int index) ACopyOnWriteArrayListmethod, removes the element at the specified position in this list and shifts any subsequent elements to the left (subtracts one from their indices).booleanACopyOnWriteArrayListmethod, removes the first occurrence of the specified element from thisArray, if it is present.booleanremoveAll(@NotNull Collection<?> c) booleanretainAll(@NotNull Collection<?> c) reverse()Reverses the order of elements of thisArray.shift()Removes the first element from an array and returns it.intsize()ACopyOnWriteArrayListmethod and an alias forlength(), returns the length (the number of elements) in thisArray.slice()Returns a copy of a section of anArrayfrom0toarray.length() - 1.slice(int start) Returns a copy of a section of anArrayfromstarttoarray.length() - 1.slice(int start, int end) Returns a copy of a section of anArray.booleanDetermines whether at least one element of thisArraysatisfy the specified predicate.A renamed implementation of Array.prototype.sort(), sorts an array's elements' string value in ascending, ASCII character order.sortElements(BiFunction<T, T, Integer> compareFn) A renamed implementation of Array.prototype.sort(), sorts thisArrayin place according to the order induced by the specifiedBiFunction.stream()subList(int fromIndex, int toIndex) ACopyOnWriteArrayListmethod, returns a view of the portion of this list betweenfromIndex, inclusive, andtoIndex, exclusive.T @NotNull []toArray()<U> U @NotNull []toArray(U @NotNull [] a) Reverses the order of elements of thisArray.toSorted(BiFunction<T, T, Integer> compareFn) Sorts thisArrayaccording to the order induced by the specifiedComparator.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, toArrayMethods inherited from interface java.util.List
equals, hashCode, replaceAll, sort, spliterator
-
Field Details
-
array
-
-
Constructor Details
-
Array
public Array() -
Array
Creates anArraywith the specified elements.- Parameters:
elements- The elements of the array.
-
Array
Creates anArrayfrom the specifiedIterable.- Parameters:
iterable- TheIterableto create the array from.
-
Array
Creates anArrayfrom the specifiedStream. This consumes all the elements of the stream.- Parameters:
stream- TheStreamto create the array from.
-
Array
Creates anArrayfrom the specifiedIterator. This consumes all the elements in the iterator.- Parameters:
iterator- TheIteratorto create the array from.
-
Array
Creates anArrayfrom the specifiedCollection.- Parameters:
collection- TheCollectionto create the array from.
-
Array
Creates anArrayfrom anotherArray.
This array will have all the elements ofarray, but any changes made to this array will not reflect in the original array.- Parameters:
array- TheArrayto create the array from.
-
-
Method Details
-
add
ACopyOnWriteArrayListmethod, appends the specified element to the end of thisArray. -
add
ACopyOnWriteArrayListmethod, inserts the specified element at the specified position in thisArray. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). -
addAll
ACopyOnWriteArrayListmethod, appends all the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. -
addAll
ACopyOnWriteArrayListmethod, inserts all the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified collection's iterator. -
set
-
size
public int size()ACopyOnWriteArrayListmethod and an alias forlength(), returns the length (the number of elements) in thisArray. This is a number one higher than the highest index in the array. -
at
Returns the element located at the specified index.- Parameters:
index- The zero-based index of the desired element. A negative index will count back from the last element.
-
clear
public void clear()AnCopyOnWriteArrayListmethod, removes all the elements from thisArray. TheArraywill be empty after this call returns. -
concat
Combines two or more arrays. This returns a newArraywithout modifying any existing Arrays.- Parameters:
items- Additional items to add to the end of the array.
-
concat
Combines two or more arrays. This returns a newArraywithout modifying any existing Arrays.- Parameters:
arrays- Additional arrays with the items to add at the end of the array. These arrays are iterated over, with their inner elements being added as elements of the array.
-
contains
Alias forincludes(java.lang.Object), returnstrueif thisArraycontains the specified element. More formally, returnstrueif and only if thisArraycontains at least one elementesuch thatObjects.equals(o, e). -
containsAll
ACopyOnWriteArrayListmethod, returnstrueif thisArraycontains all the elements of the specified collection.- Specified by:
containsAllin interfaceCollection<T>- Specified by:
containsAllin interfaceList<T>
-
every
Determines whether any elements of thisArraysatisfy the specified predicate.- Parameters:
predicate- The predicate to apply to the elements of thisArray.- See Also:
-
allMatch
Alias forevery(java.util.function.Predicate<? super T>), determines whether all the members of thisArraysatisfy the specified predicate.- Parameters:
predicate- The predicate to apply to the elements of thisArray.- See Also:
-
anyMatch
Alias forsome(java.util.function.Predicate<T>), determines whether at least one element of thisArraysatisfy the specified predicate.- Parameters:
predicate- The predicate to apply to the elements of thisArray.- See Also:
-
fill
Changes all array elements tovalueand returns the modified array.- Parameters:
value- The value to fill this array with.
-
fill
Changes all array elements fromstarttoarray.length() - 1intovalueand returns the modified array.- Parameters:
value- The value to fill this array with.start- The starting fill point of the array.
-
fill
Changes all array elements fromstarttoendintovalueand returns the modified array.
- Parameters:
value- The value to fill this array with.start- The starting fill index point of the array. If this value is negative, it is treated aslength + startend- The ending fill index point of the array. This element at this index is included and is also changed tovalue. If this value is negative, it is treated aslength + end
-
filter
Returns a newArrayconsisting of all the elements of thisArraythat satisfy the specified predicate.- Parameters:
predicate- The predicate to apply to the elements of thisArray.
-
find
Returns the value of the first element in thisArraythat satisfies the given predicate, andnullotherwise.- Parameters:
predicate- The predicate to apply in finding an element from thisArray.- See Also:
-
findAsOptional
Returns the value of the first element in thisArraythat satisfies the given predicate as anOptional.- Parameters:
predicate- The predicate to apply in finding an element from thisArray.- See Also:
-
findFirst
-
findIndex
Returns the index of the first element in thisArraythat satisfies the given predicate, and-1otherwise.- Parameters:
predicate- The predicate to apply in finding an element from thisArray.- See Also:
-
findIndexAsOptional
Returns the index of the first element in thisArraythat satisfies the given predicate as anOptional.- Parameters:
predicate- The predicate to apply in finding an element from thisArray.- See Also:
-
findLast
Returns the value of the last element in thisArraythat satisfies the given predicate, andnullotherwise.- Parameters:
predicate- The predicate to apply in finding an element from thisArray.- See Also:
-
findLastAsOptional
Returns the value of the last element in thisArraythat satisfies the given predicate as anOptional.- Parameters:
predicate- The predicate to apply in finding an element from thisArray.- See Also:
-
findLastIndex
Returns the index of the last element in thisArraythat satisfies the given predicate, and-1otherwise.- Parameters:
predicate- The predicate to apply in finding an element from thisArray.- See Also:
-
get
ACopyOnWriteArrayListmethod and an alias forat(int), returns the element located at the specified index. -
includes
Returnstrueif thisArraycontains the specified element. More formally, returnstrueif and only if thisArraycontains at least one elementesuch thatObjects.equals(o, e).- Parameters:
object- The element to search for in thisArray.
-
indexOf
Returns the index of the first occurrence of the specified element in thisArray, or-1if thisArraydoes not contain the element. More formally, returns the lowest indexisuch thatObjects.equals(o, get(i)), or-1if there is no such index. -
iterator
ACopyOnWriteArrayListmethod, returns an iterator over the elements in this list in proper sequence.
The returned iterator provides a snapshot of the state of the list when the iterator was constructed. No synchronization is needed while traversing the iterator. The iterator does NOT support theremovemethod. -
isEmpty
public boolean isEmpty()ACopyOnWriteArrayListmethod, returnstrueif this list contains no elements. -
join
Joins all the elements of thisArrayinto aString, separated by commas. -
join
Joins all the elements of thisArrayinto aString, separated by the specified separator string.- Parameters:
separator- A string used to separate one element of the array from the next in the resulting string.
-
lastIndexOf
Returns the index of the last occurrence of the specified element in thisArray, or-1if thisArraydoes not contain the element. More formally, returns the highest indexisuch thatObjects.equals(o, get(i)), or-1if there is no such index.- Specified by:
lastIndexOfin interfaceList<T>- Parameters:
object- The value to locate in the array.
-
length
public int length()Returns the length (the number of elements) in thisArray. This is a number one higher than the highest index in the array. -
listIterator
- Specified by:
listIteratorin interfaceList<T>
-
listIterator
- Specified by:
listIteratorin interfaceList<T>
-
toArray
-
toArray
public <U> U @NotNull [] toArray(U @NotNull [] a) -
map
Returns a newArrayconsisting of the results of applying the given function to the elements of thisArray.- Type Parameters:
R- The element type of the newArray.- Parameters:
mapper- A function that takes an element and returns a value.
-
peek
AStreammethod, performs the specified action for each element in an array, then returns theArray.- Parameters:
consumer- The action to be performed for each element.- See Also:
-
pop
Removes the element at the end of thisArrayand returns it.
If the array is empty,nullis returned and theArrayis not modified. -
push
Appends new elements to the end of thisArrayand returns the new length of thisArray.- Parameters:
elements- The new elements to add to thisArray.
-
reduce
Performs a reduction on the elements of thisArray, using the providedinitialValueandaccumulatorfunction. This is equivalent to:U result = initialValue; for (T element : this.array) result = accumulator.apply(result, element); return result;- Type Parameters:
U- The type of the initial value, and the valuereducermust return.- Parameters:
accumulator- A function that combines both the value and the current element.initialValue- The initial value for the reducer function.- See Also:
-
reduceRight
A right-sided version ofreduce(java.util.function.BiFunction<U, T, U>, U), performs a reduction on the elements of thisArraystarting from the last element to the first element, using the providedinitialValueandaccumulatorfunction.- Type Parameters:
U- The type of the initial value, and the valuereducermust return.- Parameters:
accumulator- A function that combines both the value and the current element.initialValue- The initial value for the reducer function.- See Also:
-
retainAll
-
removeAll
-
remove
ACopyOnWriteArrayListmethod, removes the first occurrence of the specified element from thisArray, if it is present.
-
remove
ACopyOnWriteArrayListmethod, removes the element at the specified position in this list and shifts any subsequent elements to the left (subtracts one from their indices).
-
reverse
Reverses the order of elements of thisArray. This method mutates the currentArray.- See Also:
-
shift
Removes the first element from an array and returns it.
If the array is empty,nullis returned and the array is not modified. -
slice
Returns a copy of a section of anArrayfrom0toarray.length() - 1. This is returned as a newArray, meaning that any modifications made to the returnedArrayare not reflected in the originalArray.
-
slice
Returns a copy of a section of anArrayfromstarttoarray.length() - 1. This is returned as a newArray, meaning that any modifications made to the returnedArrayare not reflected in the originalArray.
For bothstartandend, a negative index can be used to indicate an offset from the end of the array. For example,-2refers to the second to last element of the array.- Parameters:
start- The beginning index of the specified portion of the array.
-
slice
Returns a copy of a section of anArray. This is returned as a newArray, meaning that any modifications made to the returnedArrayare not made in the originalArray.
For bothstartandend, a negative index can be used to indicate an offset from the end of the array. For example,-2refers to the second to last element of the array.- Parameters:
start- The beginning index of the specified portion of the array.end- The end index of the specified portion of the array.
-
some
Determines whether at least one element of thisArraysatisfy the specified predicate.- Parameters:
predicate- The predicate to apply to the elements of thisArray.- See Also:
-
sortElements
A renamed implementation of Array.prototype.sort(), sorts an array's elements' string value in ascending, ASCII character order. -
sortElements
A renamed implementation of Array.prototype.sort(), sorts thisArrayin place according to the order induced by the specifiedBiFunction. The sort is stable: this method must not reorder equal elements.
The resulting sort will be based on the value ofcompareFn(a, b).- If
compareFn(a, b) < 0, thena > b. - If
compareFn(a, b) = 0, thena = b. - If
compareFn(a, b) > 0, thena < b.
This method mutates the originalArray.- Parameters:
compareFn- The function used to compareArrayelements.
- If
-
stream
- Specified by:
streamin interfaceCollection<T>
-
subList
ACopyOnWriteArrayListmethod, returns a view of the portion of this list betweenfromIndex, inclusive, andtoIndex, exclusive. The returned list is backed by this list, so changes in the returned list are reflected in this list.
The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is modified in any way other than via the returned list. -
toReversed
Reverses the order of elements of thisArray. Unlikereverse(), this method returns a newArraywith the reversed elements.- See Also:
-
toSorted
Sorts thisArrayaccording to the order induced by the specifiedComparator. The sort is stable: this method must not reorder equal elements. UnlikeList.sort(java.util.Comparator<? super E>), this method returns a newArraywith the sorted elements.- Parameters:
compareFn- The function used to compareArrayelements.
-
normalizeIndex
private int normalizeIndex(int index) Normalizes index inputs, resolving negative and positive array indexes.- Parameters:
index- The index to normalize.- Returns:
- The normalized index.
-
toString
-