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.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
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 AbstractEffectiveDocumentedNode<QName, IdentityStatement>
30 implements IdentitySchemaNode {
31 private final QName qname;
32 private final SchemaPath path;
33 private IdentitySchemaNode baseIdentity;
34 private ImmutableSet<IdentitySchemaNode> derivedIdentities;
35 private ImmutableList<UnknownSchemaNode> unknownNodes;
37 public IdentityEffectiveStatementImpl(
38 final StmtContext<QName, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> ctx) {
41 this.qname = ctx.getStatementArgument();
42 this.path = Utils.getSchemaPath(ctx);
44 initSubstatementCollections();
45 initDerivedIdentities(ctx);
48 private void initDerivedIdentities(
49 final StmtContext<QName, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> ctx) {
51 Set<IdentitySchemaNode> derivedIdentitiesInit = new HashSet<IdentitySchemaNode>();
52 List<StmtContext<?, ?, ?>> derivedIdentitiesCtxList = ctx.getFromNamespace(
53 DerivedIdentitiesNamespace.class, ctx.getStatementArgument());
55 if (derivedIdentitiesCtxList == null) {
56 this.derivedIdentities = ImmutableSet.of();
60 for (StmtContext<?, ?, ?> derivedIdentityCtx : derivedIdentitiesCtxList) {
61 IdentityEffectiveStatementImpl derivedIdentity = (IdentityEffectiveStatementImpl) derivedIdentityCtx
63 derivedIdentity.initBaseIdentity(this);
64 derivedIdentitiesInit.add(derivedIdentity);
66 this.derivedIdentities = ImmutableSet.copyOf(derivedIdentitiesInit);
69 private void initBaseIdentity(final IdentityEffectiveStatementImpl baseIdentity) {
70 this.baseIdentity = baseIdentity;
73 private void initSubstatementCollections() {
74 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
76 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
78 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
79 if (effectiveStatement instanceof UnknownSchemaNode) {
80 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
81 unknownNodesInit.add(unknownNode);
85 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
89 public QName getQName() {
94 public IdentitySchemaNode getBaseIdentity() {
99 public Set<IdentitySchemaNode> getDerivedIdentities() {
100 return Collections.unmodifiableSet(derivedIdentities);
104 public SchemaPath getPath() {
109 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
114 public int hashCode() {
115 final int prime = 31;
117 result = prime * result + Objects.hashCode(qname);
118 result = prime * result + Objects.hashCode(path);
123 public boolean equals(final Object obj) {
130 if (getClass() != obj.getClass()) {
133 IdentityEffectiveStatementImpl other = (IdentityEffectiveStatementImpl) obj;
134 return Objects.equals(qname, other.qname) && Objects.equals(path, other.path);
138 public String toString() {
139 StringBuilder sb = new StringBuilder(
140 IdentityEffectiveStatementImpl.class.getSimpleName());
142 sb.append("base=").append(baseIdentity);
143 sb.append(", qname=").append(qname);
145 return sb.toString();