BUG-2304 Fix SchemaContextUtil for augmented nodes.
[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 import com.google.common.base.Optional;
23
24 /**
25  * The <code>default</code> implementation of Decimal Type Definition interface.
26  *
27  *
28  * @see DecimalTypeDefinition
29  */
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;
35
36     private 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.";
40
41     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.3";
42
43     private final List<RangeConstraint> rangeStatements;
44     private final Integer fractionDigits;
45     private static final int MAX_NUMBER_OF_FRACTION_DIGITS = 18;
46
47     /**
48      * Default Decimal64 Type Constructor. <br>
49      * <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}
58      *
59      * @param path
60      * @param fractionDigits
61      *            integer between 1 and 18 inclusively
62      *
63      * @see DecimalTypeDefinition
64      * @exception IllegalArgumentException
65      * @deprecated Use static factory {@link #create(SchemaPath, Integer)}.
66      */
67     @Deprecated
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");
72         }
73         this.fractionDigits = fractionDigits;
74         rangeStatements = defaultRangeStatements();
75         this.path = path;
76     }
77
78     public static Decimal64 create(final SchemaPath path, final Integer fractionDigits) {
79         return new Decimal64(path, fractionDigits);
80     }
81
82     /**
83      * Returns unmodifiable List with default definition of Range Statements.
84      *
85      * @return unmodifiable List with default definition of Range Statements.
86      */
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);
95     }
96
97     @Override
98     public DecimalTypeDefinition getBaseType() {
99         return null;
100     }
101
102     @Override
103     public String getUnits() {
104         return UNITS;
105     }
106
107     @Override
108     public Object getDefaultValue() {
109         return DEFAULT_VALUE;
110     }
111
112     @Override
113     public QName getQName() {
114         return NAME;
115     }
116
117     @Override
118     public SchemaPath getPath() {
119         return path;
120     }
121
122     @Override
123     public String getDescription() {
124         return description;
125     }
126
127     @Override
128     public String getReference() {
129         return REFERENCE;
130     }
131
132     @Override
133     public Status getStatus() {
134         return Status.CURRENT;
135     }
136
137     @Override
138     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
139         return Collections.emptyList();
140     }
141
142     @Override
143     public List<RangeConstraint> getRangeConstraints() {
144         return rangeStatements;
145     }
146
147     @Override
148     public Integer getFractionDigits() {
149         return fractionDigits;
150     }
151
152     @Override
153     public int hashCode() {
154         final int prime = 31;
155         int result = 1;
156         result = prime * result + ((NAME == null) ? 0 : NAME.hashCode());
157         result = prime * result + ((path == null) ? 0 : path.hashCode());
158         return result;
159     }
160
161     @Override
162     public boolean equals(final Object obj) {
163         if (this == obj) {
164             return true;
165         }
166         if (obj == null) {
167             return false;
168         }
169         if (getClass() != obj.getClass()) {
170             return false;
171         }
172         Decimal64 other = (Decimal64) obj;
173         if (path == null) {
174             if (other.path != null) {
175                 return false;
176             }
177         } else if (!path.equals(other.path)) {
178             return false;
179         }
180         return true;
181     }
182
183     @Override
184     public String toString() {
185         return Decimal64.class.getSimpleName() + "[qname=" + NAME + ", fractionDigits=" + fractionDigits + "]";
186     }
187 }