Class SortedList<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
net.luis.utils.collection.SortedList<E>
Type Parameters:
E - The type of the elements
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>, SequencedCollection<E>

public class SortedList<E> extends AbstractList<E>
A list which is after every modification sorted
using the set comparator or if it is null the natural order.
  • Field Details

    • internalList

      private final List<E> internalList
      The internal list.
    • comparator

      private Comparator<E> comparator
      The comparator used to sort the list.
  • Constructor Details

    • SortedList

      public SortedList()
      Constructs a new empty sorted list.
      The comparator is set to null.
      Internally, a ArrayList is used.
    • SortedList

      public SortedList(@Nullable @Nullable Comparator<E> comparator)
      Constructs a new empty sorted list with the given comparator.
      Internally, a ArrayList is used.
      Parameters:
      comparator - The comparator used to sort the list
    • SortedList

      @SafeVarargs public SortedList(E @NotNull ... elements)
      Constructs a new sorted list with the given elements.
      The comparator is set to null.
      The elements are wrapped in an internal ArrayList.
      Parameters:
      elements - The elements to add
    • SortedList

      @SafeVarargs public SortedList(@Nullable @Nullable Comparator<E> comparator, E @NotNull ... elements)
      Constructs a new sorted list with the given elements and the comparator.
      The elements are wrapped in an internal ArrayList.
      Parameters:
      comparator - The comparator used to sort the list
      elements - The elements to add
    • SortedList

      public SortedList(@NotNull @NotNull List<E> elements)
      Constructs a new sorted list with from the given list.
      The comparator is set to null.
      The list is not copied, it is used directly.
      Parameters:
      elements - The list to use
      Throws:
      NullPointerException - If the list is null
    • SortedList

      public SortedList(@NotNull @NotNull List<E> elements, @Nullable @Nullable Comparator<E> comparator)
      Constructs a new sorted list from the given list and the comparator.
      The list is not copied, it is used directly.
      Parameters:
      elements - The list to use
      comparator - The comparator used to sort the list
      Throws:
      NullPointerException - If the list is null
  • Method Details

    • createList

      @SafeVarargs @NotNull private static <E> @NotNull List<E> createList(E @NotNull ... elements)
      Creates a new list from the given elements.
      The elements are wrapped in an internal ArrayList.
      Type Parameters:
      E - The type of the elements
      Parameters:
      elements - The elements to add
      Returns:
      The new list
      Throws:
      NullPointerException - If the elements are null
    • setComparator

      public void setComparator(@Nullable @Nullable Comparator<E> comparator)
      Sets the comparator used to sort the list.
      Parameters:
      comparator - The comparator to set
    • set

      public E set(int index, @NotNull E element)

      The list is sorted after the element is added.
      Specified by:
      set in interface List<E>
      Overrides:
      set in class AbstractList<E>
    • add

      public void add(int index, @NotNull E element)

      The list is sorted after the element is added.
      Specified by:
      add in interface List<E>
      Overrides:
      add in class AbstractList<E>
    • remove

      public boolean remove(@Nullable @Nullable Object object)

      The list is sorted after the element is removed.
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class AbstractCollection<E>
    • get

      @NotNull public E get(int index)
      Specified by:
      get in interface List<E>
      Specified by:
      get in class AbstractList<E>
    • size

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface List<E>
      Specified by:
      size in class AbstractCollection<E>
    • sort

      public void sort(@Always @Nullable @Nullable Comparator<? super E> comparator)
      The list is sorted with the comparator of this list.
      Parameters:
      comparator - The ignored comparator