BUG-869: added proper handling of nullable parameter
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / BitsType.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.BitsTypeDefinition;
18
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.ImmutableList;
21
22 /**
23  * The <code>default</code> implementation of Bits Type Definition interface.
24  *
25  * @see BitsTypeDefinition
26  */
27 public final class BitsType implements BitsTypeDefinition {
28     private final static QName NAME = BaseTypes.BITS_QNAME;
29
30     private final SchemaPath path;
31     private final String description = "The bits built-in type represents a bit set.  "
32             + "That is, a bits value is a set of flags identified by small integer position "
33             + "numbers starting at 0.  Each bit number has an assigned name.";
34
35     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.7";
36     private final List<Bit> bits;
37     private static final String UNITS = "";
38
39     /**
40      * Default constructor. <br>
41      * Instantiates Bits type as empty bits list.
42      *
43      * @param path
44      * @deprecated Use static factory method {@link #create(SchemaPath, List)} instead.
45      */
46     @Deprecated
47     public BitsType(final SchemaPath path) {
48         super();
49         this.bits = Collections.emptyList();
50         this.path = Preconditions.checkNotNull(path,"path must not be null");
51     }
52
53     /**
54      * Constructor with explicit definition of bits assigned to BitsType.
55      *
56      * @param path
57      * @param bits
58      * @deprecated Use static factory method {@link #create(SchemaPath, List)} instead.
59      */
60     @Deprecated
61     public BitsType(final SchemaPath path, final List<Bit> bits) {
62         super();
63         this.bits = ImmutableList.copyOf(bits);
64         this.path = Preconditions.checkNotNull(path,"path must not be null");
65     }
66
67     public static BitsType create(final SchemaPath path, final List<Bit> bits) {
68         return new BitsType(path,bits);
69     }
70
71     /*
72      * (non-Javadoc)
73      *
74      * @see
75      * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getBaseType()
76      */
77     @Override
78     public BitsTypeDefinition getBaseType() {
79         return null;
80     }
81
82     /*
83      * (non-Javadoc)
84      *
85      * @see org.opendaylight.yangtools.yang.model.api.TypeDefinition#getUnits()
86      */
87     @Override
88     public String getUnits() {
89         return UNITS;
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see
96      * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue
97      * ()
98      */
99     @Override
100     public Object getDefaultValue() {
101         return bits;
102     }
103
104     /*
105      * (non-Javadoc)
106      *
107      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getQName()
108      */
109     @Override
110     public QName getQName() {
111         return NAME;
112     }
113
114     /*
115      * (non-Javadoc)
116      *
117      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getPath()
118      */
119     @Override
120     public SchemaPath getPath() {
121         return path;
122     }
123
124     /*
125      * (non-Javadoc)
126      *
127      * @see
128      * org.opendaylight.yangtools.yang.model.api.SchemaNode#getDescription()
129      */
130     @Override
131     public String getDescription() {
132         return description;
133     }
134
135     /*
136      * (non-Javadoc)
137      *
138      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getReference()
139      */
140     @Override
141     public String getReference() {
142         return REFERENCE;
143     }
144
145     /*
146      * (non-Javadoc)
147      *
148      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getStatus()
149      */
150     @Override
151     public Status getStatus() {
152         return Status.CURRENT;
153     }
154
155     @Override
156     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
157         return Collections.emptyList();
158     }
159
160     @Override
161     public List<Bit> getBits() {
162         return bits;
163     }
164
165     @Override
166     public int hashCode() {
167         final int prime = 31;
168         int result = 1;
169         result = prime * result + ((bits == null) ? 0 : bits.hashCode());
170         result = prime * result + ((description == null) ? 0 : description.hashCode());
171         result = prime * result + NAME.hashCode();
172         result = prime * result + path.hashCode();
173         return result;
174     }
175
176     @Override
177     public boolean equals(final Object obj) {
178         if (this == obj) {
179             return true;
180         }
181         if (obj == null) {
182             return false;
183         }
184         if (getClass() != obj.getClass()) {
185             return false;
186         }
187         BitsType other = (BitsType) obj;
188         if (bits == null) {
189             if (other.bits != null) {
190                 return false;
191             }
192         } else if (!bits.equals(other.bits)) {
193             return false;
194         }
195         if (description == null) {
196             if (other.description != null) {
197                 return false;
198             }
199         } else if (!description.equals(other.description)) {
200             return false;
201         }
202
203         if (path == null) {
204             if (other.path != null) {
205                 return false;
206             }
207         } else if (!path.equals(other.path)) {
208             return false;
209         }
210         return true;
211     }
212
213     @Override
214     public String toString() {
215         StringBuilder builder = new StringBuilder();
216         builder.append("BitsType [name=");
217         builder.append(NAME);
218         builder.append(", path=");
219         builder.append(path);
220         builder.append(", description=");
221         builder.append(description);
222         builder.append(", reference=");
223         builder.append(REFERENCE);
224         builder.append(", bits=");
225         builder.append(bits);
226         builder.append(", units=");
227         builder.append(UNITS);
228         builder.append("]");
229         return builder.toString();
230     }
231 }