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;
48 * Default Decimal64 Type Constructor. <br>
50 * The initial range statements are set to Decimal64
51 * <code>min=-922337203685477580.8</code> and
52 * <code>max=922337203685477580.7</code> <br>
53 * The fractions digits MUST be defined as integer between 1 and 18
54 * inclusively as defined interface {@link DecimalTypeDefinition} <br>
55 * If the fraction digits are not defined inner the definition boundaries
56 * the constructor will throw {@link IllegalArgumentException}
58 * @param fractionDigits
59 * integer between 1 and 18 inclusively
61 * @see DecimalTypeDefinition
62 * @exception IllegalArgumentException
64 public Decimal64(final List<String> actualPath, final URI namespace,
65 final Date revision, final Integer fractionDigits) {
67 if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
68 throw new IllegalArgumentException(
69 "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
71 this.fractionDigits = fractionDigits;
72 rangeStatements = defaultRangeStatements();
73 this.path = BaseTypes.schemaPath(actualPath, namespace, revision);
77 * Decimal64 Type Constructor. <br>
79 * If parameter <code>Range Statements</code> is <code>null</code> or
80 * defined as <code>empty List</code> the constructor automatically assigns
81 * the boundaries as min and max value defined for Decimal64 in <a
82 * href="https://tools.ietf.org/html/rfc6020#section-9.3">[RFC-6020] The
83 * decimal64 Built-In Type</a> <br>
85 * The fractions digits MUST be defined as integer between 1 and 18
86 * inclusively as defined interface {@link DecimalTypeDefinition} <br>
87 * If the fraction digits are not defined inner the definition boundaries
88 * the constructor will throw {@link IllegalArgumentException}
90 * @param rangeStatements
91 * Range Constraint Statements
92 * @param fractionDigits
93 * integer between 1 and 18 inclusively
94 * @exception IllegalArgumentException
96 public Decimal64(final List<String> actualPath, final URI namespace,
97 final Date revision, final List<RangeConstraint> rangeStatements,
98 Integer fractionDigits) {
100 if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
101 throw new IllegalArgumentException(
102 "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
104 if (rangeStatements == null || rangeStatements.isEmpty()) {
105 this.rangeStatements = defaultRangeStatements();
107 this.rangeStatements = Collections.unmodifiableList(rangeStatements);
109 this.fractionDigits = fractionDigits;
110 this.path = BaseTypes.schemaPath(actualPath, namespace, revision);
114 * Decimal64 Type Constructor. <br>
115 * If parameter <code>Range Statements</code> is <code>null</code> or
116 * defined as <code>empty List</code> the constructor automatically assigns
117 * the boundaries as min and max value defined for Decimal64 in <a
118 * href="https://tools.ietf.org/html/rfc6020#section-9.3">[RFC-6020] The
119 * decimal64 Built-In Type</a> <br>
121 * The fractions digits MUST be defined as integer between 1 and 18
122 * inclusively as defined interface {@link DecimalTypeDefinition} <br>
123 * If the fraction digits are not defined inner the definition boundaries
124 * the constructor will throw {@link IllegalArgumentException}
127 * units associated with the type
128 * @param defaultValue
129 * Default Value for type
130 * @param rangeStatements
131 * Range Constraint Statements
132 * @param fractionDigits
133 * integer between 1 and 18 inclusively
135 * @exception IllegalArgumentException
137 public Decimal64(final List<String> actualPath, final URI namespace,
138 final Date revision, final String units, final BigDecimal defaultValue,
139 final List<RangeConstraint> rangeStatements,
140 final Integer fractionDigits) {
142 if (!((fractionDigits.intValue() > 1) && (fractionDigits.intValue() <= 18))) {
143 throw new IllegalArgumentException(
144 "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
147 if (rangeStatements == null || rangeStatements.isEmpty()) {
148 this.rangeStatements = defaultRangeStatements();
151 this.rangeStatements = Collections.unmodifiableList(rangeStatements);
154 this.defaultValue = defaultValue;
155 this.fractionDigits = fractionDigits;
156 this.path = BaseTypes.schemaPath(name);
160 * Returns unmodifiable List with default definition of Range Statements.
162 * @return unmodifiable List with default definition of Range Statements.
164 private List<RangeConstraint> defaultRangeStatements() {
165 final List<RangeConstraint> rangeStatements = new ArrayList<RangeConstraint>();
166 final BigDecimal min = new BigDecimal("-922337203685477580.8");
167 final BigDecimal max = new BigDecimal("922337203685477580.7");
168 final String rangeDescription = "Integer values between " + min
169 + " and " + max + ", inclusively.";
170 rangeStatements.add(BaseConstraints.rangeConstraint(min, max,
172 "https://tools.ietf.org/html/rfc6020#section-9.2.4"));
173 return Collections.unmodifiableList(rangeStatements);
177 public DecimalTypeDefinition getBaseType() {
182 public String getUnits() {
187 public Object getDefaultValue() {
192 public QName getQName() {
197 public SchemaPath getPath() {
202 public String getDescription() {
207 public String getReference() {
212 public Status getStatus() {
213 return Status.CURRENT;
217 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
218 return Collections.emptyList();
222 public List<RangeConstraint> getRangeStatements() {
223 return rangeStatements;
227 public Integer getFractionDigits() {
228 return fractionDigits;
232 public int hashCode() {
233 final int prime = 31;
235 result = prime * result + ((name == null) ? 0 : name.hashCode());
236 result = prime * result + ((path == null) ? 0 : path.hashCode());
241 public boolean equals(Object obj) {
248 if (getClass() != obj.getClass()) {
251 Decimal64 other = (Decimal64) obj;
253 if (other.name != null) {
256 } else if (!name.equals(other.name)) {
260 if (other.path != null) {
263 } else if (!path.equals(other.path)) {
270 public String toString() {
271 return Decimal64.class.getSimpleName() + "[qname=" + name
272 + ", fractionDigits=" + fractionDigits + "]";