Create Stream#pushBack() method
This commit is contained in:
parent
e98a07dd5e
commit
82dd3c2968
|
@ -115,6 +115,15 @@ public class Stream<E> implements Iterable<E> {
|
||||||
throw new UnexpectedElementException(lookahead, expected);
|
throw new UnexpectedElementException(lookahead, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pushes back one element at a time.
|
||||||
|
*/
|
||||||
|
public void pushBack() {
|
||||||
|
if (offset > 0) {
|
||||||
|
offset--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the next element in this stream without consuming it.
|
* Returns the next element in this stream without consuming it.
|
||||||
*
|
*
|
||||||
|
|
|
@ -203,4 +203,29 @@ public class StreamTest {
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldPushBackOneElementAtATime() {
|
||||||
|
Stream<Character> stream = new Stream<Character>(
|
||||||
|
new Character[] {'a', 'b', 'c'}
|
||||||
|
);
|
||||||
|
assertEquals(Character.valueOf('a'), stream.consume());
|
||||||
|
stream.pushBack();
|
||||||
|
assertEquals(Character.valueOf('a'), stream.consume());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldStopPushingBackWhenThereAreNoElements() {
|
||||||
|
Stream<Character> stream = new Stream<Character>(
|
||||||
|
new Character[] {'a', 'b', 'c'}
|
||||||
|
);
|
||||||
|
assertEquals(Character.valueOf('a'), stream.consume());
|
||||||
|
assertEquals(Character.valueOf('b'), stream.consume());
|
||||||
|
assertEquals(Character.valueOf('c'), stream.consume());
|
||||||
|
stream.pushBack();
|
||||||
|
stream.pushBack();
|
||||||
|
stream.pushBack();
|
||||||
|
stream.pushBack();
|
||||||
|
assertEquals(Character.valueOf('a'), stream.consume());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user