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.rfc7950.stmt.identity;
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Objects;
18 import javax.annotation.Nonnull;
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.stmt.IdentityEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveSchemaNode;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedIdentitiesNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 final class IdentityEffectiveStatementImpl extends AbstractEffectiveSchemaNode<IdentityStatement>
30 implements IdentityEffectiveStatement, IdentitySchemaNode, MutableStatement {
31 private final Set<IdentitySchemaNode> derivedIdentities;
32 private Set<IdentitySchemaNode> baseIdentities;
33 private boolean sealed;
35 IdentityEffectiveStatementImpl(
36 final StmtContext<QName, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> ctx) {
39 this.baseIdentities = new HashSet<>();
40 ((StmtContext.Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
42 // initDerivedIdentities
43 final Set<IdentitySchemaNode> derivedIdentitiesInit = new HashSet<>();
44 final List<StmtContext<?, ?, ?>> derivedIdentitiesCtxList = ctx.getFromNamespace(
45 DerivedIdentitiesNamespace.class, ctx.getStatementArgument());
46 if (derivedIdentitiesCtxList == null) {
47 this.derivedIdentities = ImmutableSet.of();
50 for (final StmtContext<?, ?, ?> derivedIdentityCtx : derivedIdentitiesCtxList) {
51 final IdentityEffectiveStatementImpl derivedIdentity = (IdentityEffectiveStatementImpl) derivedIdentityCtx
53 derivedIdentity.addBaseIdentity(this);
54 derivedIdentitiesInit.add(derivedIdentity);
56 this.derivedIdentities = ImmutableSet.copyOf(derivedIdentitiesInit);
59 private void addBaseIdentity(final IdentityEffectiveStatementImpl baseIdentity) {
60 Preconditions.checkState(!sealed, "Attempt to modify sealed identity effective statement %s", getQName());
61 this.baseIdentities.add(baseIdentity);
66 public Set<IdentitySchemaNode> getBaseIdentities() {
67 Preconditions.checkState(sealed,
68 "Attempt to get base identities from unsealed identity effective statement %s", getQName());
69 return baseIdentities;
73 public Set<IdentitySchemaNode> getDerivedIdentities() {
74 return Collections.unmodifiableSet(derivedIdentities);
78 public int hashCode() {
81 result = prime * result + Objects.hashCode(getQName());
82 result = prime * result + Objects.hashCode(getPath());
87 public boolean equals(final Object obj) {
94 if (getClass() != obj.getClass()) {
97 final IdentityEffectiveStatementImpl other = (IdentityEffectiveStatementImpl) obj;
98 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
102 public String toString() {
103 return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).toString();
109 baseIdentities = ImmutableSet.copyOf(baseIdentities);