EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.item.support]

COVERAGE SUMMARY FOR SOURCE FILE [SingleItemPeekableItemReader.java]

nameclass, %method, %block, %line, %
SingleItemPeekableItemReader.java100% (1/1)100% (8/8)100% (114/114)100% (32/32)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SingleItemPeekableItemReader100% (1/1)100% (8/8)100% (114/114)100% (32/32)
SingleItemPeekableItemReader (): void 100% (1/1)100% (8/8)100% (2/2)
close (): void 100% (1/1)100% (17/17)100% (5/5)
open (ExecutionContext): void 100% (1/1)100% (17/17)100% (5/5)
peek (): Object 100% (1/1)100% (15/15)100% (4/4)
read (): Object 100% (1/1)100% (15/15)100% (5/5)
setDelegate (ItemReader): void 100% (1/1)100% (4/4)100% (2/2)
update (ExecutionContext): void 100% (1/1)100% (28/28)100% (6/6)
updateDelegate (ExecutionContext): void 100% (1/1)100% (10/10)100% (3/3)

1/*
2 * Copyright 2006-2010 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.springframework.batch.item.support;
17 
18import java.util.Map.Entry;
19 
20import org.springframework.batch.item.ExecutionContext;
21import org.springframework.batch.item.ItemReader;
22import org.springframework.batch.item.ItemStream;
23import org.springframework.batch.item.ItemStreamException;
24import org.springframework.batch.item.ItemStreamReader;
25import org.springframework.batch.item.ParseException;
26import org.springframework.batch.item.PeekableItemReader;
27import org.springframework.batch.item.UnexpectedInputException;
28 
29/**
30 * <p>
31 * A {@link PeekableItemReader} that allows the user to peek one item ahead.
32 * Repeated calls to {@link #peek()} will return the same item, and this will be
33 * the next item returned from {@link #read()}.
34 * </p>
35 * 
36 * <p>
37 * Intentionally not thread safe: it wouldn't be possible to honour the peek in
38 * multiple threads because only one of the threads that peeked would get that
39 * item in the next call to read.
40 * </p>
41 * 
42 * @author Dave Syer
43 * 
44 */
45public class SingleItemPeekableItemReader<T> implements ItemStreamReader<T>, PeekableItemReader<T> {
46 
47        private ItemReader<T> delegate;
48 
49        private T next;
50 
51        private ExecutionContext executionContext = new ExecutionContext();
52 
53        /**
54         * The item reader to use as a delegate. Items are read from the delegate
55         * and passed to the caller in {@link #read()}.
56         * 
57         * @param delegate the delegate to set
58         */
59        public void setDelegate(ItemReader<T> delegate) {
60                this.delegate = delegate;
61        }
62 
63        /**
64         * Get the next item from the delegate (whether or not it has already been
65         * peeked at).
66         * 
67         * @see ItemReader#read()
68         */
69        public T read() throws Exception, UnexpectedInputException, ParseException {
70                if (next != null) {
71                        T item = next;
72                        next = null;
73                        // executionContext = new ExecutionContext();
74                        return item;
75                }
76                return delegate.read();
77        }
78 
79        /**
80         * Peek at the next item, ensuring that if the delegate is an
81         * {@link ItemStream} the state is stored for the next call to
82         * {@link #update(ExecutionContext)}.
83         * 
84         * @return the next item (or null if there is none).
85         * 
86         * @see PeekableItemReader#peek()
87         */
88        public T peek() throws Exception, UnexpectedInputException, ParseException {
89                if (next == null) {
90                        updateDelegate(executionContext);
91                        next = delegate.read();
92                }
93                return next;
94        }
95 
96        /**
97         * If the delegate is an {@link ItemStream}, just pass the call on,
98         * otherwise reset the peek cache.
99         * 
100         * @throws ItemStreamException if there is a problem
101         * @see ItemStream#close()
102         */
103        public void close() throws ItemStreamException {
104                next = null;
105                if (delegate instanceof ItemStream) {
106                        ((ItemStream) delegate).close();
107                }
108                executionContext = new ExecutionContext();
109        }
110 
111        /**
112         * If the delegate is an {@link ItemStream}, just pass the call on,
113         * otherwise reset the peek cache.
114         * 
115         * @param executionContext the current context
116         * @throws ItemStreamException if there is a problem
117         * @see ItemStream#open(ExecutionContext)
118         */
119        public void open(ExecutionContext executionContext) throws ItemStreamException {
120                next = null;
121                if (delegate instanceof ItemStream) {
122                        ((ItemStream) delegate).open(executionContext);
123                }
124                executionContext = new ExecutionContext();
125        }
126 
127        /**
128         * If there is a cached peek, then retrieve the execution context state from
129         * that point. If there is no peek cached, then call directly to the
130         * delegate.
131         * 
132         * @param executionContext the current context
133         * @throws ItemStreamException if there is a problem
134         * @see ItemStream#update(ExecutionContext)
135         */
136        public void update(ExecutionContext executionContext) throws ItemStreamException {
137                if (next != null) {
138                        // Get the last state from the delegate instead of using
139                        // current value.
140                        for (Entry<String, Object> entry : this.executionContext.entrySet()) {
141                                executionContext.put(entry.getKey(), entry.getValue());
142                        }
143                        return;
144                }
145                updateDelegate(executionContext);
146        }
147 
148        private void updateDelegate(ExecutionContext executionContext) {
149                if (delegate instanceof ItemStream) {
150                        ((ItemStream) delegate).update(executionContext);
151                }
152        }
153 
154}

[all classes][org.springframework.batch.item.support]
EMMA 2.0.5312 (C) Vladimir Roubtsov