6a070837bd8e9f3f79cec5c02639ffbdd7f0965b
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-util / src / main / java / org / opendaylight / controller / 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.controller.yang.model.util;
9
10 import java.math.BigDecimal;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14
15 import org.opendaylight.controller.yang.common.QName;
16 import org.opendaylight.controller.yang.model.api.SchemaPath;
17 import org.opendaylight.controller.yang.model.api.Status;
18 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.controller.yang.model.api.type.DecimalTypeDefinition;
20 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
21
22 /**
23  * The <code>default</code> implementation of Decimal Type Definition interface.
24  *
25  *
26  * @see DecimalTypeDefinition
27  */
28 public final class Decimal64 implements DecimalTypeDefinition {
29
30     private final QName name = BaseTypes.constructQName("decimal64");
31     private final SchemaPath path;
32     private String units = "";
33     private BigDecimal defaultValue = null;
34
35     private final String description = "The decimal64 type represents a subset of the real numbers, which can "
36             + "be represented by decimal numerals. The value space of decimal64 is the set of numbers that can "
37             + "be obtained by multiplying a 64-bit signed integer by a negative power of ten, i.e., expressible as "
38             + "'i x 10^-n' where i is an integer64 and n is an integer between 1 and 18, inclusively.";
39
40     private final String reference = "https://tools.ietf.org/html/rfc6020#section-9.3";
41
42     private final List<RangeConstraint> rangeStatements;
43     private final Integer fractionDigits;
44     private final DecimalTypeDefinition baseType;
45
46     /**
47      * Default Decimal64 Type Constructor. <br>
48      * <br>
49      * The initial range statements are set to Decimal64
50      * <code>min=-922337203685477580.8</code> and
51      * <code>max=922337203685477580.7</code> <br>
52      * The fractions digits MUST be defined as integer between 1 and 18
53      * inclusively as defined interface {@link DecimalTypeDefinition} <br>
54      * If the fraction digits are not defined inner the definition boundaries
55      * the constructor will throw {@link IllegalArgumentException}
56      *
57      * @param fractionDigits
58      *            integer between 1 and 18 inclusively
59      *
60      * @see DecimalTypeDefinition
61      * @exception IllegalArgumentException
62      */
63     private Decimal64(final Integer fractionDigits) {
64         if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
65             throw new IllegalArgumentException(
66                     "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
67         }
68         this.fractionDigits = fractionDigits;
69         this.rangeStatements = defaultRangeStatements();
70         this.path = BaseTypes.schemaPath(name);
71         this.baseType = this;
72     }
73
74     public Decimal64(final SchemaPath path, final Integer fractionDigits) {
75         super();
76         if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
77             throw new IllegalArgumentException(
78                     "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
79         }
80         this.fractionDigits = fractionDigits;
81         rangeStatements = defaultRangeStatements();
82         this.path = path;
83         this.baseType = new Decimal64(fractionDigits);
84     }
85
86     /**
87      * Decimal64 Type Constructor. <br>
88      *
89      * If parameter <code>Range Statements</code> is <code>null</code> or
90      * defined as <code>empty List</code> the constructor automatically assigns
91      * the boundaries as min and max value defined for Decimal64 in <a
92      * href="https://tools.ietf.org/html/rfc6020#section-9.3">[RFC-6020] The
93      * decimal64 Built-In Type</a> <br>
94      * <br>
95      * The fractions digits MUST be defined as integer between 1 and 18
96      * inclusively as defined interface {@link DecimalTypeDefinition} <br>
97      * If the fraction digits are not defined inner the definition boundaries
98      * the constructor will throw {@link IllegalArgumentException}
99      *
100      * @param actualPath
101      * @param namespace
102      * @param revision
103      * @param rangeStatements
104      *            Range Constraint Statements
105      * @param fractionDigits
106      *            integer between 1 and 18 inclusively
107      * @exception IllegalArgumentException
108      */
109     public Decimal64(final SchemaPath path, final List<RangeConstraint> rangeStatements,
110             Integer fractionDigits) {
111         super();
112         if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
113             throw new IllegalArgumentException(
114                     "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
115         }
116         if (rangeStatements == null || rangeStatements.isEmpty()) {
117             this.rangeStatements = defaultRangeStatements();
118         } else {
119             this.rangeStatements = Collections
120                     .unmodifiableList(rangeStatements);
121         }
122         this.fractionDigits = fractionDigits;
123         this.path = path;
124         this.baseType = new Decimal64(fractionDigits);
125     }
126
127     /**
128      * Decimal64 Type Constructor. <br>
129      * If parameter <code>Range Statements</code> is <code>null</code> or
130      * defined as <code>empty List</code> the constructor automatically assigns
131      * the boundaries as min and max value defined for Decimal64 in <a
132      * href="https://tools.ietf.org/html/rfc6020#section-9.3">[RFC-6020] The
133      * decimal64 Built-In Type</a> <br>
134      * <br>
135      * The fractions digits MUST be defined as integer between 1 and 18
136      * inclusively as defined interface {@link DecimalTypeDefinition} <br>
137      * If the fraction digits are not defined inner the definition boundaries
138      * the constructor will throw {@link IllegalArgumentException}
139      *
140      * @param actualPath
141      * @param namespace
142      * @param revision
143      * @param units
144      *            units associated with the type
145      * @param defaultValue
146      *            Default Value for type
147      * @param rangeStatements
148      *            Range Constraint Statements
149      * @param fractionDigits
150      *            integer between 1 and 18 inclusively
151      */
152     public Decimal64(final SchemaPath path, final String units,
153             final BigDecimal defaultValue,
154             final List<RangeConstraint> rangeStatements,
155             final Integer fractionDigits) {
156         super();
157         if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
158             throw new IllegalArgumentException(
159                     "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
160         }
161
162         if (rangeStatements == null || rangeStatements.isEmpty()) {
163             this.rangeStatements = defaultRangeStatements();
164
165         } else {
166             this.rangeStatements = Collections
167                     .unmodifiableList(rangeStatements);
168         }
169         this.units = units;
170         this.defaultValue = defaultValue;
171         this.fractionDigits = fractionDigits;
172         this.path = path;
173         this.baseType = new Decimal64(fractionDigits);
174     }
175
176     /**
177      * Returns unmodifiable List with default definition of Range Statements.
178      *
179      * @return unmodifiable List with default definition of Range Statements.
180      */
181     private List<RangeConstraint> defaultRangeStatements() {
182         final List<RangeConstraint> rangeStatements = new ArrayList<RangeConstraint>();
183         final BigDecimal min = new BigDecimal("-922337203685477580.8");
184         final BigDecimal max = new BigDecimal("922337203685477580.7");
185         final String rangeDescription = "Integer values between " + min
186                 + " and " + max + ", inclusively.";
187         rangeStatements.add(BaseConstraints.rangeConstraint(min, max,
188                 rangeDescription,
189                 "https://tools.ietf.org/html/rfc6020#section-9.2.4"));
190         return Collections.unmodifiableList(rangeStatements);
191     }
192
193     @Override
194     public DecimalTypeDefinition getBaseType() {
195         return baseType;
196     }
197
198     @Override
199     public String getUnits() {
200         return units;
201     }
202
203     @Override
204     public Object getDefaultValue() {
205         return defaultValue;
206     }
207
208     @Override
209     public QName getQName() {
210         return name;
211     }
212
213     @Override
214     public SchemaPath getPath() {
215         return path;
216     }
217
218     @Override
219     public String getDescription() {
220         return description;
221     }
222
223     @Override
224     public String getReference() {
225         return reference;
226     }
227
228     @Override
229     public Status getStatus() {
230         return Status.CURRENT;
231     }
232
233     @Override
234     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
235         return Collections.emptyList();
236     }
237
238     @Override
239     public List<RangeConstraint> getRangeStatements() {
240         return rangeStatements;
241     }
242
243     @Override
244     public Integer getFractionDigits() {
245         return fractionDigits;
246     }
247
248     @Override
249     public int hashCode() {
250         final int prime = 31;
251         int result = 1;
252         result = prime * result + ((name == null) ? 0 : name.hashCode());
253         result = prime * result + ((path == null) ? 0 : path.hashCode());
254         return result;
255     }
256
257     @Override
258     public boolean equals(Object obj) {
259         if (this == obj) {
260             return true;
261         }
262         if (obj == null) {
263             return false;
264         }
265         if (getClass() != obj.getClass()) {
266             return false;
267         }
268         Decimal64 other = (Decimal64) obj;
269         if (name == null) {
270             if (other.name != null) {
271                 return false;
272             }
273         } else if (!name.equals(other.name)) {
274             return false;
275         }
276         if (path == null) {
277             if (other.path != null) {
278                 return false;
279             }
280         } else if (!path.equals(other.path)) {
281             return false;
282         }
283         return true;
284     }
285
286     @Override
287     public String toString() {
288         return Decimal64.class.getSimpleName() + "[qname=" + name
289                 + ", fractionDigits=" + fractionDigits + "]";
290     }
291 }