Use {Byte,Short}.compareUnsigned 85/84285/4
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 10 Sep 2019 04:47:42 +0000 (06:47 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 10 Sep 2019 14:50:34 +0000 (14:50 +0000)
Java 9 has these utility methods, use them instead of custom-brewing
an equivalent.

Change-Id: I774f2c0dcfa9f602830a1c98761b36520613bd52
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint16.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint8.java
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint16Test.java

index e174bd9ae12b3362ee1e980b0e01c4262a11618e..b4a32cd9cdf9c12258ca240970c09c4e42151520 100644 (file)
@@ -257,7 +257,7 @@ public class Uint16 extends Number implements CanonicalValue<Uint16> {
     @Override
     @SuppressWarnings("checkstyle:parameterName")
     public final int compareTo(final Uint16 o) {
-        return Integer.compare(intValue(), o.intValue());
+        return Short.compareUnsigned(value, o.value);
     }
 
     @Override
index 5d47250268d1d5b76c75a453cfc0289d23826d49..ecd1b11b08e4bcdb5cf7d67ef0187da68d131c80 100644 (file)
@@ -241,7 +241,7 @@ public class Uint8 extends Number implements CanonicalValue<Uint8> {
     @Override
     @SuppressWarnings("checkstyle:parameterName")
     public final int compareTo(final Uint8 o) {
-        return intValue() - o.intValue();
+        return Byte.compareUnsigned(value, o.value);
     }
 
     @Override
index 5311162d941c7d0637859c486502643ca157199d..e84347cdeef0c17d3582cae5458aaca50e0128e8 100644 (file)
@@ -7,9 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.common;
 
+import static org.hamcrest.Matchers.greaterThan;
+import static org.hamcrest.Matchers.lessThan;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
@@ -36,15 +39,15 @@ public class Uint16Test {
         final Uint16 max = Uint16.valueOf(65535);
 
         assertEquals(0, zero.compareTo(zero));
-        assertEquals(-1, zero.compareTo(five));
-        assertEquals(-1, zero.compareTo(max));
+        assertThat(zero.compareTo(five), lessThan(0));
+        assertThat(zero.compareTo(max), lessThan(0));
 
-        assertEquals(1, five.compareTo(zero));
+        assertThat(five.compareTo(zero), greaterThan(0));
         assertEquals(0, five.compareTo(five));
-        assertEquals(-1, five.compareTo(max));
+        assertThat(five.compareTo(max), lessThan(0));
 
-        assertEquals(1, max.compareTo(zero));
-        assertEquals(1, max.compareTo(five));
+        assertThat(max.compareTo(zero), greaterThan(0));
+        assertThat(max.compareTo(five), greaterThan(0));
         assertEquals(0, max.compareTo(max));
     }