Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2010 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.springframework.batch.item.support;
 
 

A org.springframework.batch.item.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(s):
Dave Syer
 
 public class SingleItemPeekableItemReader<T> implements ItemStreamReader<T>, PeekableItemReader<T> {
 
 	private ItemReader<T> delegate;
 
 	private T next;
 
The item reader to use as a delegate. Items are read from the delegate and passed to the caller in read().

Parameters:
delegate the delegate to set
 
 	public void setDelegate(ItemReader<T> delegate) {
 		this. = delegate;
 	}

Get the next item from the delegate (whether or not it has already been peeked at).

 
 		if ( != null) {
 			T item = ;
 			 = null;
 			// executionContext = new ExecutionContext();
 			return item;
 		}
 		return .read();
 	}

Peek at the next item, ensuring that if the delegate is an org.springframework.batch.item.ItemStream the state is stored for the next call to update(org.springframework.batch.item.ExecutionContext).

Returns:
the next item (or null if there is none).
See also:
org.springframework.batch.item.PeekableItemReader.peek()
 
 		if ( == null) {
 			 = .read();
 		}
 		return ;
 	}

If the delegate is an org.springframework.batch.item.ItemStream, just pass the call on, otherwise reset the peek cache.

	public void close() throws ItemStreamException {
		 = null;
		if ( instanceof ItemStream) {
		}
	}

If the delegate is an org.springframework.batch.item.ItemStream, just pass the call on, otherwise reset the peek cache.

	public void open(ExecutionContext executionContextthrows ItemStreamException {
		 = null;
		if ( instanceof ItemStream) {
			((ItemStream).open(executionContext);
		}
		executionContext = new ExecutionContext();
	}

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.

	public void update(ExecutionContext executionContextthrows ItemStreamException {
		if ( != null) {
			// Get the last state from the delegate instead of using
			// current value.
			for (Entry<StringObjectentry : this..entrySet()) {
				executionContext.put(entry.getKey(), entry.getValue());
			}
			return;
		}
		updateDelegate(executionContext);
	}
	private void updateDelegate(ExecutionContext executionContext) {
		if ( instanceof ItemStream) {
			((ItemStream).update(executionContext);
		}
	}
New to GrepCode? Check out our FAQ X