Skip to content

Commit

Permalink
Resolve several issues reported by Sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeEdgar committed Jul 7, 2021
1 parent 019fef6 commit 3e90ce9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
21 changes: 11 additions & 10 deletions src/main/java/io/xlate/edi/internal/schema/ElementPosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
public class ElementPosition implements EDIElementPosition {

private static final String TOSTRING_FORMAT = "%d.%d";
private final int elementPosition;
private final int componentPosition;

final int elementIndex;
final int componentIndex;

public ElementPosition(int elementPosition, int componentPosition) {
super();
this.elementPosition = elementPosition;
this.componentPosition = componentPosition;
this.elementIndex = elementPosition;
this.componentIndex = componentPosition;
}

@Override
Expand All @@ -28,28 +29,28 @@ public boolean equals(Object o) {

ElementPosition other = (ElementPosition) o;

return Objects.equals(elementPosition, other.elementPosition) &&
Objects.equals(componentPosition, other.componentPosition);
return Objects.equals(elementIndex, other.elementIndex) &&
Objects.equals(componentIndex, other.componentIndex);
}

@Override
public int hashCode() {
return Objects.hash(elementPosition, componentPosition);
return Objects.hash(elementIndex, componentIndex);
}

@Override
public String toString() {
return String.format(TOSTRING_FORMAT, elementPosition, componentPosition);
return String.format(TOSTRING_FORMAT, elementIndex, componentIndex);
}

@Override
public int getElementPosition() {
return elementPosition;
return elementIndex;
}

@Override
public int getComponentPosition() {
return componentPosition > 0 ? componentPosition : -1;
return componentIndex > 0 ? componentIndex : -1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
public class DiscriminatorImpl implements Discriminator {

private static final String TOSTRING_FORMAT = "position: %s, values: %s";
private final EDIElementPosition position;
private final Set<String> valueSet;
final EDIElementPosition position;
final Set<String> valueSet;

public DiscriminatorImpl(EDIElementPosition position, Set<String> valueSet) {
this.position = position;
Expand All @@ -28,7 +28,7 @@ public boolean equals(Object o) {
}

DiscriminatorImpl other = (DiscriminatorImpl) o;
return Objects.equals(position, other.position) &&
return Objects.equals(position, other.position) &&
Objects.equals(valueSet, other.valueSet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,14 @@ public boolean elementData(char[] text, int start, int length) {
typeReference = null;
}

if (derivedComposite && elementHolder.getText() != null/* Not an empty composite */) {
/*
* The first component of a composite was the only element received
* for the composite. It was found to be a composite via the schema
* and the composite begin/end events must be generated.
**/
final boolean componentReceivedAsSimple = derivedComposite && text != null;

if (componentReceivedAsSimple) {
this.compositeBegin(elementHolder.length() == 0);
location.incrementComponentPosition();
}
Expand All @@ -394,7 +401,7 @@ public boolean elementData(char[] text, int start, int length) {
}
}

if (derivedComposite && text != null /* Not an empty composite */) {
if (componentReceivedAsSimple) {
this.compositeEnd(length == 0);
location.clearComponentPosition();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import io.xlate.edi.internal.schema.ElementPosition;
import io.xlate.edi.schema.EDIElementPosition;
import io.xlate.edi.schema.implementation.Discriminator;

class DiscriminatorImplTest {

Expand All @@ -22,7 +21,7 @@ void setUp() throws Exception {
target = new DiscriminatorImpl(position(1, 2), Collections.singleton("50"));
}

EDIElementPosition position(int e, int c) {
ElementPosition position(int e, int c) {
return new ElementPosition(e, c);
}

Expand All @@ -34,12 +33,14 @@ void testHashCode() {

@Test
void testEquals_Same() {
assertEquals(target.position, target.position);
assertEquals(target, target);
}

@Test
void testEquals_Identical() {
Discriminator identical = new DiscriminatorImpl(position(1, 2), Collections.singleton("50"));
DiscriminatorImpl identical = new DiscriminatorImpl(position(1, 2), Collections.singleton("50"));
assertEquals(target.position, identical.position);
assertEquals(target, identical);
}

Expand All @@ -50,6 +51,23 @@ void testEquals_NotInstance() {
}
}

@Test
void testEquals_PositionNotInstance() {
DiscriminatorImpl actual = new DiscriminatorImpl(new EDIElementPosition() {
@Override
public int getComponentPosition() {
return 1;
}

@Override
public int getElementPosition() {
return 2;
}
}, Collections.singleton("50"));

assertNotEquals(target, actual);
}

@Test
void testEquals_Different() {
assertNotEquals(new DiscriminatorImpl(position(1, 2), Collections.singleton("60")), target);
Expand Down

0 comments on commit 3e90ce9

Please sign in to comment.