Added Builder for ClassifierCompositeItemProcessor

resolves #BATCH-2618
This commit is contained in:
Glenn Renfro
2017-05-22 16:12:32 -04:00
committed by Michael Minella
parent cf9cec3981
commit 332a2017b3
3 changed files with 126 additions and 1 deletions

View File

@@ -36,7 +36,8 @@ public class ClassifierCompositeItemProcessor<I,O> implements ItemProcessor<I, O
new ClassifierSupport<I, ItemProcessor<?, ? extends O>> (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<? super I, ItemProcessor<?, ? extends O>> classifier) {
this.classifier = classifier;

View File

@@ -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<I, O> {
private Classifier<? super I, ItemProcessor<?, ? extends O>> 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<I, O> classifier(Classifier<? super I, ItemProcessor<?, ? extends O>> classifier) {
this.classifier = classifier;
return this;
}
/**
* Returns a fully constructed {@link ClassifierCompositeItemProcessor}.
*
* @return a new {@link ClassifierCompositeItemProcessor}
*/
public ClassifierCompositeItemProcessor<I, O> build() {
Assert.notNull(classifier, "A classifier is required.");
ClassifierCompositeItemProcessor<I, O> processor = new ClassifierCompositeItemProcessor<>();
processor.setClassifier(this.classifier);
return processor;
}
}

View File

@@ -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<String, String> fooProcessor = item -> "foo: " + item;
ItemProcessor<String, String> defaultProcessor = item -> item;
Map<String, ItemProcessor<?, ? extends String>> routingConfiguration = new HashMap<>();
routingConfiguration.put("foo", fooProcessor);
routingConfiguration.put("*", defaultProcessor);
ClassifierCompositeItemProcessor<String, String> processor = new ClassifierCompositeItemProcessorBuilder<String, String>()
.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<String, String>().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());
}
}
}