Class BreakableList<E>

Type Parameters:
E - The elements type for the List.
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>, SequencedCollection<E>, Breakable, SupportedMethods

public class BreakableList<E> extends BreakableSequencedCollection<E> implements List<E>

A list that can be broken in well-defined ways in order to test collection utilities or testing classes.

Breaks

In addition to the breaks for BreakableSequencedCollection, the breaks found in [?] are also supported.

Builder methods are provided to make declaring a broken list easier, for example:

    BreakableList<Integer> broken = Breakables.buildList(1, 2)
        .withBreak(List.ADD_ALL_AT_INDEX_DOES_NOT_ADD_ANY_ELEMENTS)
        .build();
Since:
1.0.0
Author:
evanbergstrom
See Also:
  • Field Details

    • ADD_ALL_AT_INDEX_ALWAYS_RETURNS_TRUE

      public static final Break ADD_ALL_AT_INDEX_ALWAYS_RETURNS_TRUE
      The addAll method always return a result of true, even if the element is not added.
      See Also:
    • ADD_ALL_AT_INDEX_ALWAYS_RETURNS_FALSE

      public static final Break ADD_ALL_AT_INDEX_ALWAYS_RETURNS_FALSE
      The addAll method always return a result of false, even if the element is added.
      See Also:
    • ADD_ALL_AT_INDEX_ALWAYS_RETURNS_OPPOSITE_VALUE

      public static final Break ADD_ALL_AT_INDEX_ALWAYS_RETURNS_OPPOSITE_VALUE
      The addAll method always returns the opposite of the appropriate result.
      See Also:
    • ADD_ALL_AT_INDEX_ADDS_TO_THE_END

      public static final Break ADD_ALL_AT_INDEX_ADDS_TO_THE_END
      The addAll method adds to the end of the collection.
      See Also:
    • ADD_ALL_AT_INDEX_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX

      public static final Break ADD_ALL_AT_INDEX_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX
      The addAll method method throws the wrong exception when the index is out of bounds.
      See Also:
    • ADD_ALL_AT_INDEX_THROWS_WRONG_EXCEPTION_IF_NOT_SUPPORTED

      public static final Break ADD_ALL_AT_INDEX_THROWS_WRONG_EXCEPTION_IF_NOT_SUPPORTED
      The addAll method method throws the wrong exception if it is not supported.
      See Also:
    • ADD_ALL_AT_INDEX_THROWS_WRONG_EXCEPTION_WHEN_ARGUMENT_IS_NULL

      public static final Break ADD_ALL_AT_INDEX_THROWS_WRONG_EXCEPTION_WHEN_ARGUMENT_IS_NULL
      The addAll method method throws the wrong exception if the argument is null.
      See Also:
    • ADD_ALL_AT_INDEX_DOES_NOT_ADD_ANY_ELEMENTS

      public static final Break ADD_ALL_AT_INDEX_DOES_NOT_ADD_ANY_ELEMENTS
      The addAll method will not add any elements to the collection
      See Also:
    • ADD_AT_INDEX_DOES_NOT_ADD_THE_ELEMENT

      public static final Break ADD_AT_INDEX_DOES_NOT_ADD_THE_ELEMENT
      The add method will not add an element to the collection
      See Also:
    • ADD_AT_INDEX_ADDS_AT_NEXT_POSITION

      public static final Break ADD_AT_INDEX_ADDS_AT_NEXT_POSITION
      The add method adds the element at the next position.
      See Also:
    • ADD_AT_INDEX_ADDS_AT_PREVIOUS_POSITION

      public static final Break ADD_AT_INDEX_ADDS_AT_PREVIOUS_POSITION
      The add method adds the element at the previous position.
      See Also:
    • ADD_AT_INDEX_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX

      public static final Break ADD_AT_INDEX_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX
      The add method throws the wrong exception when the index is out of bounds.
      See Also:
    • GET_ALWAYS_RETURNS_NULL

      public static final Break GET_ALWAYS_RETURNS_NULL
      The get method always returns null
      See Also:
    • GET_ALWAYS_RETURNS_THE_FIRST_ELEMENT

      public static final Break GET_ALWAYS_RETURNS_THE_FIRST_ELEMENT
      The get method always returns the first element.
      See Also:
    • GET_ALWAYS_RETURNS_THE_LAST_ELEMENT

      public static final Break GET_ALWAYS_RETURNS_THE_LAST_ELEMENT
      The get method always returns the last element.
      See Also:
    • GET_RETURNS_THE_NEXT_ELEMENT

      public static final Break GET_RETURNS_THE_NEXT_ELEMENT
      The get method returns the next element (i.e. at index + 1).
      See Also:
    • GET_RETURNS_THE_PREVIOUS_ELEMENT

      public static final Break GET_RETURNS_THE_PREVIOUS_ELEMENT
      The get method returns the previous element (i.e. at index - 1).
      See Also:
    • GET_RETURNS_NULL_ON_BAD_INDEX

      public static final Break GET_RETURNS_NULL_ON_BAD_INDEX
      The get method returns null on a bad index instead of throwing en exception.
      See Also:
    • GET_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX

      public static final Break GET_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX
      The get method throws the wrong exception on a bad index.
      See Also:
    • REMOVE_AT_INDEX_DOES_NOT_REMOVE_THE_ELEMENT

      public static final Break REMOVE_AT_INDEX_DOES_NOT_REMOVE_THE_ELEMENT
      The remove method does not remove the element.
      See Also:
    • REMOVE_AT_INDEX_REMOVES_THE_NEXT_ELEMENT

      public static final Break REMOVE_AT_INDEX_REMOVES_THE_NEXT_ELEMENT
      The remove method removes the next element.
      See Also:
    • REMOVE_AT_INDEX_REMOVES_THE_PREVIOUS_ELEMENT

      public static final Break REMOVE_AT_INDEX_REMOVES_THE_PREVIOUS_ELEMENT
      The remove method removes the previous element
      See Also:
    • REMOVE_AT_INDEX_RETURNS_NULL_ON_BAD_INDEX

      public static final Break REMOVE_AT_INDEX_RETURNS_NULL_ON_BAD_INDEX
      The remove method returns null when the index is out of bounds.
      See Also:
    • REMOVE_AT_INDEX_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX

      public static final Break REMOVE_AT_INDEX_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX
      The remove method throws the wrong exception when the index is out of bounds.
      See Also:
    • REMOVE_AT_INDEX_ALWAYS_RETURNS_NULL

      public static final Break REMOVE_AT_INDEX_ALWAYS_RETURNS_NULL
      The remove method always returns null
      See Also:
    • REPLACE_ALL_DOES_NOT_REPLACE_ELEMENTS

      public static final Break REPLACE_ALL_DOES_NOT_REPLACE_ELEMENTS
      The replaceAll method does not replace any elements
      See Also:
    • REPLACE_ALL_SKIPS_FIRST_ELEMENT

      public static final Break REPLACE_ALL_SKIPS_FIRST_ELEMENT
      The replaceAll method skips the first element.
      See Also:
    • REPLACE_ALL_SKIPS_LAST_ELEMENT

      public static final Break REPLACE_ALL_SKIPS_LAST_ELEMENT
      The replaceAll method skips the last element
      See Also:
    • SET_DOES_NOT_CHANGE_THE_ELEMENT

      public static final Break SET_DOES_NOT_CHANGE_THE_ELEMENT
      The set method does not change the element.
      See Also:
    • SET_ALWAYS_RETURNS_NULL

      public static final Break SET_ALWAYS_RETURNS_NULL
      The set method always returns null
      See Also:
    • SET_CHANGES_THE_NEXT_ELEMENT

      public static final Break SET_CHANGES_THE_NEXT_ELEMENT
      The set method changes the next element (i.e. at index + 1).
      See Also:
    • SET_CHANGES_THE_PREVIOUS_ELEMENT

      public static final Break SET_CHANGES_THE_PREVIOUS_ELEMENT
      The set method changes the previous element (i.e. at index - 1).
      See Also:
    • SET_RETURNS_NULL_ON_BAD_INDEX

      public static final Break SET_RETURNS_NULL_ON_BAD_INDEX
      The set method returns null on a bad index instead of throwing en exception.
      See Also:
    • SET_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX

      public static final Break SET_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX
      The set method throws the wrong exception on a bad index.
      See Also:
    • SORT_REVERSES_THE_ORDER

      public static final Break SORT_REVERSES_THE_ORDER
      The sort method sorts the elements in the reverse order.
      See Also:
    • SORT_THROWS_ON_NULL_ARGUMENT

      public static final Break SORT_THROWS_ON_NULL_ARGUMENT
      The sort method throws a NullPointerException if the argument is null.
      See Also:
    • SORT_DOES_NOT_SORT_THE_ELEMENTS

      public static final Break SORT_DOES_NOT_SORT_THE_ELEMENTS
      The sort method does not sort the elements.
      See Also:
  • Constructor Details

    • BreakableList

      public BreakableList()
      Creates an empty list that has no breaks.
    • BreakableList

      public BreakableList(@NotNull @NotNull BreakableList<E> other)
      Creates a breakable sequenced collection from an existing instance.
      Parameters:
      other - the breakable collection to copy.
    • BreakableList

      public BreakableList(@NotNull @NotNull List<E> collection)
      Creates a breakable iterable from an iterable.
      Parameters:
      collection - the iterator to use for the elements.
    • BreakableList

      public BreakableList(@NotNull @NotNull List<E> c, @NotNull @NotNull Collection<Break> breaks, int characteristics)
      Creates a BreakableSequencedCollection from en existing collection and specifying the breaks and collection characteristics. Rather than calling this constructor directly, consider using the builder BreakableCollection.Builder.
      Parameters:
      c - the initial elements for the breakable collection.
      breaks - the breaks for the collection.
      characteristics - the characteristics for the collection.
      Throws:
      NullPointerException - if either the c or the breaks parameters are null.
  • Method Details

    • reversed

      public List<E> reversed()
      Description copied from class: BreakableSequencedCollection

      Implements the reversed method from the SequencedCollection interface. This method can be broken using the following collection breaks:

      • REVERSED_DOES_NOT_REVERSE_COLLECTION
      • REVERSED_MODIFIES_THE_COLLECTION

      A collection that has any of these breaks can be constructed using the builder:

          Collection<Integer> collection = Breakables.buildSequencedCollection(1,2,3,4,5)
              .withBreak(CollectionBreaks.REVERSED_DOES_NOT_REVERSE_COLLECTION)
              .build();
      
      Specified by:
      reversed in interface List<E>
      Specified by:
      reversed in interface SequencedCollection<E>
      Overrides:
      reversed in class BreakableSequencedCollection<E>
      Returns:
      a collection with the elements in the reverse order.
      See Also:
    • addAll

      public boolean addAll(int index, @NotNull @NotNull Collection<? extends E> c)
      Break Description
      ADD_ALL_AT_INDEX_DOES_NOT_ADD_ANY_ELEMENTS The addAll method will not add any elements to the collection.
      ADD_ALL_AT_INDEX_ADDS_TO_THE_END The addAll method adds to the end of the collection.
      ADD_ALL_AT_INDEX_ALWAYS_RETURNS_TRUE The addAll method always return a result of true, even if the element is not added.
      ADD_ALL_AT_INDEX_ALWAYS_RETURNS_FALSE The addAll method always returns false
      ADD_ALL_AT_INDEX_ALWAYS_RETURNS_OPPOSITE_VALUE The addAll method always returns true
      ADD_ALL_AT_INDEX_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX The addAll method throws the wrong exception when the index is out of bounds.
      ADD_ALL_AT_INDEX_THROWS_WRONG_EXCEPTION_IF_NOT_SUPPORTED The addAll method throws the wrong exception if it is not supported.
      ADD_ALL_AT_INDEX_THROWS_WRONG_EXCEPTION_WHEN_ARGUMENT_IS_NULL The addAll method throws the wrong exception when the argument is null.

      A collection that has any of these breaks can be constructed using the builder:

          Collection<Integer> collection = Breakables.buildList(1,2,3,4,5)
              .withBreak(ADD_ALL_AT_INDEX_ALWAYS_RETURNS_TRUE)
              .build();
      
      Specified by:
      addAll in interface List<E>
      Parameters:
      index - index at which to insert the first element from the specified collection.
      c - collection containing elements to be added to this list
      Returns:
      'true' if this list changed as a result of the call
      Throws:
      UnsupportedOperationException - if the 'addAll' operation is not supported by this list
      ClassCastException - if the class of an element of the specified collection prevents it from being added to this list
      NullPointerException - if the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null
      IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this list
      IndexOutOfBoundsException - if the index is out of range ('index < 0 || index > size()')
      See Also:
    • replaceAll

      public void replaceAll(UnaryOperator<E> operator)
      Break Description
      REPLACE_ALL_DOES_NOT_REPLACE_ELEMENTS The replaceAll method does not replace any elements
      REPLACE_ALL_SKIPS_FIRST_ELEMENT The replaceAll method skips the first element
      REPLACE_ALL_SKIPS_LAST_ELEMENT The replaceAll method skips the last element

      A collection that has any of these breaks can be constructed using the builder:

          Collection<Integer> collection = Breakables.buildList(1,2,3,4,5)
              .withBreak(REPLACE_ALL_DOES_NOT_REPLACE_ELEMENTS)
              .build();
      
      Specified by:
      replaceAll in interface List<E>
      Throws:
      UnsupportedOperationException - if the replaceAll operation is not supported by this list
      NullPointerException - if the specified operator is null or if the operator result is a null value and this list does not permit null elements.
      See Also:
    • sort

      public void sort(Comparator<? super E> c)
      Break Description
      SORT_DOES_NOT_SORT_THE_ELEMENTS The sort method does not sort the elements.
      SORT_REVERSES_THE_ORDER The sort method sorts the elements in the reverse order.
      SORT_THROWS_ON_NULL_ARGUMENT The sort method throws a NullPointerException if the argument is null

      A collection that has any of these breaks can be constructed using the builder:

          Collection<Integer> collection = Breakables.buildList(1,2,3,4,5)
              .withBreak(SORT_DOES_NOT_SORT_THE_ELEMENTS)
              .build();
      
      Specified by:
      sort in interface List<E>
      Parameters:
      c - the Comparatorused to compare list elements. Anullvalue indicates that the elements' [natural ordering][Comparable] should be used. @throws ClassCastException if the list contains elements that are not mutually comparable using the specified comparator @throws UnsupportedOperationException if thesort` operation is not supported by this list.
      See Also:
    • get

      public E get(int index)
      Break Description
      GET_ALWAYS_RETURNS_NULL The get method always returns null
      GET_ALWAYS_RETURNS_THE_FIRST_ELEMENT The get methods always returns the first element.
      GET_ALWAYS_RETURNS_THE_LAST_ELEMENT The get methods always returns the last element.
      GET_RETURNS_THE_NEXT_ELEMENT The get methods returns the next element (i.e. at index + 1).
      GET_RETURNS_THE_PREVIOUS_ELEMENT The get methods returns the previous element (i.e. at index - 1).
      GET_RETURNS_NULL_ON_BAD_INDEX The get methods returns null on a bad index instead of throwing en exception.
      GET_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX The get methods throws the wrong exception on a bad index.

      A collection that has any of these breaks can be constructed using the builder:

          Collection<Integer> collection = Breakables.buildList(1,2,3,4,5)
              .withBreak(GET_ALWAYS_RETURNS_NULL)
              .build();
      
      Specified by:
      get in interface List<E>
      Parameters:
      index - index of the element to return.
      Returns:
      the element at the specified position in this list, or possibly a different element if the list is broken.
      Throws:
      IndexOutOfBoundsException - if the index is out of range index < 0 || index >= size()}
    • set

      @Contract(mutates="this") public E set(int index, E element)
      Break Description
      SET_DOES_NOT_CHANGE_THE_ELEMENT The set method does not change the element.
      SET_ALWAYS_RETURNS_NULL The set method always returns null
      SET_CHANGES_THE_NEXT_ELEMENT The set methods changes the next element (i.e. at index + 1).
      SET_CHANGES_THE_PREVIOUS_ELEMENT The set methods changes the previous element (i.e. at index - 1).
      SET_RETURNS_NULL_ON_BAD_INDEX The set methods returns null on a bad index instead of throwing en exception.
      SET_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX The get methods throws the wrong exception on a bad index.

      A collection that has any of these breaks can be constructed using the builder:

          Collection<Integer> collection = Breakables.buildList(1,2,3,4,5)
              .withBreak(SET_DOES_NOT_CHANGE_THE_ELEMENT)
              .build();
      
      Specified by:
      set in interface List<E>
      Parameters:
      index - index of the element to return.
      element - the element to add at the index.
      Returns:
      the element at the specified position in this list, or possibly a different element if the list is broken.
      Throws:
      IndexOutOfBoundsException - if the index is out of range index < 0 || index >= size()}
    • add

      @Contract(mutates="this") public void add(int index, E element)

      Implements the add method from the List interface. This method can be broken using the following collection breaks:

      Break Description
      ADD_AT_INDEX_DOES_NOT_ADD_THE_ELEMENT The add method will not add an element to the collection
      ADD_AT_INDEX_ADDS_AT_NEXT_POSITION The add method adds the element at the next position.
      ADD_AT_INDEX_ADDS_AT_PREVIOUS_POSITION The add method adds the element at the previous position.
      ADD_AT_INDEX_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX The add method throws the wrong exception when the index is out of bounds.

      A collection that has any of these breaks can be constructed using the builder:

          Collection<Integer> collection = Breakables.buildList(1,2,3,4,5)
              .withBreak(ADD_AT_INDEX_DOES_NOT_ADD_THE_ELEMENT)
              .build();
      
      Specified by:
      add in interface List<E>
      Parameters:
      index - index at which the specified element is to be inserted
      element - element to be inserted
      Throws:
      UnsupportedOperationException - if the add operation is not supported by this list
      ClassCastException - if the class of the specified element prevents it from being added to this list
      NullPointerException - if the specified element is null and this list does not permit null elements
      IllegalArgumentException - if some property of the specified element prevents it from being added to this list
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
    • remove

      public E remove(int index)

      Implements the remove method from the List interface. This method can be broken using the following collection breaks:

      Break Description
      REMOVE_AT_INDEX_DOES_NOT_REMOVE_THE_ELEMENT The remove method does not remove the element.
      REMOVE_AT_INDEX_REMOVES_THE_NEXT_ELEMENT The remove method removes the element from the next position.
      REMOVE_AT_INDEX_REMOVES_THE_PREVIOUS_ELEMENT The remove method removes the element from the previous position.
      REMOVE_AT_INDEX_RETURNS_NULL_ON_BAD_INDEX The remove method returns null when the index is out of bounds.
      REMOVE_AT_INDEX_THROWS_WRONG_EXCEPTION_ON_BAD_INDEX The remove method throws the wrong exception when the index is out of bounds.
      REMOVE_AT_INDEX_ALWAYS_RETURNS_NULL The remove method always returns null

      A collection that has any of these breaks can be constructed using the builder:

          Collection<Integer> collection = Breakables.buildList(1,2,3,4,5)
              .withBreak(REMOVE_AT_INDEX_DOES_NOT_REMOVE_THE_ELEMENT)
              .build();
      
      Specified by:
      remove in interface List<E>
      Parameters:
      index - the index of the element to be removed
      Returns:
      the element previously at the specified position
      Throws:
      UnsupportedOperationException - if the remove operation is not supported by this list
      IndexOutOfBoundsException - if the index is out of range (@code index < 0 || index >= size())
    • indexOf

      public int indexOf(Object o)
      Specified by:
      indexOf in interface List<E>
    • lastIndexOf

      public int lastIndexOf(Object o)
      Specified by:
      lastIndexOf in interface List<E>
    • listIterator

      @NotNull public @NotNull ListIterator<E> listIterator()
      Specified by:
      listIterator in interface List<E>
    • listIterator

      @NotNull public @NotNull ListIterator<E> listIterator(int index)
      Specified by:
      listIterator in interface List<E>
    • subList

      @NotNull public @NotNull List<E> subList(int fromIndex, int toIndex)
      Specified by:
      subList in interface List<E>