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 org.springframework.batch.item.ItemProcessor;
020import org.springframework.batch.item.support.ClassifierCompositeItemProcessor;
021import org.springframework.classify.Classifier;
022import org.springframework.util.Assert;
023
024/**
025 * Creates a fully qualified {@link ClassifierCompositeItemProcessor}.
026 *
027 * @author Glenn Renfro
028 *
029 * @since 4.0
030 */
031public class ClassifierCompositeItemProcessorBuilder<I, O> {
032
033        private Classifier<? super I, ItemProcessor<?, ? extends O>> classifier;
034
035        /**
036         * Establishes the classifier that will determine which {@link ItemProcessor} to use.
037         * @param classifier the classifier to set
038         * @return this instance for method chaining
039         * @see ClassifierCompositeItemProcessor#setClassifier(Classifier)
040         */
041        public ClassifierCompositeItemProcessorBuilder<I, O> classifier(Classifier<? super I, ItemProcessor<?, ? extends O>> classifier) {
042                this.classifier = classifier;
043
044                return this;
045        }
046
047        /**
048         * Returns a fully constructed {@link ClassifierCompositeItemProcessor}.
049         *
050         * @return a new {@link ClassifierCompositeItemProcessor}
051         */
052        public ClassifierCompositeItemProcessor<I, O> build() {
053                Assert.notNull(classifier, "A classifier is required.");
054
055                ClassifierCompositeItemProcessor<I, O> processor = new ClassifierCompositeItemProcessor<>();
056                processor.setClassifier(this.classifier);
057                return processor;
058        }
059}