diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/ClassifierCompositeItemProcessor.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/ClassifierCompositeItemProcessor.java index a2ccc60c9..5b54bad99 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/ClassifierCompositeItemProcessor.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/ClassifierCompositeItemProcessor.java @@ -36,7 +36,8 @@ public class ClassifierCompositeItemProcessor implements ItemProcessor> (null); /** - * @param classifier the classifier to set + * Establishes the classifier that will determine which {@link ItemProcessor} to use. + * @param classifier the {@link Classifier} to set */ public void setClassifier(Classifier> classifier) { this.classifier = classifier; diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/builder/ClassifierCompositeItemProcessorBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/builder/ClassifierCompositeItemProcessorBuilder.java new file mode 100644 index 000000000..893e20e89 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/builder/ClassifierCompositeItemProcessorBuilder.java @@ -0,0 +1,59 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.batch.item.support.builder; + +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.support.ClassifierCompositeItemProcessor; +import org.springframework.classify.Classifier; +import org.springframework.util.Assert; + +/** + * Creates a fully qualified {@link ClassifierCompositeItemProcessor}. + * + * @author Glenn Renfro + * + * @since 4.0 + */ +public class ClassifierCompositeItemProcessorBuilder { + + private Classifier> classifier; + + /** + * Establishes the classifier that will determine which {@link ItemProcessor} to use. + * @param classifier the classifier to set + * @return this instance for method chaining + * @see ClassifierCompositeItemProcessor#setClassifier(Classifier) + */ + public ClassifierCompositeItemProcessorBuilder classifier(Classifier> classifier) { + this.classifier = classifier; + + return this; + } + + /** + * Returns a fully constructed {@link ClassifierCompositeItemProcessor}. + * + * @return a new {@link ClassifierCompositeItemProcessor} + */ + public ClassifierCompositeItemProcessor build() { + Assert.notNull(classifier, "A classifier is required."); + + ClassifierCompositeItemProcessor processor = new ClassifierCompositeItemProcessor<>(); + processor.setClassifier(this.classifier); + return processor; + } +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/builder/ClassifierCompositeItemProcessorBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/builder/ClassifierCompositeItemProcessorBuilderTests.java new file mode 100644 index 000000000..737edc9e0 --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/builder/ClassifierCompositeItemProcessorBuilderTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.batch.item.support.builder; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.support.ClassifierCompositeItemProcessor; +import org.springframework.classify.PatternMatchingClassifier; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * @author Glenn Renfro + */ +public class ClassifierCompositeItemProcessorBuilderTests { + + @Test + public void testBasicClassifierCompositeItemProcessor() throws Exception { + + ItemProcessor fooProcessor = item -> "foo: " + item; + ItemProcessor defaultProcessor = item -> item; + + Map> routingConfiguration = new HashMap<>(); + routingConfiguration.put("foo", fooProcessor); + routingConfiguration.put("*", defaultProcessor); + ClassifierCompositeItemProcessor processor = new ClassifierCompositeItemProcessorBuilder() + .classifier(new PatternMatchingClassifier<>(routingConfiguration)) + .build(); + + assertEquals("bar", processor.process("bar")); + assertEquals("foo: foo", processor.process("foo")); + assertEquals("baz", processor.process("baz")); + } + + @Test + public void testNullClassifier() { + try { + new ClassifierCompositeItemProcessorBuilder().build(); + fail("IllegalArgumentException should have been thrown"); + } + catch (IllegalArgumentException iae) { + assertEquals("IllegalArgumentException message did not match the expected result.", + "A classifier is required.", iae.getMessage()); + } + } +}