Use instanceof patterns in BGPPeer 25/105625/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 11 Apr 2023 17:15:54 +0000 (19:15 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 23 Apr 2023 15:55:00 +0000 (17:55 +0200)
Reduce the risk of mismatched casts by using an explicit pattern.

Change-Id: I238585f6dc29456dc56853a511533cbbd8b8816e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 38165f9ef743f7c8df7a036349850f31fd89bc32)

bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeer.java

index 6a84134ea323276ebf30026710fe6e6b2353b31c..6a0996bd7eb07b0b5fc0b19ff6b29a7076de01c9 100644 (file)
@@ -258,10 +258,10 @@ public class BGPPeer extends AbstractPeer implements BGPSessionListener {
 
     @Override
     public void onMessage(final BGPSession session, final Notification<?> msg) throws BGPDocumentedException {
-        if (msg instanceof Update) {
-            onUpdateMessage((Update) msg);
-        } else if (msg instanceof RouteRefresh) {
-            onRouteRefreshMessage((RouteRefresh) msg);
+        if (msg instanceof Update update) {
+            onUpdateMessage(update);
+        } else if (msg instanceof RouteRefresh routeRefresh) {
+            onRouteRefreshMessage(routeRefresh);
         } else {
             LOG.info("Ignoring unhandled message class {}", msg.getClass());
         }
@@ -377,8 +377,8 @@ public class BGPPeer extends AbstractPeer implements BGPSessionListener {
             }
         });
 
-        if (currentSession instanceof BGPSessionStateProvider) {
-            ((BGPSessionStateProvider) currentSession).registerMessagesCounter(this);
+        if (currentSession instanceof BGPSessionStateProvider stateProvider) {
+            stateProvider.registerMessagesCounter(this);
         }
         final GracefulRestartCapability advertisedGracefulRestartCapability =
                 session.getAdvertisedGracefulRestartCapability();
@@ -512,8 +512,8 @@ public class BGPPeer extends AbstractPeer implements BGPSessionListener {
         final RIBSupport<?, ?> ribSupport = rib.getRibSupportContext().getRIBSupport(key);
 
         // not particularly nice
-        if (ribSupport != null && currentSession instanceof BGPSessionImpl) {
-            final ChannelOutputLimiter limiter = ((BGPSessionImpl) currentSession).getLimiter();
+        if (ribSupport != null && currentSession instanceof BGPSessionImpl bgpSession) {
+            final ChannelOutputLimiter limiter = bgpSession.getLimiter();
             final AdjRibOutListener adjRibOut = AdjRibOutListener.create(peerId, key,
                     rib.getYangRibId(), rib.getCodecsRegistry(), ribSupport,
                     rib.getService(), limiter, mpSupport);
@@ -702,24 +702,24 @@ public class BGPPeer extends AbstractPeer implements BGPSessionListener {
 
     @Override
     public synchronized BGPSessionState getBGPSessionState() {
-        if (currentSession instanceof BGPSessionStateProvider) {
-            return ((BGPSessionStateProvider) currentSession).getBGPSessionState();
+        if (currentSession instanceof BGPSessionStateProvider stateProvider) {
+            return stateProvider.getBGPSessionState();
         }
         return null;
     }
 
     @Override
     public synchronized BGPTimersState getBGPTimersState() {
-        if (currentSession instanceof BGPSessionStateProvider) {
-            return ((BGPSessionStateProvider) currentSession).getBGPTimersState();
+        if (currentSession instanceof BGPSessionStateProvider stateProvider) {
+            return stateProvider.getBGPTimersState();
         }
         return null;
     }
 
     @Override
     public synchronized BGPTransportState getBGPTransportState() {
-        if (currentSession instanceof BGPSessionStateProvider) {
-            return ((BGPSessionStateProvider) currentSession).getBGPTransportState();
+        if (currentSession instanceof BGPSessionStateProvider stateProvider) {
+            return stateProvider.getBGPTransportState();
         }
         return null;
     }