Convert base types to implement CanonicalValue
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / Decimal64.java
index dc30514253f4a5a0060b17474cb6addf8162e6f9..5fba2e0e3fe8e700c621d38f3e90c770a3707ade 100644 (file)
@@ -14,7 +14,8 @@ import com.google.common.annotations.Beta;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Strings;
 import java.math.BigDecimal;
-import org.opendaylight.yangtools.concepts.Immutable;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 
 /**
  * Dedicated type for YANG's 'type decimal64' type. This class is similar to {@link BigDecimal}, but provides more
@@ -23,7 +24,20 @@ import org.opendaylight.yangtools.concepts.Immutable;
  * @author Robert Varga
  */
 @Beta
-public class Decimal64 extends Number implements Comparable<Decimal64>, Immutable {
+@NonNullByDefault
+public class Decimal64 extends Number implements CanonicalValue<Decimal64> {
+    private static final class Support extends AbstractCanonicalValueSupport<Decimal64> {
+        Support() {
+            super(Decimal64.class);
+        }
+
+        @Override
+        public Decimal64 fromString(final String str) {
+            return Decimal64.valueOf(str);
+        }
+    }
+
+    private static final CanonicalValueSupport<Decimal64> SUPPORT = new Support();
     private static final long serialVersionUID = 1L;
 
     private static final int MAX_FRACTION_DIGITS = 18;
@@ -301,6 +315,32 @@ public class Decimal64 extends Number implements Comparable<Decimal64>, Immutabl
         return Double.compare(doubleValue(), o.doubleValue());
     }
 
+    @Override
+    public final String toCanonicalString() {
+        // https://tools.ietf.org/html/rfc6020#section-9.3.2
+        //
+        // The canonical form of a positive decimal64 does not include the sign
+        // "+".  The decimal point is required.  Leading and trailing zeros are
+        // prohibited, subject to the rule that there MUST be at least one digit
+        // before and after the decimal point.  The value zero is represented as
+        // "0.0".
+        final StringBuilder sb = new StringBuilder(21).append(intPart()).append('.');
+        final long fracPart = fracPart();
+        if (fracPart != 0) {
+            // We may need to zero-pad the fraction part
+            sb.append(Strings.padStart(Long.toString(fracPart), scaleOffset + 1, '0'));
+        } else {
+            sb.append('0');
+        }
+
+        return sb.toString();
+    }
+
+    @Override
+    public final CanonicalValueSupport<Decimal64> support() {
+        return SUPPORT;
+    }
+
     @Override
     public final int hashCode() {
         // We need to normalize the results in order to be consistent with equals()
@@ -308,7 +348,7 @@ public class Decimal64 extends Number implements Comparable<Decimal64>, Immutabl
     }
 
     @Override
-    public final boolean equals(final Object obj) {
+    public final boolean equals(final @Nullable Object obj) {
         if (this == obj) {
             return true;
         }
@@ -326,23 +366,7 @@ public class Decimal64 extends Number implements Comparable<Decimal64>, Immutabl
 
     @Override
     public final String toString() {
-        // https://tools.ietf.org/html/rfc6020#section-9.3.2
-        //
-        // The canonical form of a positive decimal64 does not include the sign
-        // "+".  The decimal point is required.  Leading and trailing zeros are
-        // prohibited, subject to the rule that there MUST be at least one digit
-        // before and after the decimal point.  The value zero is represented as
-        // "0.0".
-        final StringBuilder sb = new StringBuilder(21).append(intPart()).append('.');
-        final long fracPart = fracPart();
-        if (fracPart != 0) {
-            // We may need to zero-pad the fraction part
-            sb.append(Strings.padStart(Long.toString(fracPart), scaleOffset + 1, '0'));
-        } else {
-            sb.append('0');
-        }
-
-        return sb.toString();
+        return toCanonicalString();
     }
 
     private long intPart() {