Skip to content

Commit

Permalink
Refactor to use switch expression pattern matching
Browse files Browse the repository at this point in the history
now that b/330770905 is fixed.

PiperOrigin-RevId: 624250262
Change-Id: If9844a7043476b2e74ca9defea853e30b840b6bb
  • Loading branch information
cushon authored and copybara-github committed Apr 12, 2024
1 parent 6e6e862 commit 1115e52
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,12 @@ private boolean outputObjectFields(Object obj) {
}

private void outputField(Object parent, FieldInfo info) {
if (info instanceof PrimitiveInfo primitiveInfo) {
primitiveInfo.output(parent, out);
} else if (info instanceof ObjectInfo objectInfo) {
out.append(objectInfo.name()).append('=');
outputObject(objectInfo.getFieldValue(parent));
} else {
// TODO: b/297857068 - it should be possible to replace this with a pattern matching switch
// which won't require this line, but that's not yet supported.
throw new IllegalArgumentException("Unexpected FieldInfo type: " + info);
switch (info) {
case PrimitiveInfo primitiveInfo -> primitiveInfo.output(parent, out);
case ObjectInfo objectInfo -> {
out.append(objectInfo.name()).append('=');
outputObject(objectInfo.getFieldValue(parent));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,15 @@ private int outputObjectFields(Object obj, StringBuilder out) {
} else {
out.append(", ");
}
if (info instanceof PrimitiveInfo primitiveInfo) {
primitiveInfo.output(obj, out);
} else if (info instanceof ObjectInfo objectInfo) {
out.append(objectInfo.name()).append('=');
cycleOwnerIndex =
min(
cycleOwnerIndex,
outputFingerprintOrInlinedValue(objectInfo.getFieldValue(obj), out));
} else {
// TODO: b/297857068 - it should be possible to replace this with a pattern matching
// switch which won't require this line, but that's not yet supported.
throw new IllegalArgumentException("Unexpected FieldInfo type: " + info);
switch (info) {
case PrimitiveInfo primitiveInfo -> primitiveInfo.output(obj, out);
case ObjectInfo objectInfo -> {
out.append(objectInfo.name()).append('=');
cycleOwnerIndex =
min(
cycleOwnerIndex,
outputFingerprintOrInlinedValue(objectInfo.getFieldValue(obj), out));
}
}
}
return cycleOwnerIndex;
Expand Down

0 comments on commit 1115e52

Please sign in to comment.