Class BreakableIterator<E>
- Type Parameters:
E
- the element type.
- All Implemented Interfaces:
Iterator<E>
,Breakable
,SupportedMethods
An iterator that can be broken in well-defined ways in order to test collection utilities or testing classes.
Breaks
The breaks that are supported for this class are listed in the description of the method that they impact.
Any breaks that are not listed in this class can be added to an instance of BreakableIterator
, but will
not have any impact on how it functions.
- Since:
- 1.0
- Author:
- evanbergstrom
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Break
The iterator 'forEachRemaining' does not call the actionstatic final Break
The iterator 'forEachRemaining' throws the wrong exception for anull
argument.static final Break
The iteratorhasNext
method will always return a false.static final Break
The iteratorhasNext
method will always return a true.static final Break
The iteratorhasNext
method will always return the opposite valuestatic final Break
The iterator will always have no elementsstatic final Break
The iteratornext
method will always returnnull
static final Break
The iteratornext
method will always returnnull
static final Break
The iterator 'remove' method does not remove the element at the current iterator position.static final Break
The iterator 'remove' method throws the wrong exception for an illegal statestatic final Break
The iterator 'remove' method throws the wrong exception nif it is not supported.static final Break
The iterator will skip the first element. -
Constructor Summary
ConstructorsConstructorDescriptionBreakableIterator
(Iterator<E> iterator, Collection<Break> breaks, int characteristics) Constructs a breakable iterator from a iterator that will provide the implementation. -
Method Summary
Modifier and TypeMethodDescriptionvoid
forEachRemaining
(Consumer<? super E> action) Implements the forEachRemaining method from theIterator
interface.boolean
hasNext()
next()
void
remove()
Methods inherited from class org.soliscode.test.breakable.AbstractBreakable
addBreaks, breaks, hasBreak
Methods inherited from class org.soliscode.test.OptionalMethodSupport
doesNotSupportMethod, supportsMethod, unsupportedMethods
-
Field Details
-
ITERATOR_IS_ALWAYS_EMPTY
The iterator will always have no elements -
ITERATOR_SKIPS_FIRST_ELEMENT
The iterator will skip the first element. -
ITERATOR_HAS_NEXT_ALWAYS_RETURNS_TRUE
The iteratorhasNext
method will always return a true.- See Also:
-
ITERATOR_HAS_NEXT_ALWAYS_RETURNS_FALSE
The iteratorhasNext
method will always return a false.- See Also:
-
ITERATOR_HAS_NEXT_RETURNS_OPPOSITE_VALUE
The iteratorhasNext
method will always return the opposite value- See Also:
-
ITERATOR_NEXT_ALWAYS_RETURNS_NULL
The iteratornext
method will always returnnull
- See Also:
-
ITERATOR_NEXT_THROWS_WRONG_EXCEPTION
The iteratornext
method will always returnnull
- See Also:
-
ITERATOR_REMOVE_DOES_NOT_REMOVE_ELEMENT
The iterator 'remove' method does not remove the element at the current iterator position.- See Also:
-
ITERATOR_REMOVE_THROWS_WRONG_EXCEPTION_FOR_ILLEGAL_STATE
The iterator 'remove' method throws the wrong exception for an illegal state- See Also:
-
ITERATOR_REMOVE_THROWS_WRONG_EXCEPTION_IF_NOT_SUPPORTED
The iterator 'remove' method throws the wrong exception nif it is not supported.- See Also:
-
ITERATOR_FOR_EACH_REMAINING_DOES_NOT_CALL_ACTION
The iterator 'forEachRemaining' does not call the action- See Also:
-
ITERATOR_FOR_EACH_REMAINING_THROWS_WRONG_EXCEPTION_FOR_NULL_ARGUMENT
The iterator 'forEachRemaining' throws the wrong exception for anull
argument.- See Also:
-
-
Constructor Details
-
BreakableIterator
Constructs a breakable iterator from a iterator that will provide the implementation.- Parameters:
iterator
- The iterator that will provide the implementationbreaks
- The breaks that define how the iterator is broken.characteristics
- A mask that indicates the characteristics of the iterator.
-
-
Method Details
-
hasNext
@Contract(pure=true) public boolean hasNext()Implements the hasNext method from the
Iterator
interface.Breaks
This method can be broken using the following collection breaks:
- ITERATOR_IS_ALWAYS_EMPTY
- ITERATOR_HAS_NEXT_ALWAYS_RETURNS_TRUE
- ITERATOR_HAS_NEXT_ALWAYS_RETURNS_FALSE
- ITERATOR_HAS_NEXT_RETURNS_OPPOSITE_VALUE
Example
A iterator that has any of these breaks can be constructed using the
BreakableIterator
builder:Iterator<Integer> iterator = Breakable.iterableOf(1,2,3,4,5) .withBreak(ITERATOR_HAS_NEXT_ALWAYS_RETURNS_TRUE) .build().iterator();
-
next
Implements the next method from the
Iterator
interface.#Breaks This method can be broken using the following collection breaks:
A iterator that has any of these breaks can be constructed using the
BreakableIterator
builder:Iterator<Integer> iterator = Breakable.iterableOf(1,2,3,4,5) .withBreak(ITERATOR_NEXT_ALWAYS_RETURNS_NULL) .build().iterator();
- Specified by:
next
in interfaceIterator<E>
- Returns:
- The next element or
null
if there sare no more elements or if the iterator is broken. - Throws:
NoSuchElementException
- if the iteration has no more elements- See Also:
-
remove
public void remove()Implements the remove method from the
Iterator
interface.#Breaks This method can be broken using the following collection breaks:
- ITERATOR_REMOVE_DOES_NOT_REMOVE_ELEMENT
- ITERATOR_REMOVE_THROWS_WRONG_EXCEPTION_IF_NOT_SUPPORTED
- ITERATOR_REMOVE_THROWS_WRONG_EXCEPTION_FOR_ILLEGAL_STATE
A iterator that has any of these breaks can be constructed using the
BreakableIterator
builder:Iterator<Integer> iterator = Breakable.iterableOf(1,2,3,4,5) .withBreak(ITERATOR_REMOVE_DOES_NOT_REMOVE_ELEMENT) .build().iterator();
- Specified by:
remove
in interfaceIterator<E>
- Throws:
UnsupportedOperationException
- if theremove
operation is not supported by this iteratorIllegalStateException
- if thenext
method has not yet been called, or theremove
method has already been called after the last call to thenext
method.- See Also:
-
forEachRemaining
Implements the forEachRemaining method from the
Iterator
interface.#Breaks This method can be broken using the following iterable breaks:
- ITERATOR_FOR_EACH_REMAINING_DOES_NOT_CALL_ACTION
- ITERATOR_FOR_EACH_REMAINING_THROWS_WRONG_EXCEPTION_FOR_NULL_ARGUMENT
A collection that has any of these breaks can be constructed using the builder:
Iterable<Integer> iterable = Breakable.iterableOf(1,2,3,4,5) .withBreak(ITERATOR_FOR_EACH_REMAINING_DOES_NOT_CALL_ACTION) .build();
- Specified by:
forEachRemaining
in interfaceIterator<E>
- Parameters:
action
- The action to run on the remaining elements.- Throws:
NullPointerException
- if the specified action is null
-