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