Use pattern matching on instanceof in yang-common 98/100798/4
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 25 Apr 2022 15:21:29 +0000 (17:21 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 26 Apr 2022 07:02:35 +0000 (09:02 +0200)
This simplifies our equals() methods quite a bit.

Change-Id: If4d0fbf6bd45c58781a93f57d005b409ed9fe629
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
13 files changed:
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/BiMapYangNamespaceContext.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/CanonicalValueViolation.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Decimal64.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/ErrorTag.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Revision.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint16.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint32.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint64.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint8.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/UnresolvedQName.java
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/XMLNamespace.java

index 69f44144bddefeefbc878d768e6ce16446dd53ea..25179b0451ad84400203894a551d548480fd932a 100644 (file)
@@ -74,8 +74,7 @@ public final class BiMapYangNamespaceContext implements YangNamespaceContext, Wr
 
     @Override
     public boolean equals(final Object obj) {
-        return this == obj || obj instanceof BiMapYangNamespaceContext
-                && mapping.equals(((BiMapYangNamespaceContext) obj).mapping);
+        return this == obj || obj instanceof BiMapYangNamespaceContext other && mapping.equals(other.mapping);
     }
 
     @Override
index 6a21058faa181f70d006a658676203701e6c1f08..b766114ed79953063491824717f61399f198f13a 100644 (file)
@@ -67,7 +67,6 @@ public abstract class CanonicalValueViolation implements Immutable, Serializable
         }
 
         @Override
-        @SuppressFBWarnings("NP_NONNULL_RETURN_VIOLATION")
         @Nullable String appTag() {
             return null;
         }
@@ -140,14 +139,8 @@ public abstract class CanonicalValueViolation implements Immutable, Serializable
 
     @Override
     public final boolean equals(final @Nullable Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (!(obj instanceof CanonicalValueViolation)) {
-            return false;
-        }
-        final CanonicalValueViolation other = (CanonicalValueViolation) obj;
-        return Objects.equals(appTag(), other.appTag()) && Objects.equals(message(), other.message());
+        return this == obj || obj instanceof CanonicalValueViolation other
+            && Objects.equals(appTag(), other.appTag()) && Objects.equals(message(), other.message());
     }
 
     @Override
index c91e42f417935de228eca5f743cfbb06747c1c9b..d6b891ae0ff968cb81e9b13fb72a25c3e34211c6 100644 (file)
@@ -473,7 +473,7 @@ public class Decimal64 extends Number implements CanonicalValue<Decimal64> {
 
     @Override
     public final boolean equals(final @Nullable Object obj) {
-        return this == obj || obj instanceof Decimal64 && equalsImpl((Decimal64) obj);
+        return this == obj || obj instanceof Decimal64 other && equalsImpl(other);
     }
 
     /**
index 80de87215263a5d5556564059a84998b48afa902..90bce0263418e375e407280b5e4389ac88bb6fb7 100644 (file)
@@ -178,7 +178,7 @@ public final class ErrorTag implements Serializable {
 
     @Override
     public boolean equals(final @Nullable Object obj) {
-        return obj == this || obj instanceof ErrorTag && elementBody.equals(((ErrorTag) obj).elementBody);
+        return obj == this || obj instanceof ErrorTag other && elementBody.equals(other.elementBody);
     }
 
     @Override
index 60072a1b5955967a49c2106e38eea812802ca7e1..d836d9287d8ad86d814deafe13e0de13e44d9aef 100644 (file)
@@ -223,8 +223,8 @@ public final class QName extends AbstractQName implements Comparable<QName> {
      * @throws IOException if I/O error occurs
      */
     public static @NonNull QName readFrom(final DataInput in) throws IOException {
-        if (in instanceof QNameAwareDataInput) {
-            return ((QNameAwareDataInput) in).readQName();
+        if (in instanceof QNameAwareDataInput aware) {
+            return aware.readQName();
         }
 
         final QNameModule module = QNameModule.readFrom(in);
@@ -306,14 +306,8 @@ public final class QName extends AbstractQName implements Comparable<QName> {
      */
     @Override
     public boolean equals(final Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (!(obj instanceof QName)) {
-            return false;
-        }
-        final QName other = (QName) obj;
-        return Objects.equals(getLocalName(), other.getLocalName()) && module.equals(other.module);
+        return this == obj || obj instanceof QName other && getLocalName().equals(other.getLocalName())
+            && module.equals(other.module);
     }
 
     @Override
@@ -380,8 +374,8 @@ public final class QName extends AbstractQName implements Comparable<QName> {
 
     @Override
     public void writeTo(final DataOutput out) throws IOException {
-        if (out instanceof QNameAwareDataOutput) {
-            ((QNameAwareDataOutput) out).writeQName(this);
+        if (out instanceof QNameAwareDataOutput aware) {
+            aware.writeQName(this);
         } else {
             module.writeTo(out);
             out.writeUTF(getLocalName());
index a4f345f6e6d7ea3a95b1fe54efb8870ec924cfa5..bfaa2f23eb921a05a3b2c345fb3d9b9de272e9ab 100644 (file)
@@ -155,14 +155,8 @@ public final class QNameModule implements Comparable<QNameModule>, Immutable, Se
 
     @Override
     public boolean equals(final Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (!(obj instanceof QNameModule)) {
-            return false;
-        }
-        final QNameModule other = (QNameModule) obj;
-        return Objects.equals(revision, other.revision) && namespace.equals(other.namespace);
+        return this == obj || obj instanceof QNameModule other
+            && Objects.equals(revision, other.revision) && namespace.equals(other.namespace);
     }
 
     @Override
index a7e0d209054780f7de0e757214b9736c8be90f05..a7abcb67199274d96734833b4d993bfe69eb8640 100644 (file)
@@ -144,7 +144,7 @@ public final class Revision implements Comparable<Revision>, Immutable, Serializ
 
     @Override
     public boolean equals(final Object obj) {
-        return this == obj || obj instanceof Revision && str.equals(((Revision)obj).str);
+        return this == obj || obj instanceof Revision other && str.equals(other.str);
     }
 
     @Override
index 7671c7f15f763e8fe3c6845515d8a861645a724a..dbce6e91b741b130c0d0e65c1770232cbdca4715 100644 (file)
@@ -395,7 +395,7 @@ public class Uint16 extends Number implements CanonicalValue<Uint16> {
 
     @Override
     public final boolean equals(final @Nullable Object obj) {
-        return this == obj || obj instanceof Uint16 && value == ((Uint16)obj).value;
+        return this == obj || obj instanceof Uint16 other && value == other.value;
     }
 
     /**
index 541356fbc5d4ef5d37e2a149a8ae78ad2b991de9..e106d18ff2afd0687985d710c06ae0f72aa5e808 100644 (file)
@@ -412,7 +412,7 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
 
     @Override
     public final boolean equals(final @Nullable Object obj) {
-        return this == obj || obj instanceof Uint32 && value == ((Uint32)obj).value;
+        return this == obj || obj instanceof Uint32 other && value == other.value;
     }
 
     /**
index b2e2e4b0cf9e545b75f421d259d70d6970f5283a..68aaf80d7c178e7b3f164eefc12865e6744a853e 100644 (file)
@@ -439,7 +439,7 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
 
     @Override
     public final boolean equals(final @Nullable Object obj) {
-        return this == obj || obj instanceof Uint64 && value == ((Uint64)obj).value;
+        return this == obj || obj instanceof Uint64 other && value == other.value;
     }
 
     /**
index 48530d7a4580609caa08754efba92232bce668b4..2f218fa2baa85c7306e2a6285f6583f81899a857 100644 (file)
@@ -370,7 +370,7 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
 
     @Override
     public final boolean equals(final @Nullable Object obj) {
-        return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
+        return this == obj || obj instanceof Uint8 other && value == other.value;
     }
 
     /**
index 3865b5863ca2c20137699e75bdc961aa28e9e5a4..bdf614eb873247db377152449527f0af9ebfcaf1 100644 (file)
@@ -100,7 +100,7 @@ public abstract sealed class UnresolvedQName extends AbstractQName {
 
         @Override
         public boolean equals(final @Nullable Object obj) {
-            return this == obj || obj instanceof Qualified && getLocalName().equals(((Qualified) obj).getLocalName());
+            return this == obj || obj instanceof Qualified other && getLocalName().equals(other.getLocalName());
         }
 
         @Override
@@ -178,8 +178,7 @@ public abstract sealed class UnresolvedQName extends AbstractQName {
 
         @Override
         public boolean equals(final @Nullable Object obj) {
-            return this == obj || obj instanceof Unqualified
-                    && getLocalName().equals(((AbstractQName) obj).getLocalName());
+            return this == obj || obj instanceof Unqualified other && getLocalName().equals(other.getLocalName());
         }
 
         @Override
index f7292db2b1eca367d7b9ed14eb53fdf8ba185634..1eb199a7b6b3580dc2b2e8dedede8e6b79ee4cc5 100644 (file)
@@ -65,7 +65,7 @@ public final class XMLNamespace implements Comparable<XMLNamespace>, Immutable,
 
     @Override
     public boolean equals(final Object obj) {
-        return this == obj || obj instanceof XMLNamespace && namespace.equals(((XMLNamespace) obj).namespace);
+        return this == obj || obj instanceof XMLNamespace other && namespace.equals(other.namespace);
     }
 
     @Override