Merge "Bug 724: Handle hashCode/equals properly in InstanceIdentifier"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / BitImpl.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.parser.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 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
19
20 final class BitImpl implements BitsTypeDefinition.Bit {
21     private final Long position;
22     private final QName qname;
23     private final SchemaPath schemaPath;
24     private final String description;
25     private final String reference;
26     private final Status status;
27     private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
28
29     BitImpl(final Long position, final QName qname, final SchemaPath schemaPath, final String description,
30             final String reference, final Status status, final List<UnknownSchemaNode> unknownNodes) {
31         this.position = position;
32         this.qname = qname;
33         this.schemaPath = schemaPath;
34         this.description = description;
35         this.reference = reference;
36         this.status = status;
37         if (unknownNodes != null) {
38             this.unknownNodes = unknownNodes;
39         }
40     }
41
42     @Override
43     public QName getQName() {
44         return qname;
45     }
46
47     @Override
48     public SchemaPath getPath() {
49         return schemaPath;
50     }
51
52     @Override
53     public String getDescription() {
54         return description;
55     }
56
57     @Override
58     public String getReference() {
59         return reference;
60     }
61
62     @Override
63     public Status getStatus() {
64         return status;
65     }
66
67     @Override
68     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
69         return unknownNodes;
70     }
71
72     @Override
73     public Long getPosition() {
74         return position;
75     }
76
77     @Override
78     public String getName() {
79         return qname.getLocalName();
80     }
81
82     @Override
83     public int hashCode() {
84         final int prime = 31;
85         int result = 1;
86         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
87         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
88         result = prime * result + ((position == null) ? 0 : position.hashCode());
89         result = prime * result + ((unknownNodes == null) ? 0 : unknownNodes.hashCode());
90         return result;
91     }
92
93     @Override
94     public boolean equals(Object obj) {
95         if (this == obj) {
96             return true;
97         }
98         if (obj == null) {
99             return false;
100         }
101         if (getClass() != obj.getClass()) {
102             return false;
103         }
104         Bit other = (Bit) obj;
105         if (qname == null) {
106             if (other.getQName() != null) {
107                 return false;
108             }
109         } else if (!qname.equals(other.getQName())) {
110             return false;
111         }
112         if (schemaPath == null) {
113             if (other.getPath() != null) {
114                 return false;
115             }
116         } else if (!schemaPath.equals(other.getPath())) {
117             return false;
118         }
119         return true;
120     }
121
122     @Override
123     public String toString() {
124         return Bit.class.getSimpleName() + "[name=" + qname.getLocalName() + ", position=" + position + "]";
125     }
126
127 }