Refactored implementation of getBaseType method for yang built-in types.
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / Decimal64.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.util;
9
10 import java.math.BigDecimal;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14
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;
21
22 /**
23  * The <code>default</code> implementation of Decimal Type Definition interface.
24  *
25  *
26  * @see DecimalTypeDefinition
27  */
28 public final class Decimal64 implements DecimalTypeDefinition {
29     private final QName name = BaseTypes.constructQName("decimal64");
30     private final SchemaPath path;
31     private static final String UNITS = "";
32     private static final BigDecimal DEFAULT_VALUE = null;
33
34     private final String description = "The decimal64 type represents a subset of the real numbers, which can "
35             + "be represented by decimal numerals. The value space of decimal64 is the set of numbers that can "
36             + "be obtained by multiplying a 64-bit signed integer by a negative power of ten, i.e., expressible as "
37             + "'i x 10^-n' where i is an integer64 and n is an integer between 1 and 18, inclusively.";
38
39     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.3";
40
41     private final List<RangeConstraint> rangeStatements;
42     private final Integer fractionDigits;
43     private static final int MAX_NUMBER_OF_FRACTION_DIGITS = 18;
44
45     /**
46      * Default Decimal64 Type Constructor. <br>
47      * <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
52      * {@link #MAX_NUMBER_OF_FRACTION_DIGITS} inclusively as defined interface
53      * {@link DecimalTypeDefinition} <br>
54      * If the fraction digits are not defined inner the definition boundaries
55      * the constructor will throw {@link IllegalArgumentException}
56      *
57      * @param path
58      * @param fractionDigits
59      *            integer between 1 and 18 inclusively
60      *
61      * @see DecimalTypeDefinition
62      * @exception IllegalArgumentException
63      */
64     public Decimal64(final SchemaPath path, final Integer fractionDigits) {
65         if (!((fractionDigits.intValue() >= 1) && (fractionDigits.intValue() <= MAX_NUMBER_OF_FRACTION_DIGITS))) {
66             throw new IllegalArgumentException(
67                     "The fraction digits outside of boundaries. Fraction digits MUST be integer between 1 and 18 inclusively");
68         }
69         this.fractionDigits = fractionDigits;
70         rangeStatements = defaultRangeStatements();
71         this.path = path;
72     }
73
74     /**
75      * Returns unmodifiable List with default definition of Range Statements.
76      *
77      * @return unmodifiable List with default definition of Range Statements.
78      */
79     private List<RangeConstraint> defaultRangeStatements() {
80         final List<RangeConstraint> rangeStmts = new ArrayList<RangeConstraint>();
81         final BigDecimal min = new BigDecimal("-922337203685477580.8");
82         final BigDecimal max = new BigDecimal("922337203685477580.7");
83         final String rangeDescription = "Integer values between " + min + " and " + max + ", inclusively.";
84         rangeStmts.add(BaseConstraints.rangeConstraint(min, max, rangeDescription,
85                 "https://tools.ietf.org/html/rfc6020#section-9.2.4"));
86         return Collections.unmodifiableList(rangeStmts);
87     }
88
89     @Override
90     public DecimalTypeDefinition getBaseType() {
91         return null;
92     }
93
94     @Override
95     public String getUnits() {
96         return UNITS;
97     }
98
99     @Override
100     public Object getDefaultValue() {
101         return DEFAULT_VALUE;
102     }
103
104     @Override
105     public QName getQName() {
106         return name;
107     }
108
109     @Override
110     public SchemaPath getPath() {
111         return path;
112     }
113
114     @Override
115     public String getDescription() {
116         return description;
117     }
118
119     @Override
120     public String getReference() {
121         return REFERENCE;
122     }
123
124     @Override
125     public Status getStatus() {
126         return Status.CURRENT;
127     }
128
129     @Override
130     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
131         return Collections.emptyList();
132     }
133
134     @Override
135     public List<RangeConstraint> getRangeConstraints() {
136         return rangeStatements;
137     }
138
139     @Override
140     public Integer getFractionDigits() {
141         return fractionDigits;
142     }
143
144     @Override
145     public int hashCode() {
146         final int prime = 31;
147         int result = 1;
148         result = prime * result + ((name == null) ? 0 : name.hashCode());
149         result = prime * result + ((path == null) ? 0 : path.hashCode());
150         return result;
151     }
152
153     @Override
154     public boolean equals(Object obj) {
155         if (this == obj) {
156             return true;
157         }
158         if (obj == null) {
159             return false;
160         }
161         if (getClass() != obj.getClass()) {
162             return false;
163         }
164         Decimal64 other = (Decimal64) obj;
165         if (name == null) {
166             if (other.name != null) {
167                 return false;
168             }
169         } else if (!name.equals(other.name)) {
170             return false;
171         }
172         if (path == null) {
173             if (other.path != null) {
174                 return false;
175             }
176         } else if (!path.equals(other.path)) {
177             return false;
178         }
179         return true;
180     }
181
182     @Override
183     public String toString() {
184         return Decimal64.class.getSimpleName() + "[qname=" + name + ", fractionDigits=" + fractionDigits + "]";
185     }
186 }