Class SortedList<T>

java.lang.Object
java.util.AbstractCollection<T>
java.util.AbstractList<T>
java.util.ArrayList<T>
com.amalgamasimulation.utils.container.SortedList<T>
Type Parameters:
T - the type of elements in this list
All Implemented Interfaces:
Serializable, Cloneable, Iterable<T>, Collection<T>, List<T>, RandomAccess, SequencedCollection<T>
Direct Known Subclasses:
DoubleSortedList

public class SortedList<T> extends ArrayList<T>
List of elements that maintains order using the comparator specified at the time of list's creation.
Author:
Andrey Malykhanov
See Also:
  • Constructor Details

    • SortedList

      public SortedList(Comparator<T> comparator)
      Constructs an empty sorted list with an initial capacity of ten. The order of elements is defined by the given comparator.
      Parameters:
      comparator - comparator defining order of the elements
    • SortedList

      public SortedList(int size, Comparator<T> comparator)
      Constructs an empty sorted list with the specified initial capacity. The order of elements is defined by the given comparator.
      Parameters:
      size - the initial capacity of the list
      comparator - comparator defining order of the elements
    • SortedList

      public SortedList(Collection<T> collection, Comparator<T> comparator)
      Constructs a sorted list containing the elements of the specified collection, in the order they are returned by the collection's iterator. The order of elements is defined by the given comparator.
      Parameters:
      collection - the collection whose elements are to be placed into this list
      comparator - comparator defining order of the elements
  • Method Details

    • getInsertionIndex

      public int getInsertionIndex(T element, boolean insertBefore)
      Returns an index at which the given element can be inserted to preserve order of the elements in this list.
      Parameters:
      element - given element
      insertBefore - if true, the least possible index. If false, returns the greatest possible index
      Returns:
      index at which the given element can be inserted to preserve order of the elements in this list
    • floor

      public T floor(T element)
      Returns the greatest element in this list less than or equal to the given element, or null if there is no such element.
      Parameters:
      element - given element
      Returns:
      the greatest element less than or equal to the given element, or null if there is no such element
    • ceiling

      public T ceiling(T element)
      Returns the least element in this set greater than or equal to the given element, or null if there is no such element.
      Parameters:
      element - given element
      Returns:
      the least element greater than or equal to the given element, or null if there is no such element
    • headList

      public List<T> headList(T element, boolean inclusive)
      Returns a view of the portion of this list whose elements are strictly less than (or less than or equal to) the given element.
      Parameters:
      element - given element
      inclusive - if true, the list will contain elements that are equal to the given element
      Returns:
      view of the portion of this list whose elements are strictly less than (or less than or equal to) the given element
    • tailList

      public List<T> tailList(T element, boolean inclusive)
      Returns a view of the portion of this list whose elements are greater than (or greater than or equal to) the given element.
      Parameters:
      element - given element
      inclusive - if true, the list will contain elements that are equal to the given element
      Returns:
      view of the portion of this list whose elements are greater than (or greater than or equal to) the given element
    • subList

      public List<T> subList(T beginElement, boolean inclusiveBegin, T endElement, boolean inclusiveEnd)
      Returns a view of the portion of this list whose elements range from beginElement (inclusive or exclusive), to endElement (inclusive or exclusive).
      Parameters:
      beginElement - begin element
      inclusiveBegin - if true, the list will contain elements that are equal to the beginElement
      endElement - end element
      inclusiveEnd - if true, the list will contain elements that are equal to the endElement
      Returns:
      view of the portion of this list whose elements range from beginElement (inclusive or exclusive), to endElement (inclusive or exclusive)
    • first

      public T first()
      Returns the first element of this list, or null if there is no such element (i.e. list is empty).
      Returns:
      first element of this list, or null if there is no such element
    • last

      public T last()
      Returns the last element of this list, or null if there is no such element (i.e. list is empty).
      Returns:
      last element of this list, or null if there is no such element
    • listIteratorAtLastItem

      public ListIterator<T> listIteratorAtLastItem()
      Returns a ListIterator instance pointing to the last item of this list.
      Returns:
      ListIterator instance pointing to the last item of this list
    • add

      public boolean add(T element)
      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface List<T>
      Overrides:
      add in class ArrayList<T>
    • add

      public boolean add(T element, boolean insertBefore)
      Adds the specified element to this list. Allows to specify where this element must be inserted in the case of several other element existing in this list that are equal to the one being inserted.
      Parameters:
      element - the element being inserted
      insertBefore - if true the element will be inserted before all other elements that are equal to the element being inserted. Otherwise, the element will be inserted after all such elements
      Returns:
      true (as specified by Collection.add(E))
    • addAll

      public boolean addAll(Collection<? extends T> collection)
      Specified by:
      addAll in interface Collection<T>
      Specified by:
      addAll in interface List<T>
      Overrides:
      addAll in class ArrayList<T>
    • setSortedElements

      protected void setSortedElements(Collection<? extends T> collection)
    • set

      public T set(int index, T element)
      Specified by:
      set in interface List<T>
      Overrides:
      set in class ArrayList<T>