Use Objects.hashCode()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / BitsSpecificationEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 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.stmt.rfc6020.effective.type;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.ArrayList;
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.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 import org.opendaylight.yangtools.yang.model.api.Status;
19 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
20 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
24 import org.opendaylight.yangtools.yang.model.util.BitsType;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
28
29 public class BitsSpecificationEffectiveStatementImpl extends
30         EffectiveStatementBase<String, TypeStatement.BitsSpecification> implements BitsTypeDefinition, TypeDefinitionEffectiveBuilder {
31
32     private static final QName QNAME = BaseTypes.BITS_QNAME;
33     private static final String DESCRIPTION = "The bits built-in type represents a bit set. "
34             + "That is, a bits value is a set of flags identified by small integer position "
35             + "numbers starting at 0. Each bit number has an assigned name.";
36
37     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.7";
38     private static final String UNITS = "";
39     private final SchemaPath path;
40     private final List<Bit> bits;
41
42     public BitsSpecificationEffectiveStatementImpl(StmtContext<String, TypeStatement.BitsSpecification, EffectiveStatement<String, TypeStatement.BitsSpecification>> ctx) {
43         super(ctx);
44
45         List<Bit> bitsInit = new ArrayList<>();
46
47         path = Utils.getSchemaPath(ctx.getParentContext()).createChild(QNAME);
48
49         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
50             if (effectiveStatement instanceof Bit) {
51                 bitsInit.add(((Bit) effectiveStatement));
52             }
53         }
54
55         bits = ImmutableList.copyOf(bitsInit);
56     }
57
58     @Override
59     public List<Bit> getBits() {
60         return bits;
61     }
62
63     @Override
64     public BitsTypeDefinition getBaseType() {
65         return null;
66     }
67
68     @Override
69     public String getUnits() {
70         return UNITS;
71     }
72
73     @Override
74     public Object getDefaultValue() {
75         return bits;
76     }
77
78     @Override
79     public QName getQName() {
80         return QNAME;
81     }
82
83     @Override
84     public SchemaPath getPath() {
85         return path;
86     }
87
88     @Override
89     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
90         return Collections.emptyList();
91     }
92
93     @Override
94     public String getDescription() {
95         return DESCRIPTION;
96     }
97
98     @Override
99     public String getReference() {
100         return REFERENCE;
101     }
102
103     @Override
104     public Status getStatus() {
105         return Status.CURRENT;
106     }
107
108     @Override
109     public int hashCode() {
110         final int prime = 31;
111         int result = 1;
112         result = prime * result + Objects.hashCode(bits);
113         result = prime * result + QNAME.hashCode();
114         result = prime * result + path.hashCode();
115         return result;
116     }
117
118     @Override
119     public boolean equals(final Object obj) {
120         if (this == obj) {
121             return true;
122         }
123         if (obj == null) {
124             return false;
125         }
126         if (getClass() != obj.getClass()) {
127             return false;
128         }
129         BitsSpecificationEffectiveStatementImpl other = (BitsSpecificationEffectiveStatementImpl) obj;
130         if (bits == null) {
131             if (other.bits != null) {
132                 return false;
133             }
134         } else if (!bits.equals(other.bits)) {
135             return false;
136         }
137         if (path == null) {
138             if (other.path != null) {
139                 return false;
140             }
141         } else if (!path.equals(other.path)) {
142             return false;
143         }
144         return true;
145     }
146
147     @Override
148     public String toString() {
149         StringBuilder builder = new StringBuilder();
150         builder.append(BitsSpecificationEffectiveStatementImpl.class.getSimpleName());
151         builder.append(" [name=");
152         builder.append(QNAME);
153         builder.append(", path=");
154         builder.append(path);
155         builder.append(", description=");
156         builder.append(DESCRIPTION);
157         builder.append(", reference=");
158         builder.append(REFERENCE);
159         builder.append(", bits=");
160         builder.append(bits);
161         builder.append(", units=");
162         builder.append(UNITS);
163         builder.append("]");
164         return builder.toString();
165     }
166
167     private BitsType bitsTypeInstance = null;
168     @Override
169     public TypeDefinition<?> buildType() {
170
171         if(bitsTypeInstance != null) {
172             return bitsTypeInstance;
173         }
174
175         bitsTypeInstance = BitsType.create(path, bits);
176
177         return bitsTypeInstance;
178     }
179 }