Skip to content

Commit

Permalink
refactor: simplify SwiftFieldReader
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon committed Nov 15, 2020
1 parent d6cd0c7 commit ad82770
Showing 1 changed file with 23 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,44 @@ public class SwiftFieldReader {

private final static Set<FieldLineType> FIELD_START_LINE_TYPE_SET = ImmutableSet.of(FieldLineType.FIELD, FieldLineType.SEPARATOR);

private int currentFieldLineNumber = 0;

private final LineNumberReader lineReader;

private FieldLine currentFieldLine = null;

private FieldLine nextFieldLine = null;


public SwiftFieldReader(Reader textReader) {
this.lineReader = new LineNumberReader(textReader);
}

public int getFieldLineNumber() {
return currentFieldLineNumber;
return lineReader.getLineNumber() - 1;
}

public GeneralField readField() throws FieldParseException {
// field fields
String tag = null;
StringBuilder contentBuilder = new StringBuilder();

try {
if (currentFieldLine == null) {
nextFieldLine = readFieldLine(lineReader);
currentFieldLine = readFieldLine(lineReader);
}
if (currentFieldLine == null) {
return null;
}

GeneralField field = null;

// field fields
String tag = null;
StringBuilder contentBuilder = new StringBuilder();

Set<FieldLineType> nextValidFieldLineTypeSet = FIELD_START_LINE_TYPE_SET;

while (field == null && nextFieldLine != null) {

ensureValidNextLine(nextFieldLine, nextValidFieldLineTypeSet, lineReader);

currentFieldLine = nextFieldLine;
currentFieldLineNumber = lineReader.getLineNumber() - 1;
nextFieldLine = readFieldLine(lineReader);
while (currentFieldLine != null) {
ensureValidNextLine(currentFieldLine, nextValidFieldLineTypeSet, lineReader);

switch (currentFieldLine.getType()) {
case FIELD: {
Matcher fieldMatcher = FIELD_STRUCTURE_PATTERN.matcher(currentFieldLine.getContent());
if (!fieldMatcher.matches()) {
throw new FieldParseException("Parse error: " + currentFieldLine.getType().name() + " did not match " + FIELD_STRUCTURE_PATTERN.pattern(), currentFieldLineNumber);
throw new FieldParseException("Parse error: " + currentFieldLine.getType().name() + " did not match " + FIELD_STRUCTURE_PATTERN.pattern(), getFieldLineNumber());
}

// start of a new field
currentFieldLineNumber = lineReader.getLineNumber();
tag = fieldMatcher.group("tag");
contentBuilder.append(fieldMatcher.group("content"));
nextValidFieldLineTypeSet = ImmutableSet.of(FieldLineType.FIELD, FieldLineType.FIELD_CONTINUATION, FieldLineType.SEPARATOR);
Expand All @@ -87,23 +77,23 @@ public GeneralField readField() throws FieldParseException {
break;
}
default:
throw new FieldParseException("Bug: Missing handling for line type " + currentFieldLine.getType().name(), currentFieldLineNumber);
throw new FieldParseException("Bug: Missing handling for line type " + currentFieldLine.getType().name(), getFieldLineNumber());
}

// finish field
if (nextFieldLine == null || FIELD_START_LINE_TYPE_SET.contains(nextFieldLine.getType())) {
field = new GeneralField(
tag,
contentBuilder.toString()
);
currentFieldLine = readFieldLine(lineReader);
if (currentFieldLine == null || FIELD_START_LINE_TYPE_SET.contains(currentFieldLine.getType())) {
break;
}
}

return field;
return new GeneralField(
tag,
contentBuilder.toString()
);
} catch (FieldParseException e) {
throw e;
} catch (Exception e) {
if (e instanceof FieldParseException)
throw (FieldParseException) e;
throw new FieldParseException(e.getMessage(), currentFieldLineNumber, e);
throw new FieldParseException(e.getMessage(), getFieldLineNumber(), e);
}
}

Expand All @@ -114,7 +104,6 @@ private void ensureValidNextLine(FieldLine nextFieldLine, Set<FieldLineType> exp
}
}


private FieldLineType determineMessageLineType(String messageLine) {
Preconditions.checkArgument(messageLine != null && !messageLine.isEmpty(), "messageLine can't be null or empty");

Expand Down Expand Up @@ -160,6 +149,4 @@ private enum FieldLineType {
FIELD_CONTINUATION,
SEPARATOR
}


}

0 comments on commit ad82770

Please sign in to comment.