BATCH-700:FixedLengthTokenizer didn't support single value ranges when calculating the maximum line length.

This commit is contained in:
lucasward
2008-07-11 04:14:53 +00:00
parent a61b725bc2
commit 402bb81c40
3 changed files with 28 additions and 7 deletions

View File

@@ -65,10 +65,6 @@ public abstract class AbstractLineTokenizer implements LineTokenizer {
* @return the resulting tokens
*/
public FieldSet tokenize(String line) {
// if (line == null || line.length()==0) {
// return new DefaultFieldSet(new String[0]);
// }
if(line == null){
line = "";

View File

@@ -47,17 +47,30 @@ public class FixedLengthTokenizer extends AbstractLineTokenizer {
calculateMaxRange(ranges);
}
/*
* Calculate the highest value within an array of ranges. The ranges aren't
* necessarily in order. For example: "5-10, 1-4,11-15". Furthermore, there
* isn't always a min and max, such as: "1,4-20, 22"
*/
private void calculateMaxRange(Range[] ranges){
if(ranges == null || ranges.length == 0){
maxRange = 0;
return;
}
maxRange = ranges[0].getMax();
maxRange = ranges[0].getMin();
for(int i = 0; i < ranges.length; i++){
if(ranges[i].getMax() > maxRange){
maxRange = ranges[i].getMax();
int upperBound;
if(ranges[i].hasMaxValue()){
upperBound = ranges[i].getMax();
}
else{
upperBound = ranges[i].getMin();
}
if(upperBound > maxRange){
maxRange = upperBound;
}
}
}

View File

@@ -122,6 +122,18 @@ public class FixedLengthTokenizerTests extends TestCase {
assertEquals(line.substring(15, 25).trim(), tokens.readString(2));
assertEquals(line.substring(25).trim(), tokens.readString(3));
}
public void testFillerAtEnd() throws Exception {
tokenizer.setColumns(new Range[] {new Range(1,5),new Range(6,15),new Range(16,25),new Range(26,27),new Range(34)});
// test another type of record
line = "H2 123456 12345 12-123456";
FieldSet tokens = tokenizer.tokenize(line);
assertEquals(5, tokens.getFieldCount());
assertEquals(line.substring(0, 5).trim(), tokens.readString(0));
assertEquals(line.substring(5, 15).trim(), tokens.readString(1));
assertEquals(line.substring(15, 25).trim(), tokens.readString(2));
assertEquals(line.substring(25, 27).trim(), tokens.readString(3));
}
public void testTokenizerInvalidSetup() {
tokenizer.setNames(new String[] {"a", "b"});