Refactored SchemaPath for yang java types. Fixed SchemaPath for augmented nodes types.
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-util / src / main / java / org / opendaylight / controller / yang / model / util / BinaryType.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.controller.yang.model.util;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.controller.yang.common.QName;
15 import org.opendaylight.controller.yang.model.api.SchemaPath;
16 import org.opendaylight.controller.yang.model.api.Status;
17 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.controller.yang.model.api.type.BinaryTypeDefinition;
19 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
20
21 /**
22  * The <code>default</code> implementation of Binary Type Definition interface.
23  *
24  * @see BinaryTypeDefinition
25  */
26 public final class BinaryType implements BinaryTypeDefinition {
27
28     private final QName name = BaseTypes.constructQName("binary");
29     private final SchemaPath path;
30     private final String description = "The binary built-in type represents any binary data, i.e., a sequence of octets.";
31     private final String reference = "https://tools.ietf.org/html/rfc6020#section-9.8";
32     private final BinaryTypeDefinition baseType;
33     private List<Byte> bytes;
34     private final List<LengthConstraint> lengthConstraints;
35     private String units = "";
36
37     private BinaryType() {
38         super();
39
40         final List<LengthConstraint> constraints = new ArrayList<LengthConstraint>();
41         constraints.add(BaseConstraints.lengthConstraint(0, Long.MAX_VALUE, "",
42                 ""));
43         this.lengthConstraints = Collections.unmodifiableList(constraints);
44         this.bytes = Collections.emptyList();
45         this.path = BaseTypes.schemaPath(name);
46         this.baseType = this;
47     }
48
49     public BinaryType(final SchemaPath path) {
50         super();
51
52         final List<LengthConstraint> constraints = new ArrayList<LengthConstraint>();
53         constraints.add(BaseConstraints.lengthConstraint(0, Long.MAX_VALUE, "",
54                 ""));
55         this.lengthConstraints = Collections.unmodifiableList(constraints);
56         this.bytes = Collections.emptyList();
57         this.path = path;
58         this.baseType = new BinaryType();
59     }
60
61     /**
62      *
63      *
64      * @param bytes
65      * @param lengthConstraints
66      * @param units
67      */
68     public BinaryType(final SchemaPath path, final List<Byte> bytes,
69             final List<LengthConstraint> lengthConstraints, final String units) {
70         super();
71
72         if ((lengthConstraints == null) || (lengthConstraints.isEmpty())) {
73             final List<LengthConstraint> constraints = new ArrayList<LengthConstraint>();
74             constraints.add(BaseConstraints.lengthConstraint(0, Long.MAX_VALUE,
75                     "", ""));
76             this.lengthConstraints = Collections.unmodifiableList(constraints);
77         } else {
78             this.lengthConstraints = Collections
79                     .unmodifiableList(lengthConstraints);
80         }
81
82         this.path = path;
83         this.bytes = Collections.unmodifiableList(bytes);
84         this.units = units;
85         this.baseType = new BinaryType();
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see
92      * org.opendaylight.controller.yang.model.api.TypeDefinition#getBaseType()
93      */
94     @Override
95     public BinaryTypeDefinition getBaseType() {
96         return baseType;
97     }
98
99     /*
100      * (non-Javadoc)
101      *
102      * @see org.opendaylight.controller.yang.model.api.TypeDefinition#getUnits()
103      */
104     @Override
105     public String getUnits() {
106         return units;
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see
113      * org.opendaylight.controller.yang.model.api.TypeDefinition#getDefaultValue
114      * ()
115      */
116     @Override
117     public Object getDefaultValue() {
118         return bytes;
119     }
120
121     /*
122      * (non-Javadoc)
123      *
124      * @see org.opendaylight.controller.yang.model.api.SchemaNode#getQName()
125      */
126     @Override
127     public QName getQName() {
128         return name;
129     }
130
131     /*
132      * (non-Javadoc)
133      *
134      * @see org.opendaylight.controller.yang.model.api.SchemaNode#getPath()
135      */
136     @Override
137     public SchemaPath getPath() {
138         return path;
139     }
140
141     /*
142      * (non-Javadoc)
143      *
144      * @see
145      * org.opendaylight.controller.yang.model.api.SchemaNode#getDescription()
146      */
147     @Override
148     public String getDescription() {
149         return description;
150     }
151
152     /*
153      * (non-Javadoc)
154      *
155      * @see org.opendaylight.controller.yang.model.api.SchemaNode#getReference()
156      */
157     @Override
158     public String getReference() {
159         return reference;
160     }
161
162     /*
163      * (non-Javadoc)
164      *
165      * @see org.opendaylight.controller.yang.model.api.SchemaNode#getStatus()
166      */
167     @Override
168     public Status getStatus() {
169         return Status.CURRENT;
170     }
171
172     /*
173      * (non-Javadoc)
174      *
175      * @see
176      * org.opendaylight.controller.yang.model.base.type.api.BinaryTypeDefinition
177      * #getLengthConstraint ()
178      */
179     @Override
180     public List<LengthConstraint> getLengthConstraints() {
181         return lengthConstraints;
182     }
183
184     @Override
185     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
186         return Collections.emptyList();
187     }
188
189     @Override
190     public int hashCode() {
191         final int prime = 31;
192         int result = 1;
193         result = prime * result + ((bytes == null) ? 0 : bytes.hashCode());
194         result = prime * result
195                 + ((description == null) ? 0 : description.hashCode());
196         result = prime
197                 * result
198                 + ((lengthConstraints == null) ? 0 : lengthConstraints
199                         .hashCode());
200         result = prime * result + ((name == null) ? 0 : name.hashCode());
201         result = prime * result + ((path == null) ? 0 : path.hashCode());
202         result = prime * result
203                 + ((reference == null) ? 0 : reference.hashCode());
204         result = prime * result + ((units == null) ? 0 : units.hashCode());
205         return result;
206     }
207
208     @Override
209     public boolean equals(Object obj) {
210         if (this == obj) {
211             return true;
212         }
213         if (obj == null) {
214             return false;
215         }
216         if (getClass() != obj.getClass()) {
217             return false;
218         }
219         BinaryType other = (BinaryType) obj;
220         if (bytes == null) {
221             if (other.bytes != null) {
222                 return false;
223             }
224         } else if (!bytes.equals(other.bytes)) {
225             return false;
226         }
227         if (description == null) {
228             if (other.description != null) {
229                 return false;
230             }
231         } else if (!description.equals(other.description)) {
232             return false;
233         }
234         if (lengthConstraints == null) {
235             if (other.lengthConstraints != null) {
236                 return false;
237             }
238         } else if (!lengthConstraints.equals(other.lengthConstraints)) {
239             return false;
240         }
241         if (name == null) {
242             if (other.name != null) {
243                 return false;
244             }
245         } else if (!name.equals(other.name)) {
246             return false;
247         }
248         if (path == null) {
249             if (other.path != null) {
250                 return false;
251             }
252         } else if (!path.equals(other.path)) {
253             return false;
254         }
255         if (reference == null) {
256             if (other.reference != null) {
257                 return false;
258             }
259         } else if (!reference.equals(other.reference)) {
260             return false;
261         }
262         if (units == null) {
263             if (other.units != null) {
264                 return false;
265             }
266         } else if (!units.equals(other.units)) {
267             return false;
268         }
269         return true;
270     }
271
272     @Override
273     public String toString() {
274         StringBuilder builder = new StringBuilder();
275         builder.append("BinaryType [name=");
276         builder.append(name);
277         builder.append(", path=");
278         builder.append(path);
279         builder.append(", description=");
280         builder.append(description);
281         builder.append(", reference=");
282         builder.append(reference);
283         builder.append(", bytes=");
284         builder.append(bytes);
285         builder.append(", lengthConstraints=");
286         builder.append(lengthConstraints);
287         builder.append(", units=");
288         builder.append(units);
289         builder.append("]");
290         return builder.toString();
291     }
292 }