Class UniqueArray<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>
com.portingdeadmods.portingdeadlibs.utils.UniqueArray<E>
Type Parameters:
E - the type of elements in this collection
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess, SequencedCollection<E>, Set<E>

public class UniqueArray<E> extends ArrayList<E> implements Set<E>
A collection that maintains unique elements (like a Set) while providing indexed access (like an ArrayList).
See Also:
  • Constructor Details

    • UniqueArray

      public UniqueArray()
      Creates an empty UniqueArray.
    • UniqueArray

      public UniqueArray(Collection<? extends E> c)
      Creates a UniqueArray containing the elements of the specified collection. Duplicate elements are only added once.
      Parameters:
      c - the collection whose elements are to be added to this UniqueArray
    • UniqueArray

      public UniqueArray(int initialCapacity)
      Creates a UniqueArray with the specified initial capacity.
      Parameters:
      initialCapacity - the initial capacity of the UniqueArray
      Throws:
      IllegalArgumentException - if the specified initial capacity is negative
  • Method Details

    • add

      public boolean add(E e)
      Appends the specified element to the end of this UniqueArray if it is not already present.
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface List<E>
      Specified by:
      add in interface Set<E>
      Overrides:
      add in class ArrayList<E>
      Parameters:
      e - element to be appended to this UniqueArray
      Returns:
      true if the element was added, false if it was already present
    • add

      public void add(int index, E element)
      Inserts the specified element at the specified position in this UniqueArray if it is not already present.
      Specified by:
      add in interface List<E>
      Overrides:
      add in class ArrayList<E>
      Parameters:
      index - index at which the specified element is to be inserted
      element - element to be inserted
      Throws:
      IndexOutOfBoundsException - if the index is out of range
    • addAll

      public boolean addAll(Collection<? extends E> c)
      Adds all of the elements in the specified collection to this UniqueArray if they're not already present.
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface List<E>
      Specified by:
      addAll in interface Set<E>
      Overrides:
      addAll in class ArrayList<E>
      Parameters:
      c - collection containing elements to be added to this UniqueArray
      Returns:
      true if this UniqueArray changed as a result of the call
    • addAll

      @SafeVarargs public final boolean addAll(E... c)
      Adds all of the elements in the specified varargs to this UniqueArray if they're not already present.
      Parameters:
      c - elements to be added to this UniqueArray
      Returns:
      true if this UniqueArray changed as a result of the call
    • addAll

      public boolean addAll(int index, Collection<? extends E> c)
      Inserts all of the elements in the specified collection into this UniqueArray at the specified position, if they're not already present.
      Specified by:
      addAll in interface List<E>
      Overrides:
      addAll in class ArrayList<E>
      Parameters:
      index - index at which to insert the first element
      c - collection containing elements to be added to this UniqueArray
      Returns:
      true if this UniqueArray changed as a result of the call
      Throws:
      IndexOutOfBoundsException - if the index is out of range
    • set

      public E set(int index, E element)
      Replaces the element at the specified position in this UniqueArray with the specified element if it's not already present elsewhere in the array.
      Specified by:
      set in interface List<E>
      Overrides:
      set in class ArrayList<E>
      Parameters:
      index - index of the element to replace
      element - element to be stored at the specified position
      Returns:
      the element previously at the specified position
      Throws:
      IndexOutOfBoundsException - if the index is out of range
    • toArray

      public Object[] toArray()
      Returns an array containing all of the elements in this UniqueArray in proper sequence (from first to last element).
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface List<E>
      Specified by:
      toArray in interface Set<E>
      Overrides:
      toArray in class ArrayList<E>
      Returns:
      an array containing all of the elements in this UniqueArray
    • toArray

      public <T> T[] toArray(T[] a)
      Returns an array containing all of the elements in this UniqueArray in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface List<E>
      Specified by:
      toArray in interface Set<E>
      Overrides:
      toArray in class ArrayList<E>
      Parameters:
      a - the array into which the elements of this UniqueArray are to be stored
      Returns:
      an array containing the elements of this UniqueArray
      Throws:
      ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this UniqueArray
      NullPointerException - if the specified array is null
    • toList

      public List<E> toList()
    • toSet

      public Set<E> toSet()
    • CODEC

      public static <T> com.mojang.serialization.Codec<UniqueArray<T>> CODEC(com.mojang.serialization.Codec<T> elementCodec)
      Returns a Codec for serializing and deserializing UniqueArray instances
      Type Parameters:
      T - The type of elements in the UniqueArray
      Parameters:
      elementCodec - The Codec for the elements of the array
    • STREAM_CODEC

      public static <B extends io.netty.buffer.ByteBuf, T> net.minecraft.network.codec.StreamCodec<B,UniqueArray<T>> STREAM_CODEC(net.minecraft.network.codec.StreamCodec<B,T> elementStreamCodec)
      Returns a StreamCodec for serializing and deserializing UniqueArray instances using a ByteBuf.
      Type Parameters:
      B - The type of ByteBuf used for serialization
      T - The type of elements inthe UniqueArray
      Parameters:
      elementStreamCodec - The StreamCodec for the elements of the array