Bug 6414: Fixed DataNodeIterator's traverseModule method
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / Decimal64.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.util;
9
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;
23
24 /**
25  * The <code>default</code> implementation of Decimal Type Definition interface.
26  *
27  * @see DecimalTypeDefinition
28  * @deprecated Use {@link org.opendaylight.yangtools.yang.model.util.type.BaseTypes#decimalTypeBuilder(SchemaPath)} instead
29  */
30 @Deprecated
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;
38     static {
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();
59     }
60
61     private static List<RangeConstraint> createRangeConstraint(final String min, final String max) {
62         final String description = "Decimal values between " + min + " and " + max +", inclusively";
63
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")));
66     }
67
68     private final List<RangeConstraint> rangeStatements;
69     private final int fractionDigits;
70     private final SchemaPath path;
71
72     /**
73      * Default Decimal64 Type Constructor. <br>
74      * <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}
80      *
81      * @param path
82      * @param fractionDigits integer between 1 and 18 inclusively
83      * @throws IllegalArgumentException
84      *
85      * @see DecimalTypeDefinition
86      */
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);
90
91         this.path = Preconditions.checkNotNull(path);
92         this.fractionDigits = fractionDigits;
93         this.rangeStatements = IMPLICIT_RANGE_STATEMENTS.get(fractionDigits - 1);
94     }
95
96     public static Decimal64 create(final SchemaPath path, final Integer fractionDigits) {
97         return new Decimal64(path, fractionDigits);
98     }
99
100     @Override
101     public DecimalTypeDefinition getBaseType() {
102         return null;
103     }
104
105     @Override
106     public String getUnits() {
107         return "";
108     }
109
110     @Override
111     public Object getDefaultValue() {
112         return null;
113     }
114
115     @Override
116     public QName getQName() {
117         return BaseTypes.DECIMAL64_QNAME;
118     }
119
120     @Override
121     public SchemaPath getPath() {
122         return path;
123     }
124
125     @Override
126     public String getDescription() {
127         return DESCRIPTION;
128     }
129
130     @Override
131     public String getReference() {
132         return REFERENCE;
133     }
134
135     @Override
136     public Status getStatus() {
137         return Status.CURRENT;
138     }
139
140     @Override
141     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
142         return Collections.emptyList();
143     }
144
145     @Override
146     public List<RangeConstraint> getRangeConstraints() {
147         return rangeStatements;
148     }
149
150     @Override
151     public Integer getFractionDigits() {
152         return fractionDigits;
153     }
154
155     @Override
156     public int hashCode() {
157         final int prime = 31;
158         int result = 1;
159         result = prime * result + BaseTypes.DECIMAL64_QNAME.hashCode();
160         result = prime * result + path.hashCode();
161         return result;
162     }
163
164     @Override
165     public boolean equals(final Object obj) {
166         if (this == obj) {
167             return true;
168         }
169         if (obj == null) {
170             return false;
171         }
172         if (getClass() != obj.getClass()) {
173             return false;
174         }
175         Decimal64 other = (Decimal64) obj;
176         return path.equals(other.path);
177     }
178
179     @Override
180     public String toString() {
181         return Decimal64.class.getSimpleName() + "[qname=" + BaseTypes.DECIMAL64_QNAME + ", fractionDigits="
182                 + fractionDigits + "]";
183     }
184 }