Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring ahead of writer validation fixes #355

Merged
merged 5 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/main/java/io/xlate/edi/internal/schema/ElementType.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Supplier;

Expand All @@ -42,27 +43,27 @@ class ElementType extends BasicType implements EDISimpleType {
final List<Version> versions;

static class Version extends VersionedProperty {
final Long minLength;
final Long maxLength;
final Map<String, String> values;
final Optional<Long> minLength;
final Optional<Long> maxLength;
final Optional<Map<String, String>> values;

Version(String minVersion, String maxVersion, Long minLength, Long maxLength, Map<String, String> values) {
super(minVersion, maxVersion);
this.minLength = minLength;
this.maxLength = maxLength;
this.values = values;
this.minLength = Optional.ofNullable(minLength);
this.maxLength = Optional.ofNullable(maxLength);
this.values = Optional.ofNullable(values);
}

public long getMinLength(ElementType defaultElement) {
return minLength != null ? minLength.longValue() : defaultElement.getMinLength();
return minLength.orElseGet(defaultElement::getMinLength);
}

public long getMaxLength(ElementType defaultElement) {
return maxLength != null ? maxLength.longValue() : defaultElement.getMaxLength();
return maxLength.orElseGet(defaultElement::getMaxLength);
}

public Map<String, String> getValues(ElementType defaultElement) {
return values != null ? values : defaultElement.getValues();
return values.orElseGet(defaultElement::getValues);
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/main/java/io/xlate/edi/internal/schema/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Supplier;

Expand All @@ -40,21 +41,21 @@ class Reference implements EDIReference {
private final String description;

static class Version extends VersionedProperty {
final Integer minOccurs;
final Integer maxOccurs;
final Optional<Integer> minOccurs;
final Optional<Integer> maxOccurs;

Version(String minVersion, String maxVersion, Integer minOccurs, Integer maxOccurs) {
super(minVersion, maxVersion);
this.minOccurs = minOccurs;
this.maxOccurs = maxOccurs;
this.minOccurs = Optional.ofNullable(minOccurs);
this.maxOccurs = Optional.ofNullable(maxOccurs);
}

public int getMinOccurs(Reference defaultElement) {
return minOccurs != null ? minOccurs.intValue() : defaultElement.getMinOccurs();
return minOccurs.orElseGet(defaultElement::getMinOccurs);
}

public int getMaxOccurs(Reference defaultElement) {
return maxOccurs != null ? maxOccurs.intValue() : defaultElement.getMaxOccurs();
return maxOccurs.orElseGet(defaultElement::getMaxOccurs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

public class StaEDIStreamLocation extends LocationView implements Location {

private boolean repeated = false;
private boolean composite = false;
private boolean repeating = false;
private int repeatCount = -1;

public StaEDIStreamLocation() {
super();
Expand All @@ -32,7 +34,9 @@ public StaEDIStreamLocation(Location source) {
@Override
public StaEDIStreamLocation copy() {
StaEDIStreamLocation copy = new StaEDIStreamLocation(this);
copy.repeated = this.repeated;
copy.composite = this.composite;
copy.repeating = this.repeating;
copy.repeatCount = this.repeatCount;
return copy;
}

Expand Down Expand Up @@ -68,57 +72,93 @@ public void incrementOffset(int value) {
this.columnNumber++;
}

public void incrementSegmentPosition(String segmentTag) {
if (this.segmentPosition < 0) {
this.segmentPosition = 1;
} else {
this.segmentPosition++;
static int initOrIncrement(int position) {
if (position < 0) {
return 1;
}
return position + 1;
}

public void incrementSegmentPosition(String segmentTag) {
this.segmentPosition = initOrIncrement(segmentPosition);
this.segmentTag = segmentTag;

clearSegmentLocations();
}

public void clearSegmentLocations() {
this.elementPosition = -1;
this.elementOccurrence = -1;
this.repeating = false;
this.repeatCount = -1;
clearComponentPosition();
}

public void incrementElementPosition() {
if (this.elementPosition < 0) {
this.elementPosition = 1;
} else {
this.elementPosition++;
}

this.elementPosition = initOrIncrement(elementPosition);
this.elementOccurrence = 1;
clearComponentPosition();
}

public void incrementElementOccurrence() {
this.elementOccurrence++;
this.elementPosition = Math.max(elementPosition, 1);
this.elementOccurrence = initOrIncrement(elementOccurrence);
clearComponentPosition();
}

public void incrementComponentPosition() {
if (this.componentPosition < 0) {
this.componentPosition = 1;
} else {
this.componentPosition++;
}
this.componentPosition = initOrIncrement(componentPosition);
}

public void clearComponentPosition() {
this.composite = false;
this.componentPosition = -1;
}

public void setRepeated(boolean repeated) {
this.repeated = repeated;
public void setComposite(boolean composite) {
this.composite = composite;
}

public boolean isRepeated() {
return repeated;
public void setRepeating(boolean repeating) {
if (repeating) {
// Encountered a repeat delimiter
if (this.repeating) {
// Previous delimiter was repeat, increment
repeatCount++;
} else {
// First repeat delimiter for this element
repeatCount = 0;
}
} else if (this.repeating) {
// Previous delimiter was repeat, this one is not. The element just completed is a repeat
repeatCount++;
} else {
// Repeat does not apply
repeatCount = -1;
}

this.repeating = repeating;
}

public void incrementElement(boolean compositeBegin) {
if (composite) {
incrementComponentPosition();
} else if (elementPosition < 0 || repeatCount == -1) {
// First element of the segment or not a repeating element
incrementElementPosition();
} else if (repeating) {
if (compositeBegin) {
// Previous element delimiter was a repeater and the first component was encountered
incrementElementOccurrence();
} else if (repeatCount == 0) {
// First element of the repeating series is in a new element position
incrementElementPosition();
} else {
incrementElementOccurrence();
}
} else if (compositeBegin) {
incrementElementPosition();
} else {
incrementElementOccurrence();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ void writeOutput(int output) throws EDIStreamException {
}

@Override
public EDIStreamWriter startInterchange() throws EDIStreamException {
public EDIStreamWriter startInterchange() {
ensureLevel(LEVEL_INITIAL);
ensureState(State.INITIAL);
level = LEVEL_INTERCHANGE;
Expand Down Expand Up @@ -526,6 +526,7 @@ public EDIStreamWriter writeEndSegment() throws EDIStreamException {
if (level > LEVEL_SEGMENT) {
validateElement(this.elementBuffer::flip, this.elementBuffer);
}
level = LEVEL_SEGMENT;
validate(validator -> validator.validateSyntax(dialect, this, this, location, false));

if (state == State.ELEMENT_DATA_BINARY) {
Expand Down Expand Up @@ -858,8 +859,13 @@ public boolean binaryData(InputStream binary) {
}

@Override
public boolean elementData(char[] text, int start, int length) {
elementHolder.set(text, start, length);
public boolean elementData(CharSequence text, boolean fromStream) {
if (level > LEVEL_ELEMENT) {
location.incrementComponentPosition();
} else {
location.incrementElementPosition();
}

dialect.elementData(elementHolder, location);

validator().ifPresent(validator -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public interface ElementDataHandler {

boolean elementData(char[] text, int start, int length);
boolean elementData(CharSequence text, boolean fromStream);

boolean binaryData(InputStream binary);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface EventHandler extends ElementDataHandler, ValidationEventHandler

boolean segmentEnd();

boolean compositeBegin(boolean isNil);
boolean compositeBegin(boolean isNil, boolean derived);

boolean compositeEnd(boolean isNil);

Expand Down
Loading