RESOLVED - issue BATCH-1109: Generalise PrefixMatching* to PatternMatching*

This commit is contained in:
dsyer
2009-03-03 17:11:49 +00:00
parent 7fd99c2344
commit f2f7a12eaa
7 changed files with 105 additions and 121 deletions

View File

@@ -16,36 +16,36 @@
package org.springframework.batch.item.file.mapping;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.batch.item.file.transform.LineTokenizer;
import org.springframework.batch.item.file.transform.PrefixMatchingCompositeLineTokenizer;
import org.springframework.batch.item.file.transform.PatternMatchingCompositeLineTokenizer;
import org.springframework.batch.support.PatternMatcher;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
/**
* <p>
* A {@link LineMapper} implementation that stores a mapping of String prefixes
* to delegate {@link LineTokenizer}s as well as a mapping of String prefixes to
* A {@link LineMapper} implementation that stores a mapping of String patterns
* to delegate {@link LineTokenizer}s as well as a mapping of String patterns to
* delegate {@link FieldSetMapper}s. Each line received will be tokenized and
* then mapped to a field set.
*
* <p>
* Both the tokenizing and the mapping work in a similar way. The line will be
* checked for its prefix. If the prefix matches a key in the map of delegates,
* then the corresponding delegate will be used. Prefixes are sorted starting
* with the most specific, and the first match always succeeds.
* checked for its matching pattern. If the key matches a pattern in the map of
* delegates, then the corresponding delegate will be used. Patterns are sorted
* starting with the most specific, and the first match succeeds.
*
* @see PrefixMatchingCompositeLineTokenizer
* @see PatternMatchingCompositeLineTokenizer
*
* @author Dan Garrette
* @author Dave Syer
* @since 2.0
*/
public class PrefixMatchingCompositeLineMapper<T> implements LineMapper<T>, InitializingBean {
public class PatternMatchingCompositeLineMapper<T> implements LineMapper<T>, InitializingBean {
private PrefixMatchingCompositeLineTokenizer tokenizer = new PrefixMatchingCompositeLineTokenizer();
private PatternMatchingCompositeLineTokenizer tokenizer = new PatternMatchingCompositeLineTokenizer();
private PatternMatcher<FieldSetMapper<T>> patternMatcher;
@@ -77,14 +77,6 @@ public class PrefixMatchingCompositeLineMapper<T> implements LineMapper<T>, Init
public void setFieldSetMappers(Map<String, FieldSetMapper<T>> fieldSetMappers) {
Assert.isTrue(!fieldSetMappers.isEmpty(), "The 'fieldSetMappers' property must be non-empty");
LinkedHashMap<String, FieldSetMapper<T>> map = new LinkedHashMap<String, FieldSetMapper<T>>();
for (String key : fieldSetMappers.keySet()) {
FieldSetMapper<T> value = fieldSetMappers.get(key);
if (!key.endsWith("*")) {
key = key + "*";
}
map.put(key, value);
}
this.patternMatcher = new PatternMatcher<FieldSetMapper<T>>(map);
this.patternMatcher = new PatternMatcher<FieldSetMapper<T>>(fieldSetMappers);
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.batch.item.file.transform;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.batch.support.PatternMatcher;
@@ -25,17 +24,17 @@ import org.springframework.util.Assert;
/**
* A {@link LineTokenizer} implementation that stores a mapping of String
* prefixes to delegate {@link LineTokenizer}s. Each line tokenizied will be
* checked for its prefix. If the prefix matches a key in the map of delegates,
* then the corresponding delegate {@link LineTokenizer} will be used. Prefixes
* are sorted starting with the most specific, and the first match always
* succeeds.
* patterns to delegate {@link LineTokenizer}s. Each line tokenizied will be
* checked to see if it matches a pattern. If the line matches a key in the map
* of delegates, then the corresponding delegate {@link LineTokenizer} will be
* used. Patterns are sorted starting with the most specific, and the first
* match succeeds.
*
* @author Ben Hale
* @author Dan Garrette
* @author Dave Syer
*/
public class PrefixMatchingCompositeLineTokenizer implements LineTokenizer, InitializingBean {
public class PatternMatchingCompositeLineTokenizer implements LineTokenizer, InitializingBean {
private PatternMatcher<LineTokenizer> tokenizers = null;
@@ -62,14 +61,6 @@ public class PrefixMatchingCompositeLineTokenizer implements LineTokenizer, Init
public void setTokenizers(Map<String, LineTokenizer> tokenizers) {
Assert.isTrue(!tokenizers.isEmpty(), "The 'tokenizers' property must be non-empty");
LinkedHashMap<String, LineTokenizer> map = new LinkedHashMap<String, LineTokenizer>();
for (String key : tokenizers.keySet()) {
LineTokenizer value = tokenizers.get(key);
if (!key.endsWith("*")) {
key = key + "*";
}
map.put(key, value);
}
this.tokenizers = new PatternMatcher<LineTokenizer>(map);
this.tokenizers = new PatternMatcher<LineTokenizer>(tokenizers);
}
}

View File

@@ -34,9 +34,9 @@ import org.springframework.batch.item.file.transform.Name;
* @author Dave Syer
* @since 2.0
*/
public class PrefixMatchingCompositeLineMapperTests {
public class PatternMatchingCompositeLineMapperTests {
private PrefixMatchingCompositeLineMapper<Name> mapper = new PrefixMatchingCompositeLineMapper<Name>();
private PatternMatchingCompositeLineMapper<Name> mapper = new PatternMatchingCompositeLineMapper<Name>();
@Test(expected = IllegalArgumentException.class)
public void testNoMappers() throws Exception {
@@ -49,12 +49,12 @@ public class PrefixMatchingCompositeLineMapperTests {
@Test
public void testKeyFound() throws Exception {
Map<String, LineTokenizer> tokenizers = new HashMap<String, LineTokenizer>();
tokenizers.put("foo", new LineTokenizer() {
tokenizers.put("foo*", new LineTokenizer() {
public FieldSet tokenize(String line) {
return new DefaultFieldSet(new String[] { "a", "b" });
}
});
tokenizers.put("bar", new LineTokenizer() {
tokenizers.put("bar*", new LineTokenizer() {
public FieldSet tokenize(String line) {
return new DefaultFieldSet(new String[] { "c", "d" });
}
@@ -62,12 +62,12 @@ public class PrefixMatchingCompositeLineMapperTests {
mapper.setTokenizers(tokenizers);
Map<String, FieldSetMapper<Name>> fieldSetMappers = new HashMap<String, FieldSetMapper<Name>>();
fieldSetMappers.put("foo", new FieldSetMapper<Name>() {
fieldSetMappers.put("foo*", new FieldSetMapper<Name>() {
public Name mapFieldSet(FieldSet fs) {
return new Name(fs.readString(0), fs.readString(1), 0);
}
});
fieldSetMappers.put("bar", new FieldSetMapper<Name>() {
fieldSetMappers.put("bar*", new FieldSetMapper<Name>() {
public Name mapFieldSet(FieldSet fs) {
return new Name(fs.readString(1), fs.readString(0), 0);
}
@@ -81,12 +81,12 @@ public class PrefixMatchingCompositeLineMapperTests {
@Test(expected = IllegalStateException.class)
public void testMapperKeyNotFound() throws Exception {
Map<String, LineTokenizer> tokenizers = new HashMap<String, LineTokenizer>();
tokenizers.put("foo", new LineTokenizer() {
tokenizers.put("foo*", new LineTokenizer() {
public FieldSet tokenize(String line) {
return new DefaultFieldSet(new String[] { "a", "b" });
}
});
tokenizers.put("bar", new LineTokenizer() {
tokenizers.put("bar*", new LineTokenizer() {
public FieldSet tokenize(String line) {
return new DefaultFieldSet(new String[] { "c", "d" });
}
@@ -94,7 +94,7 @@ public class PrefixMatchingCompositeLineMapperTests {
mapper.setTokenizers(tokenizers);
Map<String, FieldSetMapper<Name>> fieldSetMappers = new HashMap<String, FieldSetMapper<Name>>();
fieldSetMappers.put("foo", new FieldSetMapper<Name>() {
fieldSetMappers.put("foo*", new FieldSetMapper<Name>() {
public Name mapFieldSet(FieldSet fs) {
return new Name(fs.readString(0), fs.readString(1), 0);
}

View File

@@ -28,10 +28,11 @@ import org.junit.Test;
/**
* @author Ben Hale
* @author Dan Garrette
* @author Dave Syer
*/
public class PrefixMatchingCompositeLineTokenizerTests {
public class PatternMatchingCompositeLineTokenizerTests {
private PrefixMatchingCompositeLineTokenizer tokenizer = new PrefixMatchingCompositeLineTokenizer();
private PatternMatchingCompositeLineTokenizer tokenizer = new PatternMatchingCompositeLineTokenizer();
@Test(expected = IllegalArgumentException.class)
public void testNoTokenizers() throws Exception {
@@ -42,7 +43,7 @@ public class PrefixMatchingCompositeLineTokenizerTests {
@Test
public void testEmptyKeyMatchesAnyLine() throws Exception {
Map<String, LineTokenizer> map = new HashMap<String, LineTokenizer>();
map.put("", new DelimitedLineTokenizer());
map.put("*", new DelimitedLineTokenizer());
map.put("foo", new LineTokenizer() {
public FieldSet tokenize(String line) {
return null;
@@ -58,12 +59,12 @@ public class PrefixMatchingCompositeLineTokenizerTests {
public void testEmptyKeyDoesNotMatchWhenAlternativeAvailable() throws Exception {
Map<String, LineTokenizer> map = new LinkedHashMap<String, LineTokenizer>();
map.put("", new LineTokenizer() {
map.put("*", new LineTokenizer() {
public FieldSet tokenize(String line) {
return null;
}
});
map.put("foo", new DelimitedLineTokenizer());
map.put("foo*", new DelimitedLineTokenizer());
tokenizer.setTokenizers(map);
tokenizer.afterPropertiesSet();
FieldSet fields = tokenizer.tokenize("foo,bar");
@@ -79,7 +80,7 @@ public class PrefixMatchingCompositeLineTokenizerTests {
@Test
public void testMatchWithPrefix() throws Exception {
tokenizer.setTokenizers(Collections.singletonMap("foo", (LineTokenizer) new LineTokenizer() {
tokenizer.setTokenizers(Collections.singletonMap("foo*", (LineTokenizer) new LineTokenizer() {
public FieldSet tokenize(String line) {
return new DefaultFieldSet(new String[] { line });
}