Use HexFormat instead of custom code 24/106824/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 5 Jul 2023 20:29:51 +0000 (22:29 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 5 Jul 2023 20:42:20 +0000 (22:42 +0200)
JDK17+ ships with HexFormat, which renders some of our code superfluous.
Migrate over while retaining compatibility.

Change-Id: I608ab71ad0406974a56d60c63de12a198a85d922
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
protocol/restconf-api/src/main/java/org/opendaylight/restconf/api/ApiPathParser.java

index 842bac31e5a82b950d538d87c38edab8132a6559..ce6f68aad4b5016ab52f43c3a95b10a855eedcf5 100644 (file)
@@ -14,6 +14,7 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableList.Builder;
 import java.text.ParseException;
+import java.util.HexFormat;
 import java.util.function.Supplier;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
@@ -307,22 +308,13 @@ class ApiPathParser {
         return (byte) (parseHex(str, offset + 1) << 4 | parseHex(str, offset + 2));
     }
 
-    // FIXME: Replace with HexFormat.fromHexDigit(str.charAt(offset)) when we have JDK17+
+    @SuppressWarnings("checkstyle:AvoidHidingCauseException")
     private static int parseHex(final String str, final int offset) throws ParseException {
-        final char ch = str.charAt(offset);
-        if (ch >= '0' && ch <= '9') {
-            return ch - '0';
-        }
-
-        final int zero;
-        if (ch >= 'a' && ch <= 'f') {
-            zero = 'a';
-        } else if (ch >= 'A' && ch <= 'F') {
-            zero = 'A';
-        } else {
-            throw new ParseException("Invalid escape character '" + ch + "'", offset);
+        try {
+            return HexFormat.fromHexDigit(str.charAt(offset));
+        } catch (NumberFormatException e) {
+            // There is no way to preserve the cause which CheckStyle would recognize
+            throw new ParseException(e.getMessage(), offset);
         }
-
-        return ch - zero + 10;
     }
 }