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