Merge "BUG-973: fixed (de)serialization of union type."
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / 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.yangtools.yang.model.util;
9
10 import java.util.Collections;
11 import java.util.List;
12
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.model.api.Status;
16 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
19
20 /**
21  * The <code>default</code> implementation of Binary Type Definition interface.
22  *
23  * @see BinaryTypeDefinition
24  */
25 public final class BinaryType implements BinaryTypeDefinition {
26     private static final String DESCRIPTION = "The binary built-in type represents any binary data, i.e., a sequence of octets.";
27     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.8";
28     private static final String UNITS = "";
29
30     private static final BinaryType INSTANCE = new BinaryType();
31
32     private final QName name = BaseTypes.BINARY_QNAME;
33     private final SchemaPath path = SchemaPath.create(Collections.singletonList(name), true);
34     private final List<Byte> bytes = Collections.emptyList();
35     private final List<LengthConstraint> lengthConstraints;
36
37     private BinaryType() {
38         this.lengthConstraints = Collections.singletonList(
39                 BaseConstraints.lengthConstraint(0, Long.MAX_VALUE, "", ""));
40     }
41
42     public static BinaryType getInstance() {
43         return INSTANCE;
44     }
45
46     /*
47      * (non-Javadoc)
48      *
49      * @see
50      * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getBaseType()
51      */
52     @Override
53     public BinaryTypeDefinition getBaseType() {
54         return null;
55     }
56
57     /*
58      * (non-Javadoc)
59      *
60      * @see org.opendaylight.yangtools.yang.model.api.TypeDefinition#getUnits()
61      */
62     @Override
63     public String getUnits() {
64         return UNITS;
65     }
66
67     /*
68      * (non-Javadoc)
69      *
70      * @see
71      * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue
72      * ()
73      */
74     @Override
75     public Object getDefaultValue() {
76         return bytes;
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getQName()
83      */
84     @Override
85     public QName getQName() {
86         return name;
87     }
88
89     /*
90      * (non-Javadoc)
91      *
92      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getPath()
93      */
94     @Override
95     public SchemaPath getPath() {
96         return path;
97     }
98
99     /*
100      * (non-Javadoc)
101      *
102      * @see
103      * org.opendaylight.yangtools.yang.model.api.SchemaNode#getDescription()
104      */
105     @Override
106     public String getDescription() {
107         return DESCRIPTION;
108     }
109
110     /*
111      * (non-Javadoc)
112      *
113      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getReference()
114      */
115     @Override
116     public String getReference() {
117         return REFERENCE;
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getStatus()
124      */
125     @Override
126     public Status getStatus() {
127         return Status.CURRENT;
128     }
129
130     /*
131      * (non-Javadoc)
132      *
133      * @see
134      * org.opendaylight.yangtools.yang.model.base.type.api.BinaryTypeDefinition
135      * #getLengthConstraint ()
136      */
137     @Override
138     public List<LengthConstraint> getLengthConstraints() {
139         return lengthConstraints;
140     }
141
142     @Override
143     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
144         return Collections.emptyList();
145     }
146
147     @Override
148     public int hashCode() {
149         final int prime = 31;
150         int result = 1;
151         result = prime * result + ((bytes == null) ? 0 : bytes.hashCode());
152         result = prime * result + ((lengthConstraints == null) ? 0 : lengthConstraints.hashCode());
153         result = prime * result + ((name == null) ? 0 : name.hashCode());
154         result = prime * result + ((path == null) ? 0 : path.hashCode());
155         return result;
156     }
157
158     @Override
159     public boolean equals(final Object obj) {
160         if (this == obj) {
161             return true;
162         }
163         if (obj == null) {
164             return false;
165         }
166         if (getClass() != obj.getClass()) {
167             return false;
168         }
169         BinaryType other = (BinaryType) obj;
170         if (bytes == null) {
171             if (other.bytes != null) {
172                 return false;
173             }
174         } else if (!bytes.equals(other.bytes)) {
175             return false;
176         }
177         if (lengthConstraints == null) {
178             if (other.lengthConstraints != null) {
179                 return false;
180             }
181         } else if (!lengthConstraints.equals(other.lengthConstraints)) {
182             return false;
183         }
184         if (name == null) {
185             if (other.name != null) {
186                 return false;
187             }
188         } else if (!name.equals(other.name)) {
189             return false;
190         }
191         if (path == null) {
192             if (other.path != null) {
193                 return false;
194             }
195         } else if (!path.equals(other.path)) {
196             return false;
197         }
198         return true;
199     }
200
201     @Override
202     public String toString() {
203         StringBuilder builder = new StringBuilder();
204         builder.append("BinaryType [name=");
205         builder.append(name);
206         builder.append(", path=");
207         builder.append(path);
208         builder.append(", description=");
209         builder.append(DESCRIPTION);
210         builder.append(", reference=");
211         builder.append(REFERENCE);
212         builder.append(", bytes=");
213         builder.append(bytes);
214         builder.append(", lengthConstraints=");
215         builder.append(lengthConstraints);
216         builder.append(", units=");
217         builder.append(UNITS);
218         builder.append("]");
219         return builder.toString();
220     }
221 }