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