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.math.RoundingMode;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.concepts.Either;
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 public static final class Support extends AbstractCanonicalValueSupport<Decimal64> {
34 super(Decimal64.class);
38 public Either<Decimal64, CanonicalValueViolation> fromString(final String str) {
39 // https://tools.ietf.org/html/rfc6020#section-9.3.1
41 // A decimal64 value is lexically represented as an optional sign ("+"
42 // or "-"), followed by a sequence of decimal digits, optionally
43 // followed by a period ('.') as a decimal indicator and a sequence of
44 // decimal digits. If no sign is specified, "+" is assumed.
46 return CanonicalValueViolation.variantOf("Empty string is not a valid decimal64 representation");
49 // Deal with optional sign
50 final boolean negative;
51 int idx = switch (str.charAt(0)) {
65 // Sanity check length
66 if (idx == str.length()) {
67 return CanonicalValueViolation.variantOf("Missing digits after sign");
70 // Character limit, used for caching and cutting trailing zeroes
71 int limit = str.length() - 1;
73 // Skip any leading zeroes, but leave at least one
74 for (; idx < limit && str.charAt(idx) == '0'; idx++) {
75 final char ch = str.charAt(idx + 1);
76 if (ch < '0' || ch > '9') {
81 // Integer part and its length
85 for (; idx <= limit; idx++, intLen++) {
86 final char ch = str.charAt(idx);
91 if (intLen == MAX_SCALE) {
92 return CanonicalValueViolation.variantOf(
93 "Integer part is longer than " + MAX_SCALE + " digits");
96 intPart = 10 * intPart + toInt(ch, idx);
100 // No fraction digits, we are done
101 return Either.ofFirst(new Decimal64((byte)1, intPart, 0, negative));
104 // Bump index to skip over period and check the remainder
107 return CanonicalValueViolation.variantOf("Value '" + str + "' is missing fraction digits");
110 // Trim trailing zeroes, if any
111 while (idx < limit && str.charAt(limit) == '0') {
115 final int fracLimit = MAX_SCALE - intLen + 1;
118 for (; idx <= limit; idx++, fracLen++) {
119 final char ch = str.charAt(idx);
120 if (fracLen == fracLimit) {
121 return CanonicalValueViolation.variantOf("Fraction part longer than " + fracLimit + " digits");
124 fracPart = 10 * fracPart + toInt(ch, idx);
127 return Either.ofFirst(new Decimal64(fracLen, intPart, fracPart, negative));
130 private static int toInt(final char ch, final int index) {
131 if (ch < '0' || ch > '9') {
132 throw new NumberFormatException("Illegal character at offset " + index);
138 private static final CanonicalValueSupport<Decimal64> SUPPORT = new Support();
139 private static final long serialVersionUID = 1L;
141 private static final int MAX_SCALE = 18;
143 private static final long[] FACTOR = {
164 private static final Decimal64Conversion[] CONVERSION = Decimal64Conversion.values();
165 private static final Decimal64[] MIN_VALUE;
166 private static final Decimal64[] MAX_VALUE;
169 verify(CONVERSION.length == MAX_SCALE);
170 verify(FACTOR.length == MAX_SCALE);
172 MIN_VALUE = new Decimal64[MAX_SCALE];
173 MAX_VALUE = new Decimal64[MAX_SCALE];
174 for (byte i = 0; i < MAX_SCALE; ++i) {
175 MIN_VALUE[i] = new Decimal64(i, -9223372036854775808L);
176 MAX_VALUE[i] = new Decimal64(i, 9223372036854775807L);
180 private final byte offset;
181 private final long value;
184 Decimal64(final int scale, final long intPart, final long fracPart, final boolean negative) {
185 offset = offsetOf(scale);
187 final long bits = intPart * FACTOR[offset] + fracPart;
188 value = negative ? -bits : bits;
191 private Decimal64(final byte offset, final long intPart, final boolean negative) {
192 this.offset = offset;
193 final long bits = intPart * FACTOR[offset];
194 value = negative ? -bits : bits;
197 private Decimal64(final byte offset, final long value) {
198 this.offset = offset;
202 protected Decimal64(final Decimal64 other) {
203 this(other.offset, other.value);
207 * Return a {@link Decimal64} with specified scale and unscaled value.
209 * @param scale scale to use
210 * @param unscaledValue unscaled value to use
211 * @return A Decimal64 instance
212 * @throws IllegalArgumentException if {@code scale} is not in range {@code [1..18]}
214 public static Decimal64 of(final int scale, final long unscaledValue) {
215 return new Decimal64(offsetOf(scale), unscaledValue);
219 * Return the minimum value supported in specified scale.
221 * @param scale scale to use
222 * @return Minimum value in that scale
223 * @throws IllegalArgumentException if {@code scale} is not in range {@code [1..18]}
225 public static Decimal64 minValueIn(final int scale) {
226 return MIN_VALUE[offsetOf(scale)];
230 * Return the maximum value supported in specified scale.
232 * @param scale scale to use
233 * @return Maximum value in that scale
234 * @throws IllegalArgumentException if {@code scale} is not in range {@code [1..18]}
236 public static Decimal64 maxValueIn(final int scale) {
237 return MAX_VALUE[offsetOf(scale)];
240 // >>> FIXME: these need truncating counterparts
241 public static Decimal64 valueOf(final int scale, final byte byteVal) {
242 final byte offset = offsetOf(scale);
243 final var conv = CONVERSION[offset];
244 if (byteVal < conv.minByte || byteVal > conv.maxByte) {
245 throw new IllegalArgumentException("Value " + byteVal + " is not in range ["
246 + conv.minByte + ".." + conv.maxByte + "] to fit scale " + scale);
248 return byteVal < 0 ? new Decimal64(offset, -byteVal, true) : new Decimal64(offset, byteVal, false);
251 public static Decimal64 valueOf(final int scale, final short shortVal) {
252 final byte offset = offsetOf(scale);
253 final var conv = CONVERSION[offset];
254 if (shortVal < conv.minShort || shortVal > conv.maxShort) {
255 throw new IllegalArgumentException("Value " + shortVal + " is not in range ["
256 + conv.minShort + ".." + conv.maxShort + "] to fit scale " + scale);
258 return shortVal < 0 ? new Decimal64(offset, -shortVal, true) : new Decimal64(offset, shortVal, false);
261 public static Decimal64 valueOf(final int scale, final int intVal) {
262 final byte offset = offsetOf(scale);
263 final var conv = CONVERSION[offset];
264 if (intVal < conv.minInt || intVal > conv.maxInt) {
265 throw new IllegalArgumentException("Value " + intVal + " is not in range ["
266 + conv.minInt + ".." + conv.maxInt + "] to fit scale " + scale);
268 return intVal < 0 ? new Decimal64(offset, - (long)intVal, true) : new Decimal64(offset, intVal, false);
271 public static Decimal64 valueOf(final int scale, final long longVal) {
272 final byte offset = offsetOf(scale);
273 final var conv = CONVERSION[offset];
274 if (longVal < conv.minLong || longVal > conv.maxLong) {
275 throw new IllegalArgumentException("Value " + longVal + " is not in range ["
276 + conv.minLong + ".." + conv.maxLong + "] to fit scale " + scale);
278 return longVal < 0 ? new Decimal64(offset, -longVal, true) : new Decimal64(offset, longVal, false);
282 // FIXME: this should take a RoundingMode and perform rounding
283 // FIXME: this should have a truncating counterpart
284 public static Decimal64 valueOf(final float floatVal, final RoundingMode rounding) {
285 // XXX: we should be able to do something smarter here
286 return valueOf(Float.toString(floatVal));
289 // FIXME: this should take a RoundingMode and perform rounding
290 // FIXME: this should have a truncating counterpart
291 public static Decimal64 valueOf(final double doubleVal, final RoundingMode rounding) {
292 // XXX: we should be able to do something smarter here
293 return valueOf(Double.toString(doubleVal));
296 public static Decimal64 valueOf(final BigDecimal decimalVal) {
297 // FIXME: we should be able to do something smarter here using BigDecimal.unscaledValue() and BigDecimal.scale()
298 return valueOf(decimalVal.toPlainString());
302 * Attempt to parse a String into a Decimal64. This method uses minimum fraction digits required to hold
305 * @param str String to parser
306 * @return A Decimal64 instance
307 * @throws NullPointerException if value is null.
308 * @throws NumberFormatException if the string does not contain a parsable decimal64.
310 public static Decimal64 valueOf(final String str) {
311 final Either<Decimal64, CanonicalValueViolation> variant = SUPPORT.fromString(str);
312 final Optional<Decimal64> value = variant.tryFirst();
313 if (value.isPresent()) {
316 final Optional<String> message = variant.getSecond().getMessage();
317 throw message.isPresent() ? new NumberFormatException(message.get()) : new NumberFormatException();
321 * Return the scale of this decimal. This is the number of fraction digits, in range {@code [1..18]}.
323 * @return This decimal's scale
325 public final int scale() {
330 * Return the unscaled value of this decimal.
332 * @return This decimal's unscaled value
334 public final long unscaledValue() {
338 public final BigDecimal decimalValue() {
339 return BigDecimal.valueOf(value, scale());
343 public final int intValue() {
344 return (int) intPart();
348 public final long longValue() {
353 public final float floatValue() {
354 return (float) doubleValue();
358 public final double doubleValue() {
359 return 1.0 * value / FACTOR[offset];
363 * Converts this {@code BigDecimal} to a {@code byte}, checking for lost information. If this {@code Decimal64} has
364 * a nonzero fractional part or is out of the possible range for a {@code byte} result then
365 * an {@code ArithmeticException} is thrown.
367 * @return this {@code Decimal64} converted to a {@code byte}.
368 * @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in a {@code byte}.
370 public final byte byteValueExact() {
371 final long val = longValueExact();
372 final byte ret = (byte) val;
374 throw new ArithmeticException("Value " + val + " is outside of byte range");
380 * Converts this {@code BigDecimal} to a {@code short}, checking for lost information. If this {@code Decimal64} has
381 * a nonzero fractional part or is out of the possible range for a {@code short} result then
382 * an {@code ArithmeticException} is thrown.
384 * @return this {@code Decimal64} converted to a {@code short}.
385 * @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in a {@code short}.
387 public final short shortValueExact() {
388 final long val = longValueExact();
389 final short ret = (short) val;
391 throw new ArithmeticException("Value " + val + " is outside of short range");
397 * Converts this {@code BigDecimal} to an {@code int}, checking for lost information. If this {@code Decimal64} has
398 * a nonzero fractional part or is out of the possible range for an {@code int} result then
399 * an {@code ArithmeticException} is thrown.
401 * @return this {@code Decimal64} converted to an {@code int}.
402 * @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in an {@code int}.
404 public final int intValueExact() {
405 final long val = longValueExact();
406 final int ret = (int) val;
408 throw new ArithmeticException("Value " + val + " is outside of integer range");
414 * Converts this {@code BigDecimal} to a {@code long}, checking for lost information. If this {@code Decimal64} has
415 * a nonzero fractional part then an {@code ArithmeticException} is thrown.
417 * @return this {@code Decimal64} converted to a {@code long}.
418 * @throws ArithmeticException if {@code this} has a nonzero fractional part.
420 public final long longValueExact() {
421 if (fracPart() != 0) {
422 throw new ArithmeticException("Conversion of " + this + " would lose fraction");
428 @SuppressWarnings("checkstyle:parameterName")
429 public final int compareTo(final Decimal64 o) {
433 if (offset == o.offset) {
434 return Long.compare(value, o.value);
437 // XXX: we could do something smarter here
438 return Double.compare(doubleValue(), o.doubleValue());
442 public final String toCanonicalString() {
443 // https://tools.ietf.org/html/rfc6020#section-9.3.2
445 // The canonical form of a positive decimal64 does not include the sign
446 // "+". The decimal point is required. Leading and trailing zeros are
447 // prohibited, subject to the rule that there MUST be at least one digit
448 // before and after the decimal point. The value zero is represented as
451 final long intPart = intPart();
452 final long fracPart = fracPart();
453 final StringBuilder sb = new StringBuilder(21);
454 if (intPart == 0 && fracPart < 0) {
457 sb.append(intPart).append('.');
460 // We may need to zero-pad the fraction part
461 sb.append(Strings.padStart(Long.toString(Math.abs(fracPart)), scale(), '0'));
466 return sb.toString();
470 public final CanonicalValueSupport<Decimal64> support() {
475 public final int hashCode() {
476 // We need to normalize the results in order to be consistent with equals()
477 return Long.hashCode(intPart()) * 31 + Long.hashCode(fracPart());
481 public final boolean equals(final @Nullable Object obj) {
482 return this == obj || obj instanceof Decimal64 other && equalsImpl(other);
486 * A slightly faster version of {@link #equals(Object)}.
488 * @param obj Decimal64 object
489 * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
491 public final boolean equals(final @Nullable Decimal64 obj) {
492 return this == obj || obj != null && equalsImpl(obj);
496 public final String toString() {
497 return toCanonicalString();
500 private boolean equalsImpl(final Decimal64 other) {
501 return offset == other.offset ? value == other.value
502 // We need to normalize both
503 : intPart() == other.intPart() && fracPart() == other.fracPart();
506 private long intPart() {
507 return value / FACTOR[offset];
510 private long fracPart() {
511 return value % FACTOR[offset];
514 private static byte offsetOf(final int scale) {
515 checkArgument(scale >= 1 && scale <= MAX_SCALE);
516 return (byte) (scale - 1);