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  package org.springframework.batch.sample.domain.multiline;
17  
18  import org.springframework.batch.item.ItemReaderException;
19  
20  /**
21   * A wrapper type for an item that is used by {@link AggregateItemReader} to
22   * identify the start and end of an aggregate record.
23   * 
24   * @see AggregateItemReader
25   * 
26   * @author Dave Syer
27   * 
28   */
29  public class AggregateItem<T> {
30  
31  	@SuppressWarnings("rawtypes")
32  	private static final AggregateItem FOOTER = new AggregateItem<Object>(false, true) {
33  		@Override
34  		public Object getItem() throws ItemReaderException {
35  			throw new IllegalStateException("Footer record has no item.");
36  		}
37  	};
38  
39  	/**
40  	 * @param <T> the type of item nominally wrapped
41  	 * @return a static {@link AggregateItem} that is a footer.
42  	 */
43  	@SuppressWarnings("unchecked")
44  	public static final <T> AggregateItem<T> getFooter() {
45  		return FOOTER;
46  	}
47  
48  	@SuppressWarnings("rawtypes")
49  	private static final AggregateItem HEADER = new AggregateItem<Object>(true, false) {
50  		@Override
51  		public Object getItem() throws ItemReaderException {
52  			throw new IllegalStateException("Header record has no item.");
53  		}
54  	};
55  
56  	/**
57  	 * @param <T> the type of item nominally wrapped
58  	 * @return a static {@link AggregateItem} that is a header.
59  	 */
60  	@SuppressWarnings("unchecked")
61  	public static final <T> AggregateItem<T> getHeader() {
62  		return HEADER;
63  	}
64  
65  	private T item;
66  
67  	private boolean footer = false;
68  
69  	private boolean header = false;
70  
71  	/**
72  	 * @param item
73  	 */
74  	public AggregateItem(T item) {
75  		super();
76  		this.item = item;
77  	}
78  
79  	public AggregateItem(boolean header, boolean footer) {
80  		this(null);
81  		this.header = header;
82  		this.footer = footer;
83  	}
84  
85  	/**
86  	 * Accessor for the wrapped item.
87  	 * 
88  	 * @return the wrapped item
89  	 * @throws IllegalStateException if called on a record for which either
90  	 * {@link #isHeader()} or {@link #isFooter()} answers true.
91  	 */
92  	public T getItem() throws IllegalStateException {
93  		return item;
94  	}
95  
96  	/**
97  	 * Responds true if this record is a footer in an aggregate.
98  	 * @return true if this is the end of an aggregate record.
99  	 */
100 	public boolean isFooter() {
101 		return footer;
102 	}
103 
104 	/**
105 	 * Responds true if this record is a header in an aggregate.
106 	 * @return true if this is the beginning of an aggregate record.
107 	 */
108 	public boolean isHeader() {
109 		return header;
110 	}
111 
112 }