2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\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
8 package org.opendaylight.controller.model.util;
\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
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
23 * The <code>default</code> implementation of Decimal Type Definition interface.
\r
26 * @see DecimalTypeDefinition
\r
28 public class Decimal64 implements DecimalTypeDefinition {
\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
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
40 private final String reference = "https://tools.ietf.org/html/rfc6020#section-9.3";
\r
42 private final List<RangeConstraint> rangeStatements;
\r
43 private final Integer fractionDigits;
\r
46 * Default Decimal64 Type Constructor. <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
56 * @param fractionDigits
\r
57 * integer between 1 and 18 inclusively
\r
59 * @see DecimalTypeDefinition
\r
60 * @exception IllegalArgumentException
\r
62 public Decimal64(final Integer fractionDigits) {
\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
68 this.fractionDigits = fractionDigits;
\r
69 rangeStatements = defaultRangeStatements();
\r
70 this.path = BaseTypes.schemaPath(name);
\r
74 * Decimal64 Type Constructor. <br>
\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
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
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
93 public Decimal64(final List<RangeConstraint> rangeStatements,
\r
94 Integer fractionDigits) {
\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
100 if (rangeStatements == null || rangeStatements.isEmpty()) {
\r
101 this.rangeStatements = defaultRangeStatements();
\r
103 this.rangeStatements = Collections.unmodifiableList(rangeStatements);
\r
105 this.fractionDigits = fractionDigits;
\r
106 this.path = BaseTypes.schemaPath(name);
\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
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
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
131 * @exception IllegalArgumentException
\r
133 public Decimal64(final String units, final BigDecimal defaultValue,
\r
134 final List<RangeConstraint> rangeStatements,
\r
135 final Integer fractionDigits) {
\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
142 if (rangeStatements == null || rangeStatements.isEmpty()) {
\r
143 this.rangeStatements = defaultRangeStatements();
\r
146 this.rangeStatements = Collections.unmodifiableList(rangeStatements);
\r
148 this.units = units;
\r
149 this.defaultValue = defaultValue;
\r
150 this.fractionDigits = fractionDigits;
\r
151 this.path = BaseTypes.schemaPath(name);
\r
155 * Returns unmodifiable List with default definition of Range Statements.
\r
157 * @return unmodifiable List with default definition of Range Statements.
\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
167 "https://tools.ietf.org/html/rfc6020#section-9.2.4"));
\r
168 return Collections.unmodifiableList(rangeStatements);
\r
172 public DecimalTypeDefinition getBaseType() {
\r
177 public String getUnits() {
\r
182 public Object getDefaultValue() {
\r
183 return defaultValue;
\r
187 public QName getQName() {
\r
192 public SchemaPath getPath() {
\r
197 public String getDescription() {
\r
198 return description;
\r
202 public String getReference() {
\r
207 public Status getStatus() {
\r
208 return Status.CURRENT;
\r
212 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
\r
213 return Collections.emptyList();
\r
217 public List<RangeConstraint> getRangeStatements() {
\r
218 return rangeStatements;
\r
222 public Integer getFractionDigits() {
\r
223 return fractionDigits;
\r
227 public int hashCode() {
\r
228 final int prime = 31;
\r
230 result = prime * result + ((name == null) ? 0 : name.hashCode());
\r
231 result = prime * result + ((path == null) ? 0 : path.hashCode());
\r
236 public boolean equals(Object obj) {
\r
243 if (getClass() != obj.getClass()) {
\r
246 Decimal64 other = (Decimal64) obj;
\r
247 if (name == null) {
\r
248 if (other.name != null) {
\r
251 } else if (!name.equals(other.name)) {
\r
254 if (path == null) {
\r
255 if (other.path != null) {
\r
258 } else if (!path.equals(other.path)) {
\r
265 public String toString() {
\r
266 return Decimal64.class.getSimpleName() + "[qname=" + name
\r
267 + ", fractionDigits=" + fractionDigits + "]";
\r