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