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