2 * Copyright (c) 2013 Cisco Systems, Inc. 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.model.util;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableList.Builder;
14 import java.math.BigDecimal;
15 import java.util.Collections;
16 import java.util.List;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.model.api.Status;
20 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
25 * The <code>default</code> implementation of Decimal Type Definition interface.
27 * @see DecimalTypeDefinition
28 * @deprecated Use {@link org.opendaylight.yangtools.yang.model.util.type.BaseTypes#decimalTypeBuilder(SchemaPath)} instead
31 public final class Decimal64 implements DecimalTypeDefinition {
32 private static final String DESCRIPTION = "The decimal64 type represents a subset of the real numbers, which can "
33 + "be represented by decimal numerals. The value space of decimal64 is the set of numbers that can "
34 + "be obtained by multiplying a 64-bit signed integer by a negative power of ten, i.e., expressible as "
35 + "'i x 10^-n' where i is an integer64 and n is an integer between 1 and 18, inclusively.";
36 private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.3";
37 private static final List<List<RangeConstraint>> IMPLICIT_RANGE_STATEMENTS;
39 final Builder<List<RangeConstraint>> b = ImmutableList.builder();
40 b.add(createRangeConstraint("-922337203685477580.8", "922337203685477580.7"));
41 b.add(createRangeConstraint("-92233720368547758.08", "92233720368547758.07"));
42 b.add(createRangeConstraint("-9223372036854775.808", "9223372036854775.807"));
43 b.add(createRangeConstraint("-922337203685477.5808", "922337203685477.5807"));
44 b.add(createRangeConstraint("-92233720368547.75808", "92233720368547.75807"));
45 b.add(createRangeConstraint("-9223372036854.775808", "9223372036854.775807"));
46 b.add(createRangeConstraint("-922337203685.4775808", "922337203685.4775807"));
47 b.add(createRangeConstraint("-92233720368.54775808", "92233720368.54775807"));
48 b.add(createRangeConstraint("-9223372036.854775808", "9223372036.854775807"));
49 b.add(createRangeConstraint("-922337203.6854775808", "922337203.6854775807"));
50 b.add(createRangeConstraint("-92233720.36854775808", "92233720.36854775807"));
51 b.add(createRangeConstraint("-9223372.036854775808", "9223372.036854775807"));
52 b.add(createRangeConstraint("-922337.2036854775808", "922337.2036854775807"));
53 b.add(createRangeConstraint("-92233.72036854775808", "92233.72036854775807"));
54 b.add(createRangeConstraint("-9223.372036854775808", "9223.372036854775807"));
55 b.add(createRangeConstraint("-922.3372036854775808", "922.3372036854775807"));
56 b.add(createRangeConstraint("-92.23372036854775808", "92.23372036854775807"));
57 b.add(createRangeConstraint("-9.223372036854775808", "9.223372036854775807"));
58 IMPLICIT_RANGE_STATEMENTS = b.build();
61 private static List<RangeConstraint> createRangeConstraint(final String min, final String max) {
62 final String description = "Decimal values between " + min + " and " + max +", inclusively";
64 return ImmutableList.of(BaseConstraints.newRangeConstraint(new BigDecimal(min), new BigDecimal(max),
65 Optional.of(description), Optional.of("https://tools.ietf.org/html/rfc6020#section-9.3.4")));
68 private final List<RangeConstraint> rangeStatements;
69 private final int fractionDigits;
70 private final SchemaPath path;
73 * Default Decimal64 Type Constructor. <br>
75 * The initial range statements are set to implicit ranges for Decimal64 implied by the number of fraction
76 * digits. Fraction digits MUST be defined as integer between 1 and 18 inclusively as defined interface
77 * {@link DecimalTypeDefinition}<br>
78 * If the fraction digits are not defined inner the definition boundaries
79 * the constructor will throw {@link IllegalArgumentException}
82 * @param fractionDigits integer between 1 and 18 inclusively
83 * @throws IllegalArgumentException
85 * @see DecimalTypeDefinition
87 private Decimal64(final SchemaPath path, final int fractionDigits) {
88 Preconditions.checkArgument(fractionDigits >= 1 && fractionDigits <= 18,
89 "The number of fraction digits %s is outside of allowed range [1, 18]", fractionDigits);
91 this.path = Preconditions.checkNotNull(path);
92 this.fractionDigits = fractionDigits;
93 this.rangeStatements = IMPLICIT_RANGE_STATEMENTS.get(fractionDigits - 1);
96 public static Decimal64 create(final SchemaPath path, final Integer fractionDigits) {
97 return new Decimal64(path, fractionDigits);
101 public DecimalTypeDefinition getBaseType() {
106 public String getUnits() {
111 public Object getDefaultValue() {
116 public QName getQName() {
117 return BaseTypes.DECIMAL64_QNAME;
121 public SchemaPath getPath() {
126 public String getDescription() {
131 public String getReference() {
136 public Status getStatus() {
137 return Status.CURRENT;
141 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
142 return Collections.emptyList();
146 public List<RangeConstraint> getRangeConstraints() {
147 return rangeStatements;
151 public Integer getFractionDigits() {
152 return fractionDigits;
156 public int hashCode() {
157 final int prime = 31;
159 result = prime * result + BaseTypes.DECIMAL64_QNAME.hashCode();
160 result = prime * result + path.hashCode();
165 public boolean equals(final Object obj) {
172 if (getClass() != obj.getClass()) {
175 Decimal64 other = (Decimal64) obj;
176 return path.equals(other.path);
180 public String toString() {
181 return Decimal64.class.getSimpleName() + "[qname=" + BaseTypes.DECIMAL64_QNAME + ", fractionDigits="
182 + fractionDigits + "]";