Merge "Added support for annotations in generated APIs."
[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
47     /**
48      * Default Decimal64 Type Constructor. <br>
49      * <br>
50      * The initial range statements are set to Decimal64
51      * <code>min=-922337203685477580.8</code> and
52      * <code>max=922337203685477580.7</code> <br>
53      * The fractions digits MUST be defined as integer between 1 and 18
54      * inclusively as defined interface {@link DecimalTypeDefinition} <br>
55      * If the fraction digits are not defined inner the definition boundaries
56      * the constructor will throw {@link IllegalArgumentException}
57      *
58      * @param fractionDigits
59      *            integer between 1 and 18 inclusively
60      *
61      * @see DecimalTypeDefinition
62      * @exception IllegalArgumentException
63      */
64     public Decimal64(final List<String> actualPath, final URI namespace,
65             final Date revision, final Integer fractionDigits) {
66         super();
67         if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
68             throw new IllegalArgumentException(
69                     "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
70         }
71         this.fractionDigits = fractionDigits;
72         rangeStatements = defaultRangeStatements();
73         this.path = BaseTypes.schemaPath(actualPath, namespace, revision);
74     }
75
76     /**
77      * Decimal64 Type Constructor. <br>
78      *
79      * If parameter <code>Range Statements</code> is <code>null</code> or
80      * defined as <code>empty List</code> the constructor automatically assigns
81      * the boundaries as min and max value defined for Decimal64 in <a
82      * href="https://tools.ietf.org/html/rfc6020#section-9.3">[RFC-6020] The
83      * decimal64 Built-In Type</a> <br>
84      * <br>
85      * The fractions digits MUST be defined as integer between 1 and 18
86      * inclusively as defined interface {@link DecimalTypeDefinition} <br>
87      * If the fraction digits are not defined inner the definition boundaries
88      * the constructor will throw {@link IllegalArgumentException}
89      *
90      * @param rangeStatements
91      *            Range Constraint Statements
92      * @param fractionDigits
93      *            integer between 1 and 18 inclusively
94      * @exception IllegalArgumentException
95      */
96     public Decimal64(final List<String> actualPath, final URI namespace,
97             final Date revision, final List<RangeConstraint> rangeStatements,
98             Integer fractionDigits) {
99         super();
100         if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
101             throw new IllegalArgumentException(
102                     "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
103         }
104         if (rangeStatements == null || rangeStatements.isEmpty()) {
105             this.rangeStatements = defaultRangeStatements();
106         } else {
107             this.rangeStatements = Collections.unmodifiableList(rangeStatements);
108         }
109         this.fractionDigits = fractionDigits;
110         this.path = BaseTypes.schemaPath(actualPath, namespace, revision);
111     }
112
113     /**
114      * Decimal64 Type Constructor. <br>
115      * If parameter <code>Range Statements</code> is <code>null</code> or
116      * defined as <code>empty List</code> the constructor automatically assigns
117      * the boundaries as min and max value defined for Decimal64 in <a
118      * href="https://tools.ietf.org/html/rfc6020#section-9.3">[RFC-6020] The
119      * decimal64 Built-In Type</a> <br>
120      * <br>
121      * The fractions digits MUST be defined as integer between 1 and 18
122      * inclusively as defined interface {@link DecimalTypeDefinition} <br>
123      * If the fraction digits are not defined inner the definition boundaries
124      * the constructor will throw {@link IllegalArgumentException}
125      *
126      * @param units
127      *            units associated with the type
128      * @param defaultValue
129      *            Default Value for type
130      * @param rangeStatements
131      *            Range Constraint Statements
132      * @param fractionDigits
133      *            integer between 1 and 18 inclusively
134      *
135      * @exception IllegalArgumentException
136      */
137     public Decimal64(final List<String> actualPath, final URI namespace,
138             final Date revision, final String units, final BigDecimal defaultValue,
139             final List<RangeConstraint> rangeStatements,
140             final Integer fractionDigits) {
141         super();
142         if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
143             throw new IllegalArgumentException(
144                     "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
145         }
146
147         if (rangeStatements == null || rangeStatements.isEmpty()) {
148             this.rangeStatements = defaultRangeStatements();
149
150         } else {
151             this.rangeStatements = Collections.unmodifiableList(rangeStatements);
152         }
153         this.units = units;
154         this.defaultValue = defaultValue;
155         this.fractionDigits = fractionDigits;
156         this.path = BaseTypes.schemaPath(name);
157     }
158
159     /**
160      * Returns unmodifiable List with default definition of Range Statements.
161      *
162      * @return unmodifiable List with default definition of Range Statements.
163      */
164     private List<RangeConstraint> defaultRangeStatements() {
165         final List<RangeConstraint> rangeStatements = new ArrayList<RangeConstraint>();
166         final BigDecimal min = new BigDecimal("-922337203685477580.8");
167         final BigDecimal max = new BigDecimal("922337203685477580.7");
168         final String rangeDescription = "Integer values between " + min
169                 + " and " + max + ", inclusively.";
170         rangeStatements.add(BaseConstraints.rangeConstraint(min, max,
171                 rangeDescription,
172                 "https://tools.ietf.org/html/rfc6020#section-9.2.4"));
173         return Collections.unmodifiableList(rangeStatements);
174     }
175
176     @Override
177     public DecimalTypeDefinition getBaseType() {
178         return this;
179     }
180
181     @Override
182     public String getUnits() {
183         return units;
184     }
185
186     @Override
187     public Object getDefaultValue() {
188         return defaultValue;
189     }
190
191     @Override
192     public QName getQName() {
193         return name;
194     }
195
196     @Override
197     public SchemaPath getPath() {
198         return path;
199     }
200
201     @Override
202     public String getDescription() {
203         return description;
204     }
205
206     @Override
207     public String getReference() {
208         return reference;
209     }
210
211     @Override
212     public Status getStatus() {
213         return Status.CURRENT;
214     }
215
216     @Override
217     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
218         return Collections.emptyList();
219     }
220
221     @Override
222     public List<RangeConstraint> getRangeStatements() {
223         return rangeStatements;
224     }
225
226     @Override
227     public Integer getFractionDigits() {
228         return fractionDigits;
229     }
230
231     @Override
232     public int hashCode() {
233         final int prime = 31;
234         int result = 1;
235         result = prime * result + ((name == null) ? 0 : name.hashCode());
236         result = prime * result + ((path == null) ? 0 : path.hashCode());
237         return result;
238     }
239
240     @Override
241     public boolean equals(Object obj) {
242         if (this == obj) {
243             return true;
244         }
245         if (obj == null) {
246             return false;
247         }
248         if (getClass() != obj.getClass()) {
249             return false;
250         }
251         Decimal64 other = (Decimal64) obj;
252         if (name == null) {
253             if (other.name != null) {
254                 return false;
255             }
256         } else if (!name.equals(other.name)) {
257             return false;
258         }
259         if (path == null) {
260             if (other.path != null) {
261                 return false;
262             }
263         } else if (!path.equals(other.path)) {
264             return false;
265         }
266         return true;
267     }
268
269     @Override
270     public String toString() {
271         return Decimal64.class.getSimpleName() + "[qname=" + name
272                 + ", fractionDigits=" + fractionDigits + "]";
273     }
274 }