From: Robert Varga Date: Thu, 10 Nov 2022 22:05:17 +0000 (+0100) Subject: Use a switch expressions for enumeration dispatch X-Git-Tag: v5.0.0~107 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;ds=sidebyside;h=8365ae3952c207c6b78ff45125706f7aae90d237;p=netconf.git Use a switch expressions for enumeration dispatch Using switch expressions allows us to be exhaustive and explicit about the values handled. Change-Id: I83c2ccc4c4def117c6f1c6d4d745e10595e59cba Signed-off-by: Robert Varga --- diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/ContentParam.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/ContentParam.java index 35a9248af9..ba55a9b8a6 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/ContentParam.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/ContentParam.java @@ -55,16 +55,12 @@ public enum ContentParam implements RestconfQueryParam { } 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 + "'"); + }; } } diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/InsertParam.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/InsertParam.java index 6594c6b472..521b030b65 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/InsertParam.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/InsertParam.java @@ -59,18 +59,13 @@ public enum InsertParam implements RestconfQueryParam { } 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 diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/WithDefaultsParam.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/WithDefaultsParam.java index 6caa650c83..4a57f87cf9 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/WithDefaultsParam.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/WithDefaultsParam.java @@ -62,19 +62,14 @@ public enum WithDefaultsParam implements RestconfQueryParam { } 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() { diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java index 8257b6394e..129f45afb8 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java @@ -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) { diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java index 6923a457cc..520dc1e5ae 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java @@ -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 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) {