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.support.builder;
018
019import java.util.List;
020
021import org.springframework.batch.item.ItemProcessor;
022import org.springframework.batch.item.support.CompositeItemProcessor;
023import org.springframework.util.Assert;
024
025/**
026 * Creates a fully qualified {@link CompositeItemProcessorBuilder}.
027 *
028 * @author Glenn Renfro
029 *
030 * @since 4.0
031 */
032public class CompositeItemProcessorBuilder<I, O> {
033        private List<? extends ItemProcessor<?, ?>> delegates;
034
035        /**
036         * Establishes the {@link ItemProcessor} delegates that will work on the item to be processed.
037         * @param delegates list of {@link ItemProcessor} delegates that will work on the item.
038         * @return this instance for method chaining.
039         * @see CompositeItemProcessor#setDelegates(List)
040         */
041        public CompositeItemProcessorBuilder<I, O> delegates(List<? extends ItemProcessor<?, ?>> delegates) {
042                this.delegates = delegates;
043
044                return this;
045        }
046
047        /**
048         * Returns a fully constructed {@link CompositeItemProcessor}.
049         *
050         * @return a new {@link CompositeItemProcessor}
051         */
052        public CompositeItemProcessor<I, O> build() {
053                Assert.notNull(delegates, "A list of delegates is required.");
054                Assert.notEmpty(delegates, "The delegates list must have one or more delegates.");
055
056                CompositeItemProcessor<I, O> processor = new CompositeItemProcessor<>();
057                processor.setDelegates(this.delegates);
058                return processor;
059        }
060}