001/*
002 * Copyright 2017 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 */
016
017package org.springframework.batch.item.amqp.builder;
018
019import org.springframework.amqp.core.AmqpTemplate;
020import org.springframework.batch.item.amqp.AmqpItemReader;
021import org.springframework.util.Assert;
022
023/**
024 * A builder implementation for the {@link AmqpItemReader}
025 *
026 * @author Glenn Renfro
027 * @since 4.0
028 * @see AmqpItemReader
029 */
030public class AmqpItemReaderBuilder<T> {
031
032        private AmqpTemplate amqpTemplate;
033
034        private Class<? extends T> itemType;
035
036        /**
037         * Establish the amqpTemplate to be used by the AmqpItemReader.
038         * @param amqpTemplate the template to be used.
039         * @return this instance for method chaining
040         * @see AmqpItemReader#AmqpItemReader(AmqpTemplate)
041         */
042        public AmqpItemReaderBuilder<T> amqpTemplate(AmqpTemplate amqpTemplate) {
043                this.amqpTemplate = amqpTemplate;
044
045                return this;
046        }
047
048        /**
049         * Establish the itemType for the reader.
050         * @param itemType class type that will be returned by the reader.
051         * @return this instance for method chaining.
052         * @see AmqpItemReader#setItemType(Class)
053         */
054        public AmqpItemReaderBuilder<T> itemType(Class<? extends T> itemType) {
055                this.itemType = itemType;
056
057                return this;
058        }
059
060        /**
061         * Validates and builds a {@link AmqpItemReader}.
062         *
063         * @return a {@link AmqpItemReader}
064         */
065        public AmqpItemReader<T> build() {
066                Assert.notNull(this.amqpTemplate, "amqpTemplate is required.");
067
068                AmqpItemReader<T> reader = new AmqpItemReader<>(this.amqpTemplate);
069                if(this.itemType != null) {
070                        reader.setItemType(this.itemType);
071                }
072
073                return reader;
074        }
075}