Skip to content
This repository has been archived by the owner on Jan 4, 2020. It is now read-only.

Commit

Permalink
toughened up lookAhead
Browse files Browse the repository at this point in the history
  • Loading branch information
Torsten Krause committed Mar 3, 2016
1 parent fe58293 commit af7395e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
11 changes: 8 additions & 3 deletions src/main/java/net/markenwerk/commons/iterators/LookAhead.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
package net.markenwerk.commons.iterators;

import java.util.NoSuchElementException;

/**
* A {@link LookAhead} is a simple container that holds the
* {@link LookAhead#get() current} and the {@link LookAhead#getNext() next}
Expand All @@ -30,7 +32,7 @@
* @since 1.1.6
*
* @param <Payload>
* The payload type.
* The payload type.
*/
public interface LookAhead<Payload> {

Expand All @@ -47,9 +49,12 @@ public interface LookAhead<Payload> {
* {@literal LookAhead#hashCode() next} payload object.
*
* @return The next payload object. May be {@literal null}.
*
* @throws NoSuchElementException
* If this {@link LookAhead} has no {@link LookAhead#getNext()
* next} payload object.
*/

public Payload getNext();
public Payload getNext() throws NoSuchElementException;

/**
* Returns whether this {@link LookAhead} has a next payload object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* the current and the next payload value of the given {@link Iterator}.
*
* @param <Payload>
* The payload type.
* The payload type.
* @author Torsten Krause (tk at markenwerk dot net)
* @since 1.1.6
*/
Expand Down Expand Up @@ -66,8 +66,12 @@ public Payload get() {
}

@Override
public Payload getNext() {
return next;
public Payload getNext() throws NoSuchElementException {
if (!nextExists) {
throw new NoSuchElementException("LookAhead has no further element");
} else {
return next;
}
}

@Override
Expand All @@ -82,7 +86,7 @@ public boolean isLast() {

@Override
public String toString() {
return "Ahead [current=" + current + ", next=" + next + ", hasNext=" + nextExists + "]";
return "LookAhead [current=" + current + ", next=" + next + ", hasNext=" + nextExists + "]";
}

}
Expand All @@ -100,11 +104,11 @@ public String toString() {
* and the given {@link Predicate}.
*
* @param iterator
* The {@link Iterator}, around which the new
* {@link LookAheadIterator} will be wrapped.
* The {@link Iterator}, around which the new
* {@link LookAheadIterator} will be wrapped.
*
* @throws IllegalArgumentException
* If the given {@link Iterator} is {@literal null}.
* If the given {@link Iterator} is {@literal null}.
*/
public LookAheadIterator(Iterator<? extends Payload> iterator) throws IllegalArgumentException {
if (null == iterator) {
Expand Down

0 comments on commit af7395e

Please sign in to comment.