Use a switch expressions for enumeration dispatch 07/103207/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 10 Nov 2022 22:05:17 +0000 (23:05 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 10 Nov 2022 22:12:59 +0000 (23:12 +0100)
Using switch expressions allows us to be exhaustive and explicit about
the values handled.

Change-Id: I83c2ccc4c4def117c6f1c6d4d745e10595e59cba
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/ContentParam.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/InsertParam.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/WithDefaultsParam.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java

index 35a9248af9f98650c0234f40c3c838db2cd0f326..ba55a9b8a697fc9ee6b7538c9a290ff5cc7f4401 100644 (file)
@@ -55,16 +55,12 @@ public enum ContentParam implements RestconfQueryParam<ContentParam> {
     }
 
     public static @NonNull ContentParam forUriValue(final String uriValue) {
-        switch (uriValue) {
-            case "all":
-                return ALL;
-            case "config":
-                return CONFIG;
-            case "nonconfig":
-                return NONCONFIG;
-            default:
-                throw new IllegalArgumentException(
-                    "Value can be 'all', 'config' or 'nonconfig', not '" + uriValue + "'");
-        }
+        return switch (uriValue) {
+            case "all" -> ALL;
+            case "config" -> CONFIG;
+            case "nonconfig" -> NONCONFIG;
+            default -> throw new IllegalArgumentException(
+                "Value can be 'all', 'config' or 'nonconfig', not '" + uriValue + "'");
+        };
     }
 }
index 6594c6b472b5c4f77dce5f8b1126c0a2f68523a9..521b030b655f3578be7c934ddc2edad740dee220 100644 (file)
@@ -59,18 +59,13 @@ public enum InsertParam implements RestconfQueryParam<InsertParam> {
     }
 
     public static @NonNull InsertParam forUriValue(final String uriValue) {
-        switch (uriValue) {
-            case "after":
-                return AFTER;
-            case "before":
-                return BEFORE;
-            case "first":
-                return FIRST;
-            case "last":
-                return LAST;
-            default:
-                throw new IllegalArgumentException(
-                    "Value can be 'after', 'before', 'first' or 'last', not '" + uriValue + "'");
-        }
+        return switch (uriValue) {
+            case "after" -> AFTER;
+            case "before" -> BEFORE;
+            case "first" -> FIRST;
+            case "last" -> LAST;
+            default -> throw new IllegalArgumentException(
+                "Value can be 'after', 'before', 'first' or 'last', not '" + uriValue + "'");
+        };
     }
 }
\ No newline at end of file
index 6caa650c83d4e2eba8bf7969eab35d91de0786ae..4a57f87cf98c651a0fdb25802e0c6142668dc9b7 100644 (file)
@@ -62,19 +62,14 @@ public enum WithDefaultsParam implements RestconfQueryParam<WithDefaultsParam> {
     }
 
     public static @NonNull WithDefaultsParam forUriValue(final String uriValue) {
-        switch (uriValue) {
-            case "explicit":
-                return EXPLICIT;
-            case "report-all":
-                return REPORT_ALL;
-            case "report-all-tagged":
-                return REPORT_ALL_TAGGED;
-            case "trim":
-                return TRIM;
-            default:
-                throw new IllegalArgumentException(
-                    "Value can be 'explicit', 'report-all', 'report-all-tagged' or 'trim', not '" + uriValue + "'");
-        }
+        return switch (uriValue) {
+            case "explicit" -> EXPLICIT;
+            case "report-all" -> REPORT_ALL;
+            case "report-all-tagged" -> REPORT_ALL_TAGGED;
+            case "trim" -> TRIM;
+            default -> throw new IllegalArgumentException(
+                "Value can be 'explicit', 'report-all', 'report-all-tagged' or 'trim', not '" + uriValue + "'");
+        };
     }
 
     public static @NonNull URI capabilityUri() {
index 8257b6394e0cc986552805cadb9b8211ff8e2582..129f45afb8eaf0dd0ce467efb45c9b3e4cd2c06b 100644 (file)
@@ -166,21 +166,20 @@ public class RestconfDataServiceImpl implements RestconfDataService {
                     ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
         }
 
-        switch (readParams.content()) {
-            case ALL:
-            case CONFIG:
+        return switch (readParams.content()) {
+            case ALL, CONFIG -> {
                 final QName type = node.getIdentifier().getNodeType();
-                return Response.status(Status.OK)
+                yield Response.status(Status.OK)
                     .entity(NormalizedNodePayload.ofReadData(instanceIdentifier, node, queryParams))
-                    .header("ETag", '"' + type.getModule().getRevision().map(Revision::toString).orElse(null)
-                        + "-" + type.getLocalName() + '"')
+                    .header("ETag", '"' + type.getModule().getRevision().map(Revision::toString).orElse(null) + "-"
+                        + type.getLocalName() + '"')
                     .header("Last-Modified", FORMATTER.format(LocalDateTime.now(Clock.systemUTC())))
                     .build();
-            default:
-                return Response.status(Status.OK)
-                    .entity(NormalizedNodePayload.ofReadData(instanceIdentifier, node, queryParams))
-                    .build();
-        }
+            }
+            case NONCONFIG -> Response.status(Status.OK)
+                .entity(NormalizedNodePayload.ofReadData(instanceIdentifier, node, queryParams))
+                .build();
+        };
     }
 
     private void createAllYangNotificationStreams(final EffectiveModelContext schemaContext, final UriInfo uriInfo) {
index 6923a457cc04873a2e105c40ed321d1d3fdde0fd..520dc1e5ae64da4a997886b8dfdc876bc1a223f0 100644 (file)
@@ -19,12 +19,9 @@ import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.nb.rfc8040.ContentParam;
 import org.opendaylight.restconf.nb.rfc8040.WithDefaultsParam;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
-import org.opendaylight.yangtools.yang.common.ErrorTag;
-import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -92,21 +89,14 @@ public final class ReadDataTransactionUtil {
                                                     final @NonNull RestconfStrategy strategy,
                                                     final WithDefaultsParam withDefa,
                                                     final EffectiveModelContext ctx) {
-        // FIXME: use a switch expression when they are available, removing source of RestconfDocumentedException
-        switch (content) {
-            case ALL:
-                return readAllData(strategy, path, withDefa, ctx);
-            case CONFIG:
-                final NormalizedNode read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path);
-                return withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
-            case NONCONFIG:
-                return readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path);
-            default:
-                throw new RestconfDocumentedException(
-                        new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
-                                "Invalid content parameter: " + content.paramValue(), null,
-                                "The content parameter value must be either config, nonconfig or all (default)"));
-        }
+        return switch (content) {
+            case ALL -> readAllData(strategy, path, withDefa, ctx);
+            case CONFIG -> {
+                final var read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path);
+                yield withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
+            }
+            case NONCONFIG -> readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path);
+        };
     }
 
     /**
@@ -125,37 +115,24 @@ public final class ReadDataTransactionUtil {
             final @NonNull YangInstanceIdentifier path, final @NonNull RestconfStrategy strategy,
             final @Nullable WithDefaultsParam withDefa, @NonNull final EffectiveModelContext ctx,
             final @NonNull List<YangInstanceIdentifier> fields) {
-        // FIXME: use a switch expression when they are available, removing source of RestconfDocumentedException
-        switch (content) {
-            case ALL:
-                return readAllData(strategy, path, withDefa, ctx, fields);
-            case CONFIG:
-                final NormalizedNode read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path,
-                    fields);
-                return withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
-            case NONCONFIG:
-                return readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path, fields);
-            default:
-                throw new RestconfDocumentedException(new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
-                        "Invalid content parameter: " + content.paramValue(), null,
-                        "The content parameter value must be either config, nonconfig or all (default)"));
-        }
+        return switch (content) {
+            case ALL -> readAllData(strategy, path, withDefa, ctx, fields);
+            case CONFIG -> {
+                final var read = readDataViaTransaction(strategy, LogicalDatastoreType.CONFIGURATION, path, fields);
+                yield withDefa == null ? read : prepareDataByParamWithDef(read, path, withDefa, ctx);
+            }
+            case NONCONFIG -> readDataViaTransaction(strategy, LogicalDatastoreType.OPERATIONAL, path, fields);
+        };
     }
 
     private static NormalizedNode prepareDataByParamWithDef(final NormalizedNode result,
             final YangInstanceIdentifier path, final WithDefaultsParam withDefa, final EffectiveModelContext ctx) {
-        boolean trim;
-        switch (withDefa) {
-            case TRIM:
-                trim = true;
-                break;
-            case EXPLICIT:
-                trim = false;
-                break;
-            default:
-                throw new RestconfDocumentedException("Unsupported with-defaults value " + withDefa.paramValue());
-        }
-
+        final boolean trim = switch (withDefa) {
+            case TRIM -> true;
+            case EXPLICIT -> false;
+            case REPORT_ALL, REPORT_ALL_TAGGED -> throw new RestconfDocumentedException(
+                "Unsupported with-defaults value " + withDefa.paramValue());
+        };
         final DataSchemaContextTree baseSchemaCtxTree = DataSchemaContextTree.from(ctx);
         final DataSchemaNode baseSchemaNode = baseSchemaCtxTree.findChild(path).orElseThrow().getDataSchemaNode();
         if (result instanceof ContainerNode) {