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