Refactored base yang-java types.
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / AbstractSignedInteger.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.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.Status;
17 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
20
21 /**
22  * The Abstract Integer class defines implementation of IntegerTypeDefinition
23  * interface which represents SIGNED Integer values defined in Yang language. <br>
24  * The integer built-in types in Yang are int8, int16, int32, int64. They
25  * represent signed integers of different sizes:
26  *
27  * <ul>
28  * <li>int8 - represents integer values between -128 and 127, inclusively.</li>
29  * <li>int16 - represents integer values between -32768 and 32767, inclusively.</li>
30  * <li>int32 - represents integer values between -2147483648 and 2147483647,
31  * inclusively.</li>
32  * <li>int64 - represents integer values between -9223372036854775808 and
33  * 9223372036854775807, inclusively.</li>
34  * </ul>
35  *
36  */
37 public abstract class AbstractSignedInteger implements IntegerTypeDefinition {
38     private final QName name;
39     private final SchemaPath path;
40     private final String description;
41     private final String reference = "https://tools.ietf.org/html/rfc6020#section-9.2";
42     private final String units;
43     private final List<RangeConstraint> rangeStatements;
44
45     /**
46      * @param name
47      * @param description
48      * @param minRange
49      * @param maxRange
50      * @param units
51      */
52     public AbstractSignedInteger(final QName name, final String description, final Number minRange,
53             final Number maxRange, final String units) {
54         this.name = name;
55         this.path = new SchemaPath(Collections.singletonList(name), true);
56         this.description = description;
57         this.units = units;
58         this.rangeStatements = new ArrayList<RangeConstraint>();
59         final String rangeDescription = "Integer values between " + minRange + " and " + maxRange + ", inclusively.";
60         this.rangeStatements.add(BaseConstraints.rangeConstraint(minRange, maxRange, rangeDescription,
61                 "https://tools.ietf.org/html/rfc6020#section-9.2.4"));
62     }
63
64     @Override
65     public String getUnits() {
66         return units;
67     }
68
69     @Override
70     public QName getQName() {
71         return name;
72     }
73
74     @Override
75     public SchemaPath getPath() {
76         return path;
77     }
78
79     @Override
80     public String getDescription() {
81         return description;
82     }
83
84     @Override
85     public String getReference() {
86         return reference;
87     }
88
89     @Override
90     public Status getStatus() {
91         return Status.CURRENT;
92     }
93
94     @Override
95     public List<RangeConstraint> getRangeStatements() {
96         return rangeStatements;
97     }
98
99     @Override
100     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
101         return Collections.emptyList();
102     }
103
104     @Override
105     public int hashCode() {
106         final int prime = 31;
107         int result = 1;
108         result = prime * result + ((description == null) ? 0 : description.hashCode());
109         result = prime * result + ((name == null) ? 0 : name.hashCode());
110         result = prime * result + ((path == null) ? 0 : path.hashCode());
111         result = prime * result + ((rangeStatements == null) ? 0 : rangeStatements.hashCode());
112         result = prime * result + ((reference == null) ? 0 : reference.hashCode());
113         result = prime * result + ((units == null) ? 0 : units.hashCode());
114         return result;
115     }
116
117     @Override
118     public boolean equals(Object obj) {
119         if (this == obj) {
120             return true;
121         }
122         if (obj == null) {
123             return false;
124         }
125         if (getClass() != obj.getClass()) {
126             return false;
127         }
128         AbstractSignedInteger other = (AbstractSignedInteger) obj;
129         if (description == null) {
130             if (other.description != null) {
131                 return false;
132             }
133         } else if (!description.equals(other.description)) {
134             return false;
135         }
136         if (name == null) {
137             if (other.name != null) {
138                 return false;
139             }
140         } else if (!name.equals(other.name)) {
141             return false;
142         }
143         if (path == null) {
144             if (other.path != null) {
145                 return false;
146             }
147         } else if (!path.equals(other.path)) {
148             return false;
149         }
150         if (rangeStatements == null) {
151             if (other.rangeStatements != null) {
152                 return false;
153             }
154         } else if (!rangeStatements.equals(other.rangeStatements)) {
155             return false;
156         }
157         if (reference == null) {
158             if (other.reference != null) {
159                 return false;
160             }
161         } else if (!reference.equals(other.reference)) {
162             return false;
163         }
164         if (units == null) {
165             if (other.units != null) {
166                 return false;
167             }
168         } else if (!units.equals(other.units)) {
169             return false;
170         }
171         return true;
172     }
173
174     @Override
175     public String toString() {
176         StringBuilder builder = new StringBuilder();
177         builder.append("AbstractInteger [name=");
178         builder.append(name);
179         builder.append(", path=");
180         builder.append(path);
181         builder.append(", description=");
182         builder.append(description);
183         builder.append(", reference=");
184         builder.append(reference);
185         builder.append(", units=");
186         builder.append(units);
187         builder.append(", rangeStatements=");
188         builder.append(rangeStatements);
189         builder.append("]");
190         return builder.toString();
191     }
192 }