View Javadoc

1   /*
2    * Copyright 2006-2007 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   */
16  
17  package org.springframework.batch.sample.domain.trade.internal;
18  
19  import java.math.BigDecimal;
20  
21  import org.springframework.batch.item.ItemReader;
22  import org.springframework.batch.sample.domain.trade.Trade;
23  
24  /**
25   * Generates configurable number of {@link Trade} items.
26   * 
27   * @author Robert Kasanicky
28   */
29  public class GeneratingTradeItemReader implements ItemReader<Trade> {
30  
31  	private int limit = 1;
32  	
33  	private int counter = 0;
34  
35  	public Trade read() throws Exception {
36  		if (counter < limit) {
37  			counter++;
38  			return new Trade(
39  					"isin" + counter, 
40  					counter, 
41  					new BigDecimal(counter), 
42  					"customer" + counter);
43  		}
44  		return null;
45  	}
46  
47  	/**
48  	 * @param limit number of items that will be generated
49  	 * (null returned on consecutive calls).
50  	 */
51  	public void setLimit(int limit) {
52  		this.limit = limit;
53  	}
54  
55  	public int getCounter() {
56  		return counter;
57  	}
58  
59  	public int getLimit() {
60  		return limit;
61  	}
62  
63  	public void resetCounter()
64  	{
65  		this.counter = 0;
66  	}
67  }