001/*
002 * Copyright 2006-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.sample.domain.multiline;
017
018/**
019 * A wrapper type for an item that is used by {@link AggregateItemReader} to
020 * identify the start and end of an aggregate record.
021 * 
022 * @see AggregateItemReader
023 * 
024 * @author Dave Syer
025 * 
026 */
027public class AggregateItem<T> {
028        @SuppressWarnings("rawtypes")
029        private static final AggregateItem FOOTER = new AggregateItem<Object>(false, true) {
030                @Override
031                public Object getItem() {
032                        throw new IllegalStateException("Footer record has no item.");
033                }
034        };
035
036        /**
037         * @param <T> the type of item nominally wrapped
038         * @return a static {@link AggregateItem} that is a footer.
039         */
040        @SuppressWarnings("unchecked")
041        public static <T> AggregateItem<T> getFooter() {
042                return FOOTER;
043        }
044
045        @SuppressWarnings("rawtypes")
046        private static final AggregateItem HEADER = new AggregateItem<Object>(true, false) {
047                @Override
048                public Object getItem() {
049                        throw new IllegalStateException("Header record has no item.");
050                }
051        };
052
053        /**
054         * @param <T> the type of item nominally wrapped
055         * @return a static {@link AggregateItem} that is a header.
056         */
057        @SuppressWarnings("unchecked")
058        public static <T> AggregateItem<T> getHeader() {
059                return HEADER;
060        }
061
062        private T item;
063
064        private boolean footer = false;
065
066        private boolean header = false;
067
068        /**
069         * @param item the item to wrap
070         */
071        public AggregateItem(T item) {
072                super();
073                this.item = item;
074        }
075
076        public AggregateItem(boolean header, boolean footer) {
077                this(null);
078                this.header = header;
079                this.footer = footer;
080        }
081
082        /**
083         * Accessor for the wrapped item.
084         * 
085         * @return the wrapped item
086         * @throws IllegalStateException if called on a record for which either
087         * {@link #isHeader()} or {@link #isFooter()} answers true.
088         */
089        public T getItem() {
090                return item;
091        }
092
093        /**
094         * Responds true if this record is a footer in an aggregate.
095         * @return true if this is the end of an aggregate record.
096         */
097        public boolean isFooter() {
098                return footer;
099        }
100
101        /**
102         * Responds true if this record is a header in an aggregate.
103         * @return true if this is the beginning of an aggregate record.
104         */
105        public boolean isHeader() {
106                return header;
107        }
108
109}