8323601: Improve LayoutPath.PathElement::toString

Reviewed-by: jvernee
This commit is contained in:
Per Minborg 2024-01-31 09:54:03 +00:00
parent f7121de4a0
commit ec56c72b51
3 changed files with 58 additions and 7 deletions

View file

@ -864,7 +864,8 @@ public sealed interface MemoryLayout
static PathElement groupElement(String name) { static PathElement groupElement(String name) {
Objects.requireNonNull(name); Objects.requireNonNull(name);
return new LayoutPath.PathElementImpl(PathKind.GROUP_ELEMENT, return new LayoutPath.PathElementImpl(PathKind.GROUP_ELEMENT,
path -> path.groupElement(name)); path -> path.groupElement(name),
"groupElement(\"" + name + "\")");
} }
/** /**
@ -879,7 +880,8 @@ public sealed interface MemoryLayout
throw new IllegalArgumentException("Index < 0"); throw new IllegalArgumentException("Index < 0");
} }
return new LayoutPath.PathElementImpl(PathKind.GROUP_ELEMENT, return new LayoutPath.PathElementImpl(PathKind.GROUP_ELEMENT,
path -> path.groupElement(index)); path -> path.groupElement(index),
"groupElement(" + index + ")");
} }
/** /**
@ -894,7 +896,8 @@ public sealed interface MemoryLayout
throw new IllegalArgumentException("Index must be positive: " + index); throw new IllegalArgumentException("Index must be positive: " + index);
} }
return new LayoutPath.PathElementImpl(PathKind.SEQUENCE_ELEMENT_INDEX, return new LayoutPath.PathElementImpl(PathKind.SEQUENCE_ELEMENT_INDEX,
path -> path.sequenceElement(index)); path -> path.sequenceElement(index),
"sequenceElement(" + index + ")");
} }
/** /**
@ -927,7 +930,8 @@ public sealed interface MemoryLayout
throw new IllegalArgumentException("Step must be != 0: " + step); throw new IllegalArgumentException("Step must be != 0: " + step);
} }
return new LayoutPath.PathElementImpl(PathKind.SEQUENCE_RANGE, return new LayoutPath.PathElementImpl(PathKind.SEQUENCE_RANGE,
path -> path.sequenceElement(start, step)); path -> path.sequenceElement(start, step),
"sequenceElement(" + start + ", " + step + ")");
} }
/** /**
@ -940,7 +944,8 @@ public sealed interface MemoryLayout
*/ */
static PathElement sequenceElement() { static PathElement sequenceElement() {
return new LayoutPath.PathElementImpl(PathKind.SEQUENCE_ELEMENT, return new LayoutPath.PathElementImpl(PathKind.SEQUENCE_ELEMENT,
LayoutPath::sequenceElement); LayoutPath::sequenceElement,
"sequenceElement()");
} }
/** /**
@ -949,7 +954,8 @@ public sealed interface MemoryLayout
*/ */
static PathElement dereferenceElement() { static PathElement dereferenceElement() {
return new LayoutPath.PathElementImpl(PathKind.DEREF_ELEMENT, return new LayoutPath.PathElementImpl(PathKind.DEREF_ELEMENT,
LayoutPath::derefElement); LayoutPath::derefElement,
"dereferenceElement()");
} }
} }

View file

@ -389,10 +389,14 @@ public class LayoutPath {
final PathKind kind; final PathKind kind;
final UnaryOperator<LayoutPath> pathOp; final UnaryOperator<LayoutPath> pathOp;
final String stringRepresentation;
public PathElementImpl(PathKind kind, UnaryOperator<LayoutPath> pathOp) { public PathElementImpl(PathKind kind,
UnaryOperator<LayoutPath> pathOp,
String stringRepresentation) {
this.kind = kind; this.kind = kind;
this.pathOp = pathOp; this.pathOp = pathOp;
this.stringRepresentation = stringRepresentation;
} }
@Override @Override
@ -403,5 +407,10 @@ public class LayoutPath {
public PathKind kind() { public PathKind kind() {
return kind; return kind;
} }
@Override
public String toString() {
return stringRepresentation;
}
} }
} }

View file

@ -311,6 +311,42 @@ public class TestLayoutPaths {
assertEquals(actualByteOffset, expectedByteOffset); assertEquals(actualByteOffset, expectedByteOffset);
} }
@Test
public void testGroupElementIndexToString() {
PathElement e = PathElement.groupElement(2);
assertEquals(e.toString(), "groupElement(2)");
}
@Test
public void testGroupElementNameToString() {
PathElement e = PathElement.groupElement("x");
assertEquals(e.toString(), "groupElement(\"x\")");
}
@Test
public void testSequenceElementToString() {
PathElement e = PathElement.sequenceElement();
assertEquals(e.toString(), "sequenceElement()");
}
@Test
public void testSequenceElementIndexToString() {
PathElement e = PathElement.sequenceElement(2);
assertEquals(e.toString(), "sequenceElement(2)");
}
@Test
public void testSequenceElementRangeToString() {
PathElement e = PathElement.sequenceElement(2, 4);
assertEquals(e.toString(), "sequenceElement(2, 4)");
}
@Test
public void testDerefereceElementToString() {
PathElement e = PathElement.dereferenceElement();
assertEquals(e.toString(), "dereferenceElement()");
}
@DataProvider @DataProvider
public static Object[][] testLayouts() { public static Object[][] testLayouts() {
List<Object[]> testCases = new ArrayList<>(); List<Object[]> testCases = new ArrayList<>();