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;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
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;
23 * The <code>default</code> implementation of Decimal Type Definition interface.
26 * @see DecimalTypeDefinition
28 public final class Decimal64 implements DecimalTypeDefinition {
30 private final QName name = BaseTypes.constructQName("decimal64");
31 private final SchemaPath path;
32 private String units = "";
33 private BigDecimal defaultValue = null;
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.";
40 private final String reference = "https://tools.ietf.org/html/rfc6020#section-9.3";
42 private final List<RangeConstraint> rangeStatements;
43 private final Integer fractionDigits;
44 private final DecimalTypeDefinition baseType;
47 * Default Decimal64 Type Constructor. <br>
49 * The initial range statements are set to Decimal64
50 * <code>min=-922337203685477580.8</code> and
51 * <code>max=922337203685477580.7</code> <br>
52 * The fractions digits MUST be defined as integer between 1 and 18
53 * inclusively as defined interface {@link DecimalTypeDefinition} <br>
54 * If the fraction digits are not defined inner the definition boundaries
55 * the constructor will throw {@link IllegalArgumentException}
57 * @param fractionDigits
58 * integer between 1 and 18 inclusively
60 * @see DecimalTypeDefinition
61 * @exception IllegalArgumentException
63 private Decimal64(final Integer fractionDigits) {
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");
68 this.fractionDigits = fractionDigits;
69 this.rangeStatements = defaultRangeStatements();
70 this.path = BaseTypes.schemaPath(name);
74 public Decimal64(final SchemaPath path, final Integer fractionDigits) {
76 if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
77 throw new IllegalArgumentException(
78 "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
80 this.fractionDigits = fractionDigits;
81 rangeStatements = defaultRangeStatements();
83 this.baseType = new Decimal64(fractionDigits);
87 * Decimal64 Type Constructor. <br>
89 * If parameter <code>Range Statements</code> is <code>null</code> or
90 * defined as <code>empty List</code> the constructor automatically assigns
91 * the boundaries as min and max value defined for Decimal64 in <a
92 * href="https://tools.ietf.org/html/rfc6020#section-9.3">[RFC-6020] The
93 * decimal64 Built-In Type</a> <br>
95 * The fractions digits MUST be defined as integer between 1 and 18
96 * inclusively as defined interface {@link DecimalTypeDefinition} <br>
97 * If the fraction digits are not defined inner the definition boundaries
98 * the constructor will throw {@link IllegalArgumentException}
103 * @param rangeStatements
104 * Range Constraint Statements
105 * @param fractionDigits
106 * integer between 1 and 18 inclusively
107 * @exception IllegalArgumentException
109 public Decimal64(final SchemaPath path, final List<RangeConstraint> rangeStatements,
110 Integer fractionDigits) {
112 if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
113 throw new IllegalArgumentException(
114 "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
116 if (rangeStatements == null || rangeStatements.isEmpty()) {
117 this.rangeStatements = defaultRangeStatements();
119 this.rangeStatements = Collections
120 .unmodifiableList(rangeStatements);
122 this.fractionDigits = fractionDigits;
124 this.baseType = new Decimal64(fractionDigits);
128 * Decimal64 Type Constructor. <br>
129 * If parameter <code>Range Statements</code> is <code>null</code> or
130 * defined as <code>empty List</code> the constructor automatically assigns
131 * the boundaries as min and max value defined for Decimal64 in <a
132 * href="https://tools.ietf.org/html/rfc6020#section-9.3">[RFC-6020] The
133 * decimal64 Built-In Type</a> <br>
135 * The fractions digits MUST be defined as integer between 1 and 18
136 * inclusively as defined interface {@link DecimalTypeDefinition} <br>
137 * If the fraction digits are not defined inner the definition boundaries
138 * the constructor will throw {@link IllegalArgumentException}
144 * units associated with the type
145 * @param defaultValue
146 * Default Value for type
147 * @param rangeStatements
148 * Range Constraint Statements
149 * @param fractionDigits
150 * integer between 1 and 18 inclusively
152 public Decimal64(final SchemaPath path, final String units,
153 final BigDecimal defaultValue,
154 final List<RangeConstraint> rangeStatements,
155 final Integer fractionDigits) {
157 if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
158 throw new IllegalArgumentException(
159 "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
162 if (rangeStatements == null || rangeStatements.isEmpty()) {
163 this.rangeStatements = defaultRangeStatements();
166 this.rangeStatements = Collections
167 .unmodifiableList(rangeStatements);
170 this.defaultValue = defaultValue;
171 this.fractionDigits = fractionDigits;
173 this.baseType = new Decimal64(fractionDigits);
177 * Returns unmodifiable List with default definition of Range Statements.
179 * @return unmodifiable List with default definition of Range Statements.
181 private List<RangeConstraint> defaultRangeStatements() {
182 final List<RangeConstraint> rangeStatements = new ArrayList<RangeConstraint>();
183 final BigDecimal min = new BigDecimal("-922337203685477580.8");
184 final BigDecimal max = new BigDecimal("922337203685477580.7");
185 final String rangeDescription = "Integer values between " + min
186 + " and " + max + ", inclusively.";
187 rangeStatements.add(BaseConstraints.rangeConstraint(min, max,
189 "https://tools.ietf.org/html/rfc6020#section-9.2.4"));
190 return Collections.unmodifiableList(rangeStatements);
194 public DecimalTypeDefinition getBaseType() {
199 public String getUnits() {
204 public Object getDefaultValue() {
209 public QName getQName() {
214 public SchemaPath getPath() {
219 public String getDescription() {
224 public String getReference() {
229 public Status getStatus() {
230 return Status.CURRENT;
234 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
235 return Collections.emptyList();
239 public List<RangeConstraint> getRangeStatements() {
240 return rangeStatements;
244 public Integer getFractionDigits() {
245 return fractionDigits;
249 public int hashCode() {
250 final int prime = 31;
252 result = prime * result + ((name == null) ? 0 : name.hashCode());
253 result = prime * result + ((path == null) ? 0 : path.hashCode());
258 public boolean equals(Object obj) {
265 if (getClass() != obj.getClass()) {
268 Decimal64 other = (Decimal64) obj;
270 if (other.name != null) {
273 } else if (!name.equals(other.name)) {
277 if (other.path != null) {
280 } else if (!path.equals(other.path)) {
287 public String toString() {
288 return Decimal64.class.getSimpleName() + "[qname=" + name
289 + ", fractionDigits=" + fractionDigits + "]";