Use instanceof patterns 41/102041/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Aug 2022 20:04:26 +0000 (22:04 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Aug 2022 20:05:30 +0000 (22:05 +0200)
We can simplify the code a bit by using pattern matching.

Change-Id: I08d9239f056ec46d4fe796ea07cc889e0298f003
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/util/src/main/java/org/opendaylight/yangtools/util/ImmutableOffsetMap.java

index bba914f7ea42f4c4211b877726e8db7ffff56f03..ae8182ea2a9690d75b63a5e80423fc2ba03ef27d 100644 (file)
@@ -186,8 +186,8 @@ public abstract class ImmutableOffsetMap<K, V> implements UnmodifiableMapPhase<K
         }
 
         // Familiar and efficient to copy
-        if (map instanceof MutableOffsetMap) {
-            return ((MutableOffsetMap<K, V>) map).toUnmodifiableMap();
+        if (map instanceof MutableOffsetMap<K, V> mop) {
+            return mop.toUnmodifiableMap();
         }
 
         if (map.isEmpty()) {
@@ -228,13 +228,11 @@ public abstract class ImmutableOffsetMap<K, V> implements UnmodifiableMapPhase<K
         if (obj == this) {
             return true;
         }
-        if (!(obj instanceof Map)) {
+        if (!(obj instanceof Map<?, ?> other)) {
             return false;
         }
 
-        if (obj instanceof ImmutableOffsetMap) {
-            final ImmutableOffsetMap<?, ?> om = (ImmutableOffsetMap<?, ?>) obj;
-
+        if (obj instanceof ImmutableOffsetMap<?, ?> om) {
             // If the offset match, the arrays have to match, too
             if (offsets.equals(om.offsets)) {
                 return Arrays.deepEquals(objects, om.objects);
@@ -244,8 +242,6 @@ public abstract class ImmutableOffsetMap<K, V> implements UnmodifiableMapPhase<K
             return obj.equals(this);
         }
 
-        final Map<?, ?> other = (Map<?, ?>)obj;
-
         // Size and key sets have to match
         if (size() != other.size() || !keySet().equals(other.keySet())) {
             return false;