2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.LinkedList;
16 import java.util.List;
17 import java.util.Objects;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
24 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedIdentitiesNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
29 public class IdentityEffectiveStatementImpl extends
30 AbstractEffectiveDocumentedNode<QName, IdentityStatement> implements
32 private final QName qname;
33 private final SchemaPath path;
34 IdentitySchemaNode baseIdentity;
35 private ImmutableSet<IdentitySchemaNode> derivedIdentities;
37 ImmutableList<UnknownSchemaNode> unknownNodes;
39 public IdentityEffectiveStatementImpl(
40 StmtContext<QName, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> ctx) {
43 this.qname = ctx.getStatementArgument();
44 this.path = Utils.getSchemaPath(ctx);
46 initSubstatementCollections();
47 initDerivedIdentities(ctx);
50 private void initDerivedIdentities(
51 StmtContext<QName, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> ctx) {
53 Set<IdentitySchemaNode> derivedIdentitiesInit = new HashSet<IdentitySchemaNode>();
54 List<StmtContext<?, ?, ?>> derivedIdentitiesCtxList = ctx.getFromNamespace(
55 DerivedIdentitiesNamespace.class, ctx.getStatementArgument());
57 if(derivedIdentitiesCtxList == null) {
58 this.derivedIdentities = ImmutableSet.of();
62 for (StmtContext<?, ?, ?> derivedIdentityCtx : derivedIdentitiesCtxList) {
63 IdentityEffectiveStatementImpl derivedIdentity = (IdentityEffectiveStatementImpl) derivedIdentityCtx
65 derivedIdentity.initBaseIdentity(this);
66 derivedIdentitiesInit.add(derivedIdentity);
68 this.derivedIdentities = ImmutableSet.copyOf(derivedIdentitiesInit);
71 private void initBaseIdentity(IdentityEffectiveStatementImpl baseIdentity) {
72 this.baseIdentity = baseIdentity;
75 private void initSubstatementCollections() {
76 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
78 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
80 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
81 if (effectiveStatement instanceof UnknownSchemaNode) {
82 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
83 unknownNodesInit.add(unknownNode);
87 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
91 public QName getQName() {
96 public IdentitySchemaNode getBaseIdentity() {
101 public Set<IdentitySchemaNode> getDerivedIdentities() {
102 return Collections.unmodifiableSet(derivedIdentities);
106 public SchemaPath getPath() {
111 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
116 public int hashCode() {
117 final int prime = 31;
119 result = prime * result + Objects.hashCode(qname);
120 result = prime * result + Objects.hashCode(path);
125 public boolean equals(final Object obj) {
132 if (getClass() != obj.getClass()) {
135 IdentityEffectiveStatementImpl other = (IdentityEffectiveStatementImpl) obj;
137 if (other.qname != null) {
140 } else if (!qname.equals(other.qname)) {
144 if (other.path != null) {
147 } else if (!path.equals(other.path)) {
154 public String toString() {
155 StringBuilder sb = new StringBuilder(
156 IdentityEffectiveStatementImpl.class.getSimpleName());
158 sb.append("base=").append(baseIdentity);
159 sb.append(", qname=").append(qname);
161 return sb.toString();