Add type-bound equals() to common int types 31/85031/3
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 10 Oct 2019 10:36:57 +0000 (12:36 +0200)
committerRobert Varga <nite@hq.sk>
Thu, 10 Oct 2019 11:32:25 +0000 (11:32 +0000)
This adds additional Object.equals() flavors to bind to smaller methods
when the objects are known to be of matching types (but still nullable).

JIRA: YANGTOOLS-1030
Change-Id: I4c750e135069692682ba65b21503345fc682051a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Decimal64.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint16.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint32.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint64.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint8.java

index 32409a86e8d18cd698babd52c0c4b2f4da0def0c..78354f391594807493c9ff3d191ca9265c75b13c 100644 (file)
@@ -365,19 +365,17 @@ public class Decimal64 extends Number implements CanonicalValue<Decimal64> {
 
     @Override
     public final boolean equals(final @Nullable Object obj) {
 
     @Override
     public final boolean equals(final @Nullable Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (!(obj instanceof Decimal64)) {
-            return false;
-        }
-        final Decimal64 other = (Decimal64) obj;
-        if (scaleOffset == other.scaleOffset) {
-            return value == other.value;
-        }
+        return this == obj || obj instanceof Decimal64 && equalsImpl((Decimal64) obj);
+    }
 
 
-        // We need to normalize both
-        return intPart() == other.intPart() && fracPart() == other.fracPart();
+    /**
+     * A slightly faster version of {@link #equals(Object)}.
+     *
+     * @param obj Decimal64 object
+     * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
+     */
+    public final boolean equals(final @Nullable Decimal64 obj) {
+        return this == obj || obj != null && equalsImpl(obj);
     }
 
     @Override
     }
 
     @Override
@@ -385,6 +383,12 @@ public class Decimal64 extends Number implements CanonicalValue<Decimal64> {
         return toCanonicalString();
     }
 
         return toCanonicalString();
     }
 
+    private boolean equalsImpl(final Decimal64 other) {
+        return scaleOffset == other.scaleOffset ? value == other.value
+                // We need to normalize both
+                : intPart() == other.intPart() && fracPart() == other.fracPart();
+    }
+
     private long intPart() {
         return value / SCALE[scaleOffset];
     }
     private long intPart() {
         return value / SCALE[scaleOffset];
     }
index 4af4172e5f6fc8b2e13bc30426cc8fc2df060314..e45d2440f7ace74cabd2aad76d157ec5754c5c79 100644 (file)
@@ -293,6 +293,16 @@ public class Uint16 extends Number implements CanonicalValue<Uint16> {
         return this == obj || obj instanceof Uint16 && value == ((Uint16)obj).value;
     }
 
         return this == obj || obj instanceof Uint16 && value == ((Uint16)obj).value;
     }
 
+    /**
+     * A slightly faster version of {@link #equals(Object)}.
+     *
+     * @param obj Uint16 object
+     * @return  {@code true} if this object is the same as the obj argument; {@code false} otherwise.
+     */
+    public final boolean equals(final @Nullable Uint16 obj) {
+        return this == obj || obj != null && value == obj.value;
+    }
+
     @Override
     public final String toString() {
         return toCanonicalString();
     @Override
     public final String toString() {
         return toCanonicalString();
index cb881a8dc833b508c7376a80873cb7fc12cb1bc5..287597521d4f75f2daf3fb7a7d84a12df716f472 100644 (file)
@@ -308,6 +308,16 @@ public class Uint32 extends Number implements CanonicalValue<Uint32> {
         return this == obj || obj instanceof Uint32 && value == ((Uint32)obj).value;
     }
 
         return this == obj || obj instanceof Uint32 && value == ((Uint32)obj).value;
     }
 
+    /**
+     * A slightly faster version of {@link #equals(Object)}.
+     *
+     * @param obj Uint32 object
+     * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
+     */
+    public final boolean equals(final @Nullable Uint32 obj) {
+        return this == obj || obj != null && value == obj.value;
+    }
+
     @Override
     public final String toString() {
         return toCanonicalString();
     @Override
     public final String toString() {
         return toCanonicalString();
index 1a80bd666f513e958c238e8499df3021b21d6850..c3d91ab806480728d534a07ceb17dbb343b30c84 100644 (file)
@@ -326,6 +326,16 @@ public class Uint64 extends Number implements CanonicalValue<Uint64> {
         return this == obj || obj instanceof Uint64 && value == ((Uint64)obj).value;
     }
 
         return this == obj || obj instanceof Uint64 && value == ((Uint64)obj).value;
     }
 
+    /**
+     * A slightly faster version of {@link #equals(Object)}.
+     *
+     * @param obj Uint64 object
+     * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
+     */
+    public final boolean equals(final @Nullable Uint64 obj) {
+        return this == obj || obj != null && value == obj.value;
+    }
+
     @Override
     public final String toString() {
         return toCanonicalString();
     @Override
     public final String toString() {
         return toCanonicalString();
index e627168d5f165e10a2efdc7d04a6796e69b31ead..858c66c73a9ff8b0b79116b28d0a106a5b2339f2 100644 (file)
@@ -267,6 +267,16 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
         return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
     }
 
         return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
     }
 
+    /**
+     * A slightly faster version of {@link #equals(Object)}.
+     *
+     * @param obj Uint8 object
+     * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
+     */
+    public final boolean equals(final @Nullable Uint8 obj) {
+        return this == obj || obj != null && value == obj.value;
+    }
+
     @Override
     public final String toString() {
         return toCanonicalString();
     @Override
     public final String toString() {
         return toCanonicalString();