Class FastArray<E>

java.lang.Object
civitas.celestis.util.array.FastArray<E>
Type Parameters:
E - The type of element to contain
All Implemented Interfaces:
BaseArray<E>, SafeArray<E>, Serializable, Iterable<E>
Direct Known Subclasses:
SyncArray

public class FastArray<E> extends Object implements SafeArray<E>
A basic type-safe array with no built-in synchronization or thread-safety measures. Fast arrays are designed for speed and efficiency.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final E[]
    The internal array of values.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    FastArray(int length)
    Creates a new fast array.
     
    FastArray(SafeArray<? extends E> a)
    Creates a new fast array.
    protected
    FastArray(E[] array)
    Creates a new fast array.
  • Method Summary

    Modifier and Type
    Method
    Description
    E[]
    Returns a primitive array containing the elements of this array in their proper order.
    boolean
    Returns whether this array contains the provided object obj.
    boolean
    Returns whether this array contains multiple objects.
    boolean
    Checks for equality between this array and the provided object obj.
    void
    fill(E v)
    Fills this array with the provided value v.
    void
    Fills every empty slot of this array with the provided value v.
    void
    fillRange(int s, int e, E v)
    Fills every slot of this array between the range of [s, e) with the provided value v.
    void
    forEach(BiConsumer<? super Integer,? super E> a)
    Executes the provided action a once for each element of this array.
    void
    forEach(Consumer<? super E> a)
    Executes the provided action a once for each element of this array.
    get(int i)
    Returns the ith element of this array.
    getOrDefault(int i, E e)
    Returns the ith element of this array, but returns the provided fallback value e instead of the value at the specified index is null.
    int
    Returns the hash code of this instance.
    Returns an iterator over every element of this array.
    int
    Returns the length of this array.
    Returns an unmodifiable list containing the elements of this array in their proper order.
    <F> SafeArray<F>
    map(Function<? super E,? extends F> f)
    Applies the provided mapper function f to each element of this array, then returns a new array containing the return values of the function f.
    Applies the provided mapper function f to each element of this array, then returns a new double array containing the return values of the function f.
    Applies the provided mapper function f to each element of this array, then returns a new float array containing the return values of the function f.
    mapToInt(ToIntFunction<? super E> f)
    Applies the provided mapper function f to each element of this array, then returns a new integer array containing the return values of the function f.
    Applies the provided mapper function f to each element of this array, then returns a new long array containing the return values of the function f.
    <F, G> SafeArray<G>
    merge(SafeArray<F> a, BiFunction<? super E,? super F,? extends G> f)
    Between this array and the provided array a, this applies the merger function f to each corresponding pair of elements, then returns a new array containing the return values of the merger function f.
    void
    replaceAll(E oldValue, E newValue)
    Replaces all instances of the old value to the new value.
    void
    replaceFirst(E oldValue, E newValue)
    Replaces only the first instance of the old value to the new value.
    void
    replaceLast(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.
    void
    set(int i, E e)
    Sets the ith element of this array.
    void
    setRange(int s, int e, SafeArray<? extends E> a)
    Sets a sub-array of this array to the values of the provided sub-array a.
    void
    Shuffles this array, randomizing its elements' order.
    void
    Sorts this array by its natural ascending order.
    void
    sort(Comparator<? super E> c)
    Sorts this array using the provided comparator function c.
    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).
    Serializes this array into a string.
    Returns a tuple containing the elements of this array in their proper order.
    void
    update(int i, UnaryOperator<E> f)
    Updates the ith element of this array with the provided update function f.
    void
    update(BiFunction<? super Integer,? super E,E> f)
    Applies the provided update function f to each element of this array, then assigns the return value of the function to the corresponding index of this array.
    void
    Applies the provided update function f to 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, wait

    Methods inherited from interface java.lang.Iterable

    spliterator

    Methods inherited from interface civitas.celestis.util.array.SafeArray

    append, filter, prepend
  • Field Details

    • values

      @Nonnull protected final E[] values
      The internal array of values.
  • Constructor Details

    • FastArray

      public FastArray(int length)
      Creates a new fast array.
      Parameters:
      length - The length to initialize this array to
    • FastArray

      public FastArray(@Nonnull SafeArray<? extends E> a)
      Creates a new fast array.
      Parameters:
      a - The array of which to copy elements from
    • FastArray

      protected FastArray(@Nonnull E[] array)
      Creates a new fast array. This is a direct assignment constructor, and thus is hidden to ensure safe usage.
      Parameters:
      array - The array of which to directly assign as the internal array
  • Method Details

    • length

      public int length()
      Returns the length of this array.
      Specified by:
      length in interface BaseArray<E>
      Specified by:
      length in interface SafeArray<E>
      Returns:
      The number of elements this array contains
    • contains

      public boolean contains(@Nullable Object obj)
      Returns whether this array contains the provided object obj.
      Specified by:
      contains in interface SafeArray<E>
      Parameters:
      obj - The object of which to check for containment
      Returns:
      true if at least one element of this array is equal to the provided object obj
    • containsAll

      public boolean containsAll(@Nonnull Iterable<?> i)
      Returns whether this array contains multiple objects.
      Specified by:
      containsAll in interface SafeArray<E>
      Parameters:
      i - The iterable object of which to check for containment
      Returns:
      true if this array contains every element of the iterable object
    • get

      public E get(int i) throws IndexOutOfBoundsException
      Returns the ith element of this array.
      Specified by:
      get in interface SafeArray<E>
      Parameters:
      i - The index of the element to get
      Returns:
      The ith element of this array
      Throws:
      IndexOutOfBoundsException - When the index i is out of bounds
    • getOrDefault

      public E getOrDefault(int i, E e) throws IndexOutOfBoundsException
      Returns the ith element of this array, but returns the provided fallback value e instead of the value at the specified index is null.
      Specified by:
      getOrDefault in interface SafeArray<E>
      Parameters:
      i - The index of the element to get
      e - The fallback value to default to when the value is null
      Returns:
      The ith element of this array if present, the fallback value e otherwise
      Throws:
      IndexOutOfBoundsException - When the index i is out of bounds
    • set

      public void set(int i, E e) throws IndexOutOfBoundsException
      Sets the ith element of this array.
      Specified by:
      set in interface SafeArray<E>
      Parameters:
      i - The index of the element to set
      e - The element of which to set to
      Throws:
      IndexOutOfBoundsException - When the index i is out of bounds
    • update

      public void update(int i, @Nonnull UnaryOperator<E> f) throws IndexOutOfBoundsException
      Updates the ith element of this array with the provided update function f.
      Specified by:
      update in interface SafeArray<E>
      Parameters:
      i - The index of the element to update
      f - The update function of which to apply to the element
      Throws:
      IndexOutOfBoundsException - When the index i is out of bounds
    • fill

      public void fill(E v)
      Fills this array with the provided value v. Every element will be reassigned.
      Specified by:
      fill in interface SafeArray<E>
      Parameters:
      v - The value to fill this array with
    • fillEmpty

      public void fillEmpty(E v)
      Fills every empty slot of this array with the provided value v. All occurrences of null will be replaced with the provided value.
      Specified by:
      fillEmpty in interface SafeArray<E>
      Parameters:
      v - The value to fill empty slots of this array with
    • fillRange

      public void fillRange(int s, int e, E v)
      Fills every slot of this array between the range of [s, e) with the provided value v.
      Specified by:
      fillRange in interface SafeArray<E>
      Parameters:
      s - The starting index at which to start assigning values from
      e - The ending index at which to stop assigning values at
      v - The value of which to assign to every slot within the specified range
    • update

      public void update(@Nonnull UnaryOperator<E> f)
      Applies the provided update function f to each element of this array, then assigns the return value of the function to the corresponding index of this array.
      Specified by:
      update in interface SafeArray<E>
      Parameters:
      f - The function of which to apply to each element of this array
    • update

      public void update(@Nonnull BiFunction<? super Integer,? super E,E> f)
      Applies the provided update function f to 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.
      Specified by:
      update in interface SafeArray<E>
      Parameters:
      f - The function of which to apply to each element of this array
    • replaceAll

      public void replaceAll(E oldValue, E newValue)
      Replaces all instances of the old value to the new value.
      Specified by:
      replaceAll in interface SafeArray<E>
      Parameters:
      oldValue - The old value to replace
      newValue - The new value to replace to
    • replaceFirst

      public void replaceFirst(E oldValue, E newValue)
      Replaces only the first instance of the old value to the new value. (starting from index 0, and incrementing upwards)
      Specified by:
      replaceFirst in interface SafeArray<E>
      Parameters:
      oldValue - The old value to replace
      newValue - The new value to replace to
    • replaceLast

      public void replaceLast(E oldValue, E newValue)
      Replaces only the last instance of the old value to the new value. (starting from the last index, and decrementing downwards)
      Specified by:
      replaceLast in interface SafeArray<E>
      Parameters:
      oldValue - The old value to replace
      newValue - The new value to replace to
    • subArray

      @Nonnull public SafeArray<E> subArray(int s, int e) throws IndexOutOfBoundsException
      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, calling a.subArray(1, 3).fill(null); will have the same effect as setting the values of indices 1, 2 of the original array to null.
      Specified by:
      subArray in interface SafeArray<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-array a.
      Specified by:
      setRange in interface SafeArray<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

      @Nonnull public SafeArray<E> resize(int size)
      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 as null.
      Specified by:
      resize in interface SafeArray<E>
      Parameters:
      size - The size to resize this array to
      Returns:
      The resized array
    • shuffle

      public void shuffle()
      Shuffles this array, randomizing its elements' order.
      Specified by:
      shuffle in interface BaseArray<E>
      Specified by:
      shuffle in interface SafeArray<E>
    • sort

      public void sort() throws UnsupportedOperationException
      Sorts this array by its natural ascending order. This operation will only succeed if the element E is an instance of Comparable.
      Specified by:
      sort in interface BaseArray<E>
      Specified by:
      sort in interface SafeArray<E>
      Throws:
      UnsupportedOperationException - When at least one element cannot be cast to Comparable
    • sort

      public void sort(@Nonnull Comparator<? super E> c)
      Sorts this array using the provided comparator function c.
      Specified by:
      sort in interface BaseArray<E>
      Specified by:
      sort in interface SafeArray<E>
      Parameters:
      c - The comparator function of which to sort this array with
    • map

      @Nonnull public <F> SafeArray<F> map(@Nonnull Function<? super E,? extends F> f)
      Applies the provided mapper function f to each element of this array, then returns a new array containing the return values of the function f.
      Specified by:
      map in interface SafeArray<E>
      Type Parameters:
      F - The type of element to map this array to
      Parameters:
      f - The function of which to apply to each element of this array
      Returns:
      The resulting array
    • mapToDouble

      @Nonnull public DoubleArray mapToDouble(@Nonnull ToDoubleFunction<? super E> f)
      Applies the provided mapper function f to each element of this array, then returns a new double array containing the return values of the function f.
      Specified by:
      mapToDouble in interface SafeArray<E>
      Parameters:
      f - The function of which to apply to each element of this array
      Returns:
      The resulting array
      See Also:
    • mapToFloat

      @Nonnull public FloatArray mapToFloat(@Nonnull ToFloatFunction<? super E> f)
      Applies the provided mapper function f to each element of this array, then returns a new float array containing the return values of the function f.
      Specified by:
      mapToFloat in interface SafeArray<E>
      Parameters:
      f - The function of which to apply to each element of this array
      Returns:
      The resulting array
      See Also:
    • mapToLong

      @Nonnull public LongArray mapToLong(@Nonnull ToLongFunction<? super E> f)
      Applies the provided mapper function f to each element of this array, then returns a new long array containing the return values of the function f.
      Specified by:
      mapToLong in interface SafeArray<E>
      Parameters:
      f - The function of which to apply to each element of this array
      Returns:
      The resulting array
      See Also:
    • mapToInt

      @Nonnull public IntArray mapToInt(@Nonnull ToIntFunction<? super E> f)
      Applies the provided mapper function f to each element of this array, then returns a new integer array containing the return values of the function f.
      Specified by:
      mapToInt in interface SafeArray<E>
      Parameters:
      f - The function of which to apply to each element of this array
      Returns:
      The resulting array
      See Also:
    • merge

      @Nonnull public <F, G> SafeArray<G> merge(@Nonnull SafeArray<F> a, @Nonnull BiFunction<? super E,? super F,? extends G> f) throws IllegalArgumentException
      Between this array and the provided array a, this applies the merger function f to each corresponding pair of elements, then returns a new array containing the return values of the merger function f. In other words, this merges the two arrays into a new array using the provided merger function f.
      Specified by:
      merge in interface SafeArray<E>
      Type Parameters:
      F - The type of element to merge this array with
      G - The type of element to merge the two arrays to
      Parameters:
      a - The array of which to merge this array with
      f - The merger function to handle the merging of the two arrays
      Returns:
      The resulting array
      Throws:
      IllegalArgumentException - When the provided array a's length is different from that of this array's length
    • iterator

      @Nonnull public Iterator<E> iterator()
      Returns an iterator over every element of this array.
      Specified by:
      iterator in interface BaseArray<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface SafeArray<E>
      Returns:
      An iterator over every element of this array
    • forEach

      public void forEach(@Nonnull Consumer<? super E> a)
      Executes the provided action a once for each element of this array. A reference to the element is provided as the first parameter of the action.
      Specified by:
      forEach in interface BaseArray<E>
      Specified by:
      forEach in interface Iterable<E>
      Specified by:
      forEach in interface SafeArray<E>
      Parameters:
      a - The action to be performed for each element
    • forEach

      public void forEach(@Nonnull BiConsumer<? super Integer,? super E> a)
      Executes the provided action a once 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.
      Specified by:
      forEach in interface BaseArray<E>
      Specified by:
      forEach in interface SafeArray<E>
      Parameters:
      a - The action to be performed for each element
    • array

      @Nonnull public E[] array()
      Returns a primitive array containing the elements of this array in their proper order.
      Specified by:
      array in interface SafeArray<E>
      Returns:
      The primitive array representation of this array
    • stream

      @Nonnull public Stream<E> stream()
      Returns a stream whose source is the elements of this array.
      Specified by:
      stream in interface SafeArray<E>
      Returns:
      A stream whose source is the elements of this array
      See Also:
    • list

      @Nonnull public List<E> list()
      Returns an unmodifiable list containing the elements of this array in their proper order.
      Specified by:
      list in interface BaseArray<E>
      Specified by:
      list in interface SafeArray<E>
      Returns:
      The list representation of this array
      See Also:
    • tuple

      @Nonnull public Tuple<E> tuple()
      Returns a tuple containing the elements of this array in their proper order. Unlike SafeArray.list(), this operation cannot fail as tuples inherently support the usage of null as their values.
      Specified by:
      tuple in interface SafeArray<E>
      Returns:
      The tuple representation of this array
      See Also:
    • equals

      public boolean equals(@Nullable Object obj)
      Checks for equality between this array and the provided object obj.
      Specified by:
      equals in interface SafeArray<E>
      Overrides:
      equals in class Object
      Parameters:
      obj - The object to compare to
      Returns:
      true if the object is also a type-safe array, and the number of elements, the order of the elements, and the elements' values are all equal
    • toString

      @Nonnull public String toString()
      Serializes this array into a string.
      Specified by:
      toString in interface SafeArray<E>
      Overrides:
      toString in class Object
      Returns:
      The string representation of this array
    • hashCode

      public int hashCode()
      Returns the hash code of this instance.
      Overrides:
      hashCode in class Object
      Returns:
      The hash code of this instance