Class SingleItemPeekableItemReader<T>
java.lang.Object
org.springframework.batch.infrastructure.item.support.SingleItemPeekableItemReader<T>
- All Implemented Interfaces:
ItemReader<T>,ItemStream,ItemStreamReader<T>,PeekableItemReader<T>
public class SingleItemPeekableItemReader<T>
extends Object
implements ItemStreamReader<T>, PeekableItemReader<T>
A PeekableItemReader that allows the user to peek one item ahead. Repeated
calls to peek() will return the same item, and this will be the next item
returned from read().
Intentionally not thread-safe: it wouldn't be possible to honour the peek in multiple threads because only one of the threads that peeked would get that item in the next call to read.
- Author:
- Dave Syer, Mahmoud Ben Hassine
-
Constructor Summary
ConstructorsConstructorDescriptionSingleItemPeekableItemReader(ItemReader<T> delegate) Create a newSingleItemPeekableItemReaderwith the given delegate. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()If the delegate is anItemStream, just pass the call on, otherwise reset the peek cache.voidopen(ExecutionContext executionContext) If the delegate is anItemStream, just pass the call on, otherwise reset the peek cache.@Nullable Tpeek()Peek at the next item, ensuring that if the delegate is anItemStreamthe state is stored for the next call toupdate(ExecutionContext).@Nullable Tread()Get the next item from the delegate (whether or not it has already been peeked at).voidsetDelegate(ItemReader<T> delegate) The item reader to use as a delegate.voidupdate(ExecutionContext executionContext) If there is a cached peek, then retrieve the execution context state from that point.
-
Constructor Details
-
SingleItemPeekableItemReader
Create a newSingleItemPeekableItemReaderwith the given delegate.- Parameters:
delegate- the item reader to use as a delegate- Since:
- 6.0
-
-
Method Details
-
setDelegate
The item reader to use as a delegate. Items are read from the delegate and passed to the caller inread().- Parameters:
delegate- the delegate to set
-
read
Get the next item from the delegate (whether or not it has already been peeked at).- Specified by:
readin interfaceItemReader<T>- Returns:
- T the item to be processed or
nullif the data source is exhausted - Throws:
Exception- if an error occurs.- See Also:
-
peek
Peek at the next item, ensuring that if the delegate is anItemStreamthe state is stored for the next call toupdate(ExecutionContext).- Specified by:
peekin interfacePeekableItemReader<T>- Returns:
- the next item (or null if there is none).
- Throws:
Exception- if there is a problem- See Also:
-
close
If the delegate is anItemStream, just pass the call on, otherwise reset the peek cache.- Specified by:
closein interfaceItemStream- Throws:
ItemStreamException- if there is a problem- See Also:
-
open
If the delegate is anItemStream, just pass the call on, otherwise reset the peek cache.- Specified by:
openin interfaceItemStream- Parameters:
executionContext- the current context- Throws:
ItemStreamException- if there is a problem- See Also:
-
update
If there is a cached peek, then retrieve the execution context state from that point. If there is no peek cached, then call directly to the delegate.- Specified by:
updatein interfaceItemStream- Parameters:
executionContext- the current context- Throws:
ItemStreamException- if there is a problem- See Also:
-