Modernize cds-access-client 80/103580/4
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 4 Dec 2022 20:46:03 +0000 (21:46 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 18 Dec 2022 11:07:55 +0000 (12:07 +0100)
Use instanceof patterns to reduce the number of casts.

Change-Id: If0f3d0d47e9a68b24372b79e93aa478ec32cf9b0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/ClientActorBehavior.java
opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/RecoveringClientActorBehavior.java
opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/SavingClientActorBehavior.java

index 40f07f137a59712fe16da23b67d5e3ea4cdffbc2..6856c033220822ccb48d1ad456ae0b0902462f34 100644 (file)
@@ -148,12 +148,11 @@ public abstract class ClientActorBehavior<T extends BackendInfo> extends
             return ((InternalCommand<T>) command).execute(this);
         }
 
-        if (command instanceof SuccessEnvelope) {
-            return onRequestSuccess((SuccessEnvelope) command);
+        if (command instanceof SuccessEnvelope successEnvelope) {
+            return onRequestSuccess(successEnvelope);
         }
-
-        if (command instanceof FailureEnvelope) {
-            return internalOnRequestFailure((FailureEnvelope) command);
+        if (command instanceof FailureEnvelope failureEnvelope) {
+            return internalOnRequestFailure(failureEnvelope);
         }
 
         if (MessageAssembler.isHandledMessage(command)) {
@@ -170,10 +169,10 @@ public abstract class ClientActorBehavior<T extends BackendInfo> extends
     }
 
     private static long extractCookie(final Identifier id) {
-        if (id instanceof TransactionIdentifier) {
-            return ((TransactionIdentifier) id).getHistoryId().getCookie();
-        } else if (id instanceof LocalHistoryIdentifier) {
-            return ((LocalHistoryIdentifier) id).getCookie();
+        if (id instanceof TransactionIdentifier transactionId) {
+            return transactionId.getHistoryId().getCookie();
+        } else if (id instanceof LocalHistoryIdentifier historyId) {
+            return historyId.getCookie();
         } else {
             throw new IllegalArgumentException("Unhandled identifier " + id);
         }
@@ -323,8 +322,8 @@ public abstract class ClientActorBehavior<T extends BackendInfo> extends
 
             LOG.error("{}: failed to resolve shard {}", persistenceId(), shard, failure);
             final RequestException cause;
-            if (failure instanceof RequestException) {
-                cause = (RequestException) failure;
+            if (failure instanceof RequestException requestException) {
+                cause = requestException;
             } else {
                 cause = new RuntimeRequestException("Failed to resolve shard " + shard, failure);
             }
index f40deab30d5bed8bbf1a7eed8da3e76653ce27bf..b44d54921d9de38b898a06633f6c5dec25b2c36e 100644 (file)
@@ -63,8 +63,8 @@ final class RecoveringClientActorBehavior extends AbstractClientActorBehavior<In
             LOG.debug("{}: persisting new identifier {}", persistenceId(), nextId);
             context().saveSnapshot(nextId);
             return new SavingClientActorBehavior(context(), nextId);
-        } else if (recover instanceof SnapshotOffer) {
-            lastId = (ClientIdentifier) ((SnapshotOffer)recover).snapshot();
+        } else if (recover instanceof SnapshotOffer snapshotOffer) {
+            lastId = (ClientIdentifier) snapshotOffer.snapshot();
             LOG.debug("{}: recovered identifier {}", persistenceId(), lastId);
         } else {
             LOG.warn("{}: ignoring recovery message {}", persistenceId(), recover);
index 42823a19ab04400f70472e71e75b974e5f5a6840..feca185812c7652c4ef84bb74d204723e9864ff4 100644 (file)
@@ -29,26 +29,24 @@ final class SavingClientActorBehavior extends RecoveredClientActorBehavior<Initi
 
     SavingClientActorBehavior(final InitialClientActorContext context, final ClientIdentifier nextId) {
         super(context);
-        this.myId = requireNonNull(nextId);
+        myId = requireNonNull(nextId);
     }
 
     @Override
     AbstractClientActorBehavior<?> onReceiveCommand(final Object command) {
-        if (command instanceof SaveSnapshotFailure) {
-            LOG.error("{}: failed to persist state", persistenceId(), ((SaveSnapshotFailure) command).cause());
+        if (command instanceof SaveSnapshotFailure saveFailure) {
+            LOG.error("{}: failed to persist state", persistenceId(), saveFailure.cause());
             return null;
-        } else if (command instanceof SaveSnapshotSuccess) {
-            LOG.debug("{}: got command: {}", persistenceId(), command);
-            SaveSnapshotSuccess saved = (SaveSnapshotSuccess)command;
+        } else if (command instanceof SaveSnapshotSuccess saved) {
+            LOG.debug("{}: got command: {}", persistenceId(), saved);
             context().deleteSnapshots(new SnapshotSelectionCriteria(scala.Long.MaxValue(),
                     saved.metadata().timestamp() - 1, 0L, 0L));
             return this;
-        } else if (command instanceof DeleteSnapshotsSuccess) {
-            LOG.debug("{}: got command: {}", persistenceId(), command);
-        } else if (command instanceof DeleteSnapshotsFailure) {
+        } else if (command instanceof DeleteSnapshotsSuccess deleteSuccess) {
+            LOG.debug("{}: got command: {}", persistenceId(), deleteSuccess);
+        } else if (command instanceof DeleteSnapshotsFailure deleteFailure) {
             // Not treating this as a fatal error.
-            LOG.warn("{}: failed to delete prior snapshots", persistenceId(),
-                    ((DeleteSnapshotsFailure) command).cause());
+            LOG.warn("{}: failed to delete prior snapshots", persistenceId(), deleteFailure.cause());
         } else {
             LOG.debug("{}: stashing command {}", persistenceId(), command);
             context().stash();