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
* @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;
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()
}
@Override
- public final boolean equals(final Object obj) {
+ public final boolean equals(final @Nullable Object obj) {
if (this == obj) {
return true;
}
@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() {