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 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;
46 * Default Decimal64 Type Constructor. <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}
56 * @param fractionDigits
57 * integer between 1 and 18 inclusively
59 * @see DecimalTypeDefinition
60 * @exception IllegalArgumentException
62 public 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 rangeStatements = defaultRangeStatements();
70 this.path = BaseTypes.schemaPath(name);
74 * Decimal64 Type Constructor. <br>
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>
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}
87 * @param rangeStatements
88 * Range Constraint Statements
89 * @param fractionDigits
90 * integer between 1 and 18 inclusively
91 * @exception IllegalArgumentException
93 public Decimal64(final List<RangeConstraint> rangeStatements,
94 Integer fractionDigits) {
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");
100 if (rangeStatements == null || rangeStatements.isEmpty()) {
101 this.rangeStatements = defaultRangeStatements();
103 this.rangeStatements = Collections.unmodifiableList(rangeStatements);
105 this.fractionDigits = fractionDigits;
106 this.path = BaseTypes.schemaPath(name);
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>
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}
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
131 * @exception IllegalArgumentException
133 public Decimal64(final String units, final BigDecimal defaultValue,
134 final List<RangeConstraint> rangeStatements,
135 final Integer fractionDigits) {
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");
142 if (rangeStatements == null || rangeStatements.isEmpty()) {
143 this.rangeStatements = defaultRangeStatements();
146 this.rangeStatements = Collections.unmodifiableList(rangeStatements);
149 this.defaultValue = defaultValue;
150 this.fractionDigits = fractionDigits;
151 this.path = BaseTypes.schemaPath(name);
155 * Returns unmodifiable List with default definition of Range Statements.
157 * @return unmodifiable List with default definition of Range Statements.
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,
167 "https://tools.ietf.org/html/rfc6020#section-9.2.4"));
168 return Collections.unmodifiableList(rangeStatements);
172 public DecimalTypeDefinition getBaseType() {
177 public String getUnits() {
182 public Object getDefaultValue() {
187 public QName getQName() {
192 public SchemaPath getPath() {
197 public String getDescription() {
202 public String getReference() {
207 public Status getStatus() {
208 return Status.CURRENT;
212 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
213 return Collections.emptyList();
217 public List<RangeConstraint> getRangeStatements() {
218 return rangeStatements;
222 public Integer getFractionDigits() {
223 return fractionDigits;
227 public int hashCode() {
228 final int prime = 31;
230 result = prime * result + ((name == null) ? 0 : name.hashCode());
231 result = prime * result + ((path == null) ? 0 : path.hashCode());
236 public boolean equals(Object obj) {
243 if (getClass() != obj.getClass()) {
246 Decimal64 other = (Decimal64) obj;
248 if (other.name != null) {
251 } else if (!name.equals(other.name)) {
255 if (other.path != null) {
258 } else if (!path.equals(other.path)) {
265 public String toString() {
266 return Decimal64.class.getSimpleName() + "[qname=" + name
267 + ", fractionDigits=" + fractionDigits + "]";