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.yangtools.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.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
22 import com.google.common.base.Optional;
25 * The <code>default</code> implementation of Decimal Type Definition interface.
28 * @see DecimalTypeDefinition
30 public final class Decimal64 implements DecimalTypeDefinition {
31 private static final QName NAME = BaseTypes.DECIMAL64_QNAME;
32 private final SchemaPath path;
33 private static final String UNITS = "";
34 private static final BigDecimal DEFAULT_VALUE = null;
36 private static final String DESCRIPTION = "The decimal64 type represents a subset of the real numbers, which can "
37 + "be represented by decimal numerals. The value space of decimal64 is the set of numbers that can "
38 + "be obtained by multiplying a 64-bit signed integer by a negative power of ten, i.e., expressible as "
39 + "'i x 10^-n' where i is an integer64 and n is an integer between 1 and 18, inclusively.";
41 private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.3";
43 private final List<RangeConstraint> rangeStatements;
44 private final Integer fractionDigits;
45 private static final int MAX_NUMBER_OF_FRACTION_DIGITS = 18;
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
54 * {@link #MAX_NUMBER_OF_FRACTION_DIGITS} inclusively as defined interface
55 * {@link DecimalTypeDefinition} <br>
56 * If the fraction digits are not defined inner the definition boundaries
57 * the constructor will throw {@link IllegalArgumentException}
60 * @param fractionDigits
61 * integer between 1 and 18 inclusively
63 * @see DecimalTypeDefinition
64 * @exception IllegalArgumentException
65 * @deprecated Use static factory {@link #create(SchemaPath, Integer)}.
68 public Decimal64(final SchemaPath path, final Integer fractionDigits) {
69 if (!((fractionDigits.intValue() >= 1) && (fractionDigits.intValue() <= MAX_NUMBER_OF_FRACTION_DIGITS))) {
70 throw new IllegalArgumentException(
71 "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
73 this.fractionDigits = fractionDigits;
74 rangeStatements = defaultRangeStatements();
78 public static Decimal64 create(final SchemaPath path, final Integer fractionDigits) {
79 return new Decimal64(path, fractionDigits);
83 * Returns unmodifiable List with default definition of Range Statements.
85 * @return unmodifiable List with default definition of Range Statements.
87 private List<RangeConstraint> defaultRangeStatements() {
88 final List<RangeConstraint> rangeStmts = new ArrayList<RangeConstraint>();
89 final BigDecimal min = new BigDecimal("-922337203685477580.8");
90 final BigDecimal max = new BigDecimal("922337203685477580.7");
91 final String rangeDescription = "Integer values between " + min + " and " + max + ", inclusively.";
92 rangeStmts.add(BaseConstraints.newRangeConstraint(min, max, Optional.of(rangeDescription),
93 Optional.of("https://tools.ietf.org/html/rfc6020#section-9.2.4")));
94 return Collections.unmodifiableList(rangeStmts);
98 public DecimalTypeDefinition getBaseType() {
103 public String getUnits() {
108 public Object getDefaultValue() {
109 return DEFAULT_VALUE;
113 public QName getQName() {
118 public SchemaPath getPath() {
123 public String getDescription() {
128 public String getReference() {
133 public Status getStatus() {
134 return Status.CURRENT;
138 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
139 return Collections.emptyList();
143 public List<RangeConstraint> getRangeConstraints() {
144 return rangeStatements;
148 public Integer getFractionDigits() {
149 return fractionDigits;
153 public int hashCode() {
154 final int prime = 31;
156 result = prime * result + ((NAME == null) ? 0 : NAME.hashCode());
157 result = prime * result + ((path == null) ? 0 : path.hashCode());
162 public boolean equals(final Object obj) {
169 if (getClass() != obj.getClass()) {
172 Decimal64 other = (Decimal64) obj;
174 if (other.path != null) {
177 } else if (!path.equals(other.path)) {
184 public String toString() {
185 return Decimal64.class.getSimpleName() + "[qname=" + NAME + ", fractionDigits=" + fractionDigits + "]";