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