2 * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.common;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
13 import com.google.common.annotations.Beta;
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.base.Strings;
16 import java.math.BigDecimal;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.kohsuke.MetaInfServices;
21 import org.opendaylight.yangtools.concepts.Variant;
24 * Dedicated type for YANG's 'type decimal64' type. This class is similar to {@link BigDecimal}, but provides more
25 * efficient storage, as it has fixed precision.
27 * @author Robert Varga
31 public class Decimal64 extends Number implements CanonicalValue<Decimal64> {
32 @MetaInfServices(value = CanonicalValueSupport.class)
33 public static final class Support extends AbstractCanonicalValueSupport<Decimal64> {
35 super(Decimal64.class);
39 public Variant<Decimal64, CanonicalValueViolation> fromString(final String str) {
40 // https://tools.ietf.org/html/rfc6020#section-9.3.1
42 // A decimal64 value is lexically represented as an optional sign ("+"
43 // or "-"), followed by a sequence of decimal digits, optionally
44 // followed by a period ('.') as a decimal indicator and a sequence of
45 // decimal digits. If no sign is specified, "+" is assumed.
47 return CanonicalValueViolation.variantOf("Empty string is not a valid decimal64 representation");
50 // Deal with optional sign
51 final boolean negative;
53 switch (str.charAt(0)) {
67 // Sanity check length
68 if (idx == str.length()) {
69 return CanonicalValueViolation.variantOf("Missing digits after sign");
72 // Character limit, used for caching and cutting trailing zeroes
73 int limit = str.length() - 1;
75 // Skip any leading zeroes, but leave at least one
76 for (; idx < limit && str.charAt(idx) == '0'; idx++) {
77 final char ch = str.charAt(idx + 1);
78 if (ch < '0' || ch > '9') {
83 // Integer part and its length
87 for (; idx <= limit; idx++, intLen++) {
88 final char ch = str.charAt(idx);
93 if (intLen == MAX_FRACTION_DIGITS) {
94 return CanonicalValueViolation.variantOf(
95 "Integer part is longer than " + MAX_FRACTION_DIGITS + " digits");
98 intPart = 10 * intPart + toInt(ch, idx);
102 // No fraction digits, we are done
103 return Variant.ofFirst(new Decimal64((byte)1, intPart, 0, negative));
106 // Bump index to skip over period and check the remainder
109 return CanonicalValueViolation.variantOf("Value '" + str + "' is missing fraction digits");
112 // Trim trailing zeroes, if any
113 while (idx < limit && str.charAt(limit) == '0') {
117 final int fracLimit = MAX_FRACTION_DIGITS - intLen;
120 for (; idx <= limit; idx++, fracLen++) {
121 final char ch = str.charAt(idx);
122 if (fracLen == fracLimit) {
123 return CanonicalValueViolation.variantOf("Fraction part longer than " + fracLimit + " digits");
126 fracPart = 10 * fracPart + toInt(ch, idx);
129 return Variant.ofFirst(new Decimal64(fracLen, intPart, fracPart, negative));
132 private static int toInt(final char ch, final int index) {
133 if (ch < '0' || ch > '9') {
134 throw new NumberFormatException("Illegal character at offset " + index);
140 private static final CanonicalValueSupport<Decimal64> SUPPORT = new Support();
141 private static final long serialVersionUID = 1L;
143 private static final int MAX_FRACTION_DIGITS = 18;
145 private static final long[] SCALE = {
167 verify(SCALE.length == MAX_FRACTION_DIGITS);
170 private final byte scaleOffset;
171 private final long value;
174 Decimal64(final int fractionDigits, final long intPart, final long fracPart, final boolean negative) {
175 checkArgument(fractionDigits >= 1 && fractionDigits <= MAX_FRACTION_DIGITS);
176 this.scaleOffset = (byte) (fractionDigits - 1);
178 final long bits = intPart * SCALE[this.scaleOffset] + fracPart;
179 this.value = negative ? -bits : bits;
182 protected Decimal64(final Decimal64 other) {
183 this.scaleOffset = other.scaleOffset;
184 this.value = other.value;
187 public static Decimal64 valueOf(final byte byteVal) {
188 return byteVal < 0 ? new Decimal64(1, -byteVal, 0, true) : new Decimal64(1, byteVal, 0, false);
191 public static Decimal64 valueOf(final short shortVal) {
192 return shortVal < 0 ? new Decimal64(1, -shortVal, 0, true) : new Decimal64(1, shortVal, 0, false);
195 public static Decimal64 valueOf(final int intVal) {
196 return intVal < 0 ? new Decimal64(1, - (long)intVal, 0, true) : new Decimal64(1, intVal, 0, false);
199 public static Decimal64 valueOf(final long longVal) {
200 // XXX: we should be able to do something smarter here
201 return valueOf(Long.toString(longVal));
204 public static Decimal64 valueOf(final double doubleVal) {
205 // XXX: we should be able to do something smarter here
206 return valueOf(Double.toString(doubleVal));
209 public static Decimal64 valueOf(final BigDecimal decimalVal) {
210 // XXX: we should be able to do something smarter here
211 return valueOf(decimalVal.toPlainString());
215 * Attempt to parse a String into a Decimal64. This method uses minimum fraction digits required to hold
218 * @param str String to parser
219 * @return A Decimal64 instance
220 * @throws NullPointerException if value is null.
221 * @throws NumberFormatException if the string does not contain a parsable decimal64.
223 public static Decimal64 valueOf(final String str) {
224 final Variant<Decimal64, CanonicalValueViolation> variant = SUPPORT.fromString(str);
225 final Optional<Decimal64> value = variant.tryFirst();
226 if (value.isPresent()) {
229 final Optional<String> message = variant.getSecond().getMessage();
230 throw message.isPresent() ? new NumberFormatException(message.get()) : new NumberFormatException();
233 public final BigDecimal decimalValue() {
234 return BigDecimal.valueOf(value, scaleOffset + 1);
238 public final int intValue() {
239 return (int) intPart();
243 public final long longValue() {
248 public final float floatValue() {
249 return (float) doubleValue();
253 public final double doubleValue() {
254 return 1.0 * value / SCALE[scaleOffset];
258 * Converts this {@code BigDecimal} to a {@code byte}, checking for lost information. If this {@code Decimal64} has
259 * a nonzero fractional part or is out of the possible range for a {@code byte} result then
260 * an {@code ArithmeticException} is thrown.
262 * @return this {@code Decimal64} converted to a {@code byte}.
263 * @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in a {@code byte}.
265 public final byte byteValueExact() {
266 final long val = longValueExact();
267 final byte ret = (byte) val;
269 throw new ArithmeticException("Value " + val + " is outside of byte range");
275 * Converts this {@code BigDecimal} to a {@code short}, checking for lost information. If this {@code Decimal64} has
276 * a nonzero fractional part or is out of the possible range for a {@code short} result then
277 * an {@code ArithmeticException} is thrown.
279 * @return this {@code Decimal64} converted to a {@code short}.
280 * @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in a {@code short}.
282 public final short shortValueExact() {
283 final long val = longValueExact();
284 final short ret = (short) val;
286 throw new ArithmeticException("Value " + val + " is outside of short range");
292 * Converts this {@code BigDecimal} to an {@code int}, checking for lost information. If this {@code Decimal64} has
293 * a nonzero fractional part or is out of the possible range for an {@code int} result then
294 * an {@code ArithmeticException} is thrown.
296 * @return this {@code Decimal64} converted to an {@code int}.
297 * @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in an {@code int}.
299 public final int intValueExact() {
300 final long val = longValueExact();
301 final int ret = (int) val;
303 throw new ArithmeticException("Value " + val + " is outside of integer range");
309 * Converts this {@code BigDecimal} to a {@code long}, checking for lost information. If this {@code Decimal64} has
310 * a nonzero fractional part then an {@code ArithmeticException} is thrown.
312 * @return this {@code Decimal64} converted to a {@code long}.
313 * @throws ArithmeticException if {@code this} has a nonzero fractional part.
315 public final long longValueExact() {
316 if (fracPart() != 0) {
317 throw new ArithmeticException("Conversion of " + this + " would lose fraction");
323 @SuppressWarnings("checkstyle:parameterName")
324 public final int compareTo(final Decimal64 o) {
328 if (scaleOffset == o.scaleOffset) {
329 return Long.compare(value, o.value);
332 // XXX: we could do something smarter here
333 return Double.compare(doubleValue(), o.doubleValue());
337 public final String toCanonicalString() {
338 // https://tools.ietf.org/html/rfc6020#section-9.3.2
340 // The canonical form of a positive decimal64 does not include the sign
341 // "+". The decimal point is required. Leading and trailing zeros are
342 // prohibited, subject to the rule that there MUST be at least one digit
343 // before and after the decimal point. The value zero is represented as
345 final StringBuilder sb = new StringBuilder(21).append(intPart()).append('.');
346 final long fracPart = fracPart();
348 // We may need to zero-pad the fraction part
349 sb.append(Strings.padStart(Long.toString(fracPart), scaleOffset + 1, '0'));
354 return sb.toString();
358 public final CanonicalValueSupport<Decimal64> support() {
363 public final int hashCode() {
364 // We need to normalize the results in order to be consistent with equals()
365 return Long.hashCode(intPart()) * 31 + Long.hashCode(fracPart());
369 public final boolean equals(final @Nullable Object obj) {
373 if (!(obj instanceof Decimal64)) {
376 final Decimal64 other = (Decimal64) obj;
377 if (scaleOffset == other.scaleOffset) {
378 return value == other.value;
381 // We need to normalize both
382 return intPart() == other.intPart() && fracPart() == fracPart();
386 public final String toString() {
387 return toCanonicalString();
390 private long intPart() {
391 return value / SCALE[scaleOffset];
394 private long fracPart() {
395 return Math.abs(value % SCALE[scaleOffset]);