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