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 static com.google.common.base.Preconditions.checkState;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collections;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Objects;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
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 ImmutableSet<IdentitySchemaNode> derivedIdentities;
32 private @NonNull Set<IdentitySchemaNode> baseIdentities;
33 private boolean sealed;
35 IdentityEffectiveStatementImpl(final StmtContext<QName, IdentityStatement, IdentityEffectiveStatement> ctx) {
38 this.baseIdentities = new HashSet<>();
39 ((StmtContext.Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
41 // initDerivedIdentities
42 final Set<IdentitySchemaNode> derivedIdentitiesInit = new HashSet<>();
43 final List<StmtContext<?, ?, ?>> derivedIdentitiesCtxList = ctx.getFromNamespace(
44 DerivedIdentitiesNamespace.class, ctx.coerceStatementArgument());
45 if (derivedIdentitiesCtxList == null) {
46 this.derivedIdentities = ImmutableSet.of();
49 for (final StmtContext<?, ?, ?> derivedIdentityCtx : derivedIdentitiesCtxList) {
50 final IdentityEffectiveStatementImpl derivedIdentity = (IdentityEffectiveStatementImpl) derivedIdentityCtx
52 derivedIdentity.addBaseIdentity(this);
53 derivedIdentitiesInit.add(derivedIdentity);
55 this.derivedIdentities = ImmutableSet.copyOf(derivedIdentitiesInit);
58 private void addBaseIdentity(final IdentityEffectiveStatementImpl baseIdentity) {
59 checkState(!sealed, "Attempt to modify sealed identity effective statement %s", getQName());
60 this.baseIdentities.add(baseIdentity);
64 public Set<IdentitySchemaNode> getBaseIdentities() {
65 checkState(sealed, "Attempt to get base identities from unsealed identity effective statement %s", getQName());
66 return baseIdentities;
70 public Set<IdentitySchemaNode> getDerivedIdentities() {
71 return Collections.unmodifiableSet(derivedIdentities);
75 public int hashCode() {
78 result = prime * result + Objects.hashCode(getQName());
79 result = prime * result + Objects.hashCode(getPath());
84 public boolean equals(final Object obj) {
91 if (getClass() != obj.getClass()) {
94 final IdentityEffectiveStatementImpl other = (IdentityEffectiveStatementImpl) obj;
95 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
99 public String toString() {
100 return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).toString();
106 baseIdentities = ImmutableSet.copyOf(baseIdentities);