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