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