2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.yang.model.util;
10 import java.math.BigDecimal;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.Date;
15 import java.util.List;
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;
25 * The <code>default</code> implementation of Decimal Type Definition interface.
28 * @see DecimalTypeDefinition
30 public class Decimal64 implements DecimalTypeDefinition {
32 private final QName name = BaseTypes.constructQName("decimal64");
33 private final SchemaPath path;
34 private String units = "";
35 private BigDecimal defaultValue = null;
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.";
42 private final String reference = "https://tools.ietf.org/html/rfc6020#section-9.3";
44 private final List<RangeConstraint> rangeStatements;
45 private final Integer fractionDigits;
46 private final DecimalTypeDefinition baseType;
49 * Default Decimal64 Type Constructor. <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}
59 * @param fractionDigits
60 * integer between 1 and 18 inclusively
62 * @see DecimalTypeDefinition
63 * @exception IllegalArgumentException
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");
70 this.fractionDigits = fractionDigits;
71 this.rangeStatements = defaultRangeStatements();
72 this.path = BaseTypes.schemaPath(name);
76 public Decimal64(final List<String> actualPath, final URI namespace,
77 final Date revision, final Integer fractionDigits) {
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");
83 this.fractionDigits = fractionDigits;
84 rangeStatements = defaultRangeStatements();
85 this.path = BaseTypes.schemaPath(actualPath, namespace, revision);
86 this.baseType = new Decimal64(fractionDigits);
90 * Decimal64 Type Constructor. <br>
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>
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}
106 * @param rangeStatements
107 * Range Constraint Statements
108 * @param fractionDigits
109 * integer between 1 and 18 inclusively
110 * @exception IllegalArgumentException
112 public Decimal64(final List<String> actualPath, final URI namespace,
113 final Date revision, final List<RangeConstraint> rangeStatements,
114 Integer fractionDigits) {
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");
120 if (rangeStatements == null || rangeStatements.isEmpty()) {
121 this.rangeStatements = defaultRangeStatements();
123 this.rangeStatements = Collections
124 .unmodifiableList(rangeStatements);
126 this.fractionDigits = fractionDigits;
127 this.path = BaseTypes.schemaPath(actualPath, namespace, revision);
128 this.baseType = new Decimal64(fractionDigits);
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>
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}
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
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) {
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");
167 if (rangeStatements == null || rangeStatements.isEmpty()) {
168 this.rangeStatements = defaultRangeStatements();
171 this.rangeStatements = Collections
172 .unmodifiableList(rangeStatements);
175 this.defaultValue = defaultValue;
176 this.fractionDigits = fractionDigits;
177 this.path = BaseTypes.schemaPath(name);
178 this.baseType = new Decimal64(fractionDigits);
182 * Returns unmodifiable List with default definition of Range Statements.
184 * @return unmodifiable List with default definition of Range Statements.
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,
194 "https://tools.ietf.org/html/rfc6020#section-9.2.4"));
195 return Collections.unmodifiableList(rangeStatements);
199 public DecimalTypeDefinition getBaseType() {
204 public String getUnits() {
209 public Object getDefaultValue() {
214 public QName getQName() {
219 public SchemaPath getPath() {
224 public String getDescription() {
229 public String getReference() {
234 public Status getStatus() {
235 return Status.CURRENT;
239 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
240 return Collections.emptyList();
244 public List<RangeConstraint> getRangeStatements() {
245 return rangeStatements;
249 public Integer getFractionDigits() {
250 return fractionDigits;
254 public int hashCode() {
255 final int prime = 31;
257 result = prime * result + ((name == null) ? 0 : name.hashCode());
258 result = prime * result + ((path == null) ? 0 : path.hashCode());
263 public boolean equals(Object obj) {
270 if (getClass() != obj.getClass()) {
273 Decimal64 other = (Decimal64) obj;
275 if (other.name != null) {
278 } else if (!name.equals(other.name)) {
282 if (other.path != null) {
285 } else if (!path.equals(other.path)) {
292 public String toString() {
293 return Decimal64.class.getSimpleName() + "[qname=" + name
294 + ", fractionDigits=" + fractionDigits + "]";