Class AbstractIntList

All Implemented Interfaces:
IntCollection, IntIterable, IntList, IntStack, Stack<Integer>, Comparable<List<? extends Integer>>, Iterable<Integer>, Collection<Integer>, List<Integer>, SequencedCollection<Integer>
Direct Known Subclasses:
AbstractIntList.IntSubList, IntArrayList, IntImmutableList, IntLists.Singleton

public abstract class AbstractIntList extends AbstractIntCollection implements IntList, IntStack
An abstract class providing basic methods for lists implementing a type-specific list interface.

As an additional bonus, this class implements on top of the list operations a type-specific stack.

Most of the methods in this class are optimized with the assumption that the List will have have constant-time random access. If this is not the case, you should probably at least override listIterator(int) and the xAll() methods (such as addAll(int, java.util.Collection<? extends java.lang.Integer>)) with a more appropriate iteration scheme. Note the subList(int, int) method is cognizant of random-access or not, so that need not be reimplemented.

  • Method Details

    • add

      public void add(int index, int k)
      Inserts the specified element at the specified position in this list (optional operation).
      Specified by:
      add in interface IntList
      See Also:
      Implementation Specification:
      This implementation always throws an UnsupportedOperationException.
    • add

      public boolean add(int k)
      Ensures that this collection contains the specified element (optional operation).
      Specified by:
      add in interface IntCollection
      Specified by:
      add in interface IntList
      Overrides:
      add in class AbstractIntCollection
      See Also:
      Implementation Specification:
      This implementation delegates to the type-specific version of List.add(int, Object).
    • removeInt

      public int removeInt(int i)
      Removes the element at the specified position in this list (optional operation).
      Specified by:
      removeInt in interface IntList
      See Also:
      Implementation Specification:
      This implementation always throws an UnsupportedOperationException.
    • set

      public int set(int index, int k)
      Replaces the element at the specified position in this list with the specified element (optional operation).
      Specified by:
      set in interface IntList
      See Also:
      Implementation Specification:
      This implementation always throws an UnsupportedOperationException.
    • addAll

      public boolean addAll(int index, Collection<? extends Integer> c)
      Adds all of the elements in the specified collection to this list (optional operation).
      Specified by:
      addAll in interface List<Integer>
    • addAll

      public boolean addAll(Collection<? extends Integer> c)
      Specified by:
      addAll in interface Collection<Integer>
      Specified by:
      addAll in interface List<Integer>
      Overrides:
      addAll in class AbstractIntCollection
      Implementation Specification:
      This implementation delegates to the type-specific version of List.addAll(int, Collection).
    • iterator

      public IntListIterator iterator()
      Returns a type-specific iterator on the elements of this collection.
      Specified by:
      iterator in interface Collection<Integer>
      Specified by:
      iterator in interface IntCollection
      Specified by:
      iterator in interface IntIterable
      Specified by:
      iterator in interface IntList
      Specified by:
      iterator in interface Iterable<Integer>
      Specified by:
      iterator in interface List<Integer>
      Specified by:
      iterator in class AbstractIntCollection
      Returns:
      a type-specific iterator on the elements of this collection.
      See Also:
      Implementation Specification:
      This implementation delegates to listIterator().
    • listIterator

      public IntListIterator listIterator()
      Returns a type-specific list iterator on the list.
      Specified by:
      listIterator in interface IntList
      Specified by:
      listIterator in interface List<Integer>
      See Also:
      Implementation Specification:
      This implementation delegates to listIterator(0).
    • listIterator

      public IntListIterator listIterator(int index)
      Returns a type-specific list iterator on the list starting at a given index.
      Specified by:
      listIterator in interface IntList
      Specified by:
      listIterator in interface List<Integer>
      See Also:
      Implementation Specification:
      This implementation is based on the random-access methods.
    • contains

      public boolean contains(int k)
      Returns true if this list contains the specified element.
      Specified by:
      contains in interface IntCollection
      Overrides:
      contains in class AbstractIntCollection
      See Also:
      Implementation Specification:
      This implementation delegates to indexOf().
    • indexOf

      public int indexOf(int k)
      Description copied from interface: IntList
      Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
      Specified by:
      indexOf in interface IntList
      See Also:
    • lastIndexOf

      public int lastIndexOf(int k)
      Description copied from interface: IntList
      Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
      Specified by:
      lastIndexOf in interface IntList
      See Also:
    • size

      public void size(int size)
      Description copied from interface: IntList
      Sets the size of this list.

      If the specified size is smaller than the current size, the last elements are discarded. Otherwise, they are filled with 0/null/false.

      Specified by:
      size in interface IntList
      Parameters:
      size - the new size.
    • subList

      public IntList subList(int from, int to)
      Description copied from interface: IntList
      Returns a type-specific view of the portion of this list from the index from, inclusive, to the index to, exclusive.
      Specified by:
      subList in interface IntList
      Specified by:
      subList in interface List<Integer>
      See Also:
    • forEach

      public void forEach(IntConsumer action)
      Performs the given action for each element of this type-specific Iterable until all elements have been processed or the action throws an exception.
      Specified by:
      forEach in interface IntIterable
      Parameters:
      action - the action to be performed for each element.
      See Also:
      Implementation Specification:
      If this list is RandomAccess, will iterate using a for loop and the type-specific List.get(int) method. Otherwise it will fallback to using the iterator based loop implementation from the superinterface.
    • removeElements

      public void removeElements(int from, int to)
      Removes (hopefully quickly) elements of this type-specific list.

      This is a trivial iterator-based based implementation. It is expected that implementations will override this method with a more optimized version.

      Specified by:
      removeElements in interface IntList
      Parameters:
      from - the start index (inclusive).
      to - the end index (exclusive).
    • addElements

      public void addElements(int index, int[] a, int offset, int length)
      Add (hopefully quickly) elements to this type-specific list.

      This is a trivial iterator-based implementation. It is expected that implementations will override this method with a more optimized version.

      Specified by:
      addElements in interface IntList
      Parameters:
      index - the index at which to add elements.
      a - the array containing the elements.
      offset - the offset of the first element to add.
      length - the number of elements to add.
    • addElements

      public void addElements(int index, int[] a)
      Add (hopefully quickly) elements to this type-specific list.
      Specified by:
      addElements in interface IntList
      Parameters:
      index - the index at which to add elements.
      a - the array containing the elements.
      Implementation Specification:
      This implementation delegates to the analogous method for array fragments.
    • getElements

      public void getElements(int from, int[] a, int offset, int length)
      Copies (hopefully quickly) elements of this type-specific list into the given array.

      This is a trivial iterator-based implementation. It is expected that implementations will override this method with a more optimized version.

      Specified by:
      getElements in interface IntList
      Parameters:
      from - the start index (inclusive).
      a - the destination array.
      offset - the offset into the destination array where to store the first element copied.
      length - the number of elements to be copied.
    • setElements

      public void setElements(int index, int[] a, int offset, int length)
      Description copied from interface: IntList
      Set (hopefully quickly) elements to match the array given. Sets each in this list to the corresponding elements in the array, as if by
       ListIterator iter = listIterator(index);
       int i = 0;
       while (i < length) {
              iter.next();
              iter.set(a[offset + i++]);
       }
       
      However, the exact implementation may be more efficient, taking into account whether random access is faster or not, or at the discretion of subclasses, abuse internals.
      Specified by:
      setElements in interface IntList
      Parameters:
      index - the index at which to start setting elements.
      a - the array containing the elements
      offset - the offset of the first element to add.
      length - the number of elements to add.
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<Integer>
      Specified by:
      clear in interface List<Integer>
      Overrides:
      clear in class AbstractCollection<Integer>
      Implementation Specification:
      This implementation delegates to removeElements(int, int).
    • hashCode

      public int hashCode()
      Returns the hash code for this list, which is identical to List.hashCode().
      Specified by:
      hashCode in interface Collection<Integer>
      Specified by:
      hashCode in interface List<Integer>
      Overrides:
      hashCode in class Object
      Returns:
      the hash code for this list.
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Collection<Integer>
      Specified by:
      equals in interface List<Integer>
      Overrides:
      equals in class Object
    • compareTo

      public int compareTo(List<? extends Integer> l)
      Compares this list to another object. If the argument is a List, this method performs a lexicographical comparison; otherwise, it throws a ClassCastException.
      Specified by:
      compareTo in interface Comparable<List<? extends Integer>>
      Parameters:
      l - a list.
      Returns:
      if the argument is a List, a negative integer, zero, or a positive integer as this list is lexicographically less than, equal to, or greater than the argument.
      Throws:
      ClassCastException - if the argument is not a list.
    • push

      public void push(int o)
      Description copied from interface: IntStack
      Pushes the given object on the stack.
      Specified by:
      push in interface IntStack
      Parameters:
      o - the object to push on the stack.
      See Also:
    • popInt

      public int popInt()
      Description copied from interface: IntStack
      Pops the top off the stack.
      Specified by:
      popInt in interface IntStack
      Returns:
      the top of the stack.
      See Also:
    • topInt

      public int topInt()
      Description copied from interface: IntStack
      Peeks at the top of the stack (optional operation).
      Specified by:
      topInt in interface IntStack
      Returns:
      the top of the stack.
      See Also:
    • peekInt

      public int peekInt(int i)
      Description copied from interface: IntStack
      Peeks at an element on the stack (optional operation).
      Specified by:
      peekInt in interface IntStack
      Parameters:
      i - an index from the stop of the stack (0 represents the top).
      Returns:
      the i-th element on the stack.
      See Also:
    • rem

      public boolean rem(int k)
      Removes a single instance of the specified element from this collection, if it is present (optional operation).
      Specified by:
      rem in interface IntCollection
      Overrides:
      rem in class AbstractIntCollection
      See Also:
      Implementation Specification:
      This implementation delegates to indexOf().
    • toIntArray

      public int[] toIntArray()
      Description copied from interface: IntCollection
      Returns a primitive type array containing the items of this collection.
      Specified by:
      toIntArray in interface IntCollection
      Overrides:
      toIntArray in class AbstractIntCollection
      Returns:
      a primitive type array containing the items of this collection.
      See Also:
    • toArray

      public int[] toArray(int[] a)
      Description copied from interface: IntCollection
      Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

      Note that, contrarily to Collection.toArray(Object[]), this methods just writes all elements of this collection: no special value will be added after the last one.

      Specified by:
      toArray in interface IntCollection
      Overrides:
      toArray in class AbstractIntCollection
      Parameters:
      a - if this array is big enough, it will be used to store this collection.
      Returns:
      a primitive type array containing the items of this collection.
      See Also:
    • addAll

      public boolean addAll(int index, IntCollection c)
      Description copied from interface: IntList
      Inserts all of the elements in the specified type-specific collection into this type-specific list at the specified position (optional operation).
      Specified by:
      addAll in interface IntList
      See Also:
    • addAll

      public boolean addAll(IntCollection c)
      Adds all elements of the given type-specific collection to this collection.
      Specified by:
      addAll in interface IntCollection
      Overrides:
      addAll in class AbstractIntCollection
      Parameters:
      c - a type-specific collection.
      Returns:
      true if this collection changed as a result of the call.
      See Also:
      Implementation Specification:
      This implementation delegates to the type-specific version of List.addAll(int, Collection).
    • replaceAll

      public final void replaceAll(IntUnaryOperator operator)
      Replaces each element of this list with the result of applying the operator to that element.

      WARNING: Overriding this method is almost always a mistake, as this overload only exists to disambiguate. Instead, override the replaceAll() overload that uses the JDK's primitive unary operator type (e.g. IntUnaryOperator).

      If Java supported final default methods, this would be one, but sadly it does not.

      If you checked and are overriding the version with java.util.function.XUnaryOperator, and still see this warning, then your IDE is incorrectly conflating this method with the proper method to override, and you can safely ignore this message.

      Specified by:
      replaceAll in interface IntList
      Parameters:
      operator - the operator to apply to each element
      See Also:
      Implementation Specification:
      This method just delegates to the interface default method, as the default method, but it is final, so it cannot be overridden.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractIntCollection