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.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveSchemaNode;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedIdentitiesNamespace;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30 final class IdentityEffectiveStatementImpl extends AbstractEffectiveSchemaNode<IdentityStatement>
31 implements IdentityEffectiveStatement, IdentitySchemaNode, MutableStatement {
32 private final ImmutableSet<IdentitySchemaNode> derivedIdentities;
33 private @NonNull Set<IdentitySchemaNode> baseIdentities;
34 private boolean sealed;
36 IdentityEffectiveStatementImpl(
37 final StmtContext<QName, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> ctx) {
40 this.baseIdentities = new HashSet<>();
41 ((StmtContext.Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
43 // initDerivedIdentities
44 final Set<IdentitySchemaNode> derivedIdentitiesInit = new HashSet<>();
45 final List<StmtContext<?, ?, ?>> derivedIdentitiesCtxList = ctx.getFromNamespace(
46 DerivedIdentitiesNamespace.class, ctx.coerceStatementArgument());
47 if (derivedIdentitiesCtxList == null) {
48 this.derivedIdentities = ImmutableSet.of();
51 for (final StmtContext<?, ?, ?> derivedIdentityCtx : derivedIdentitiesCtxList) {
52 final IdentityEffectiveStatementImpl derivedIdentity = (IdentityEffectiveStatementImpl) derivedIdentityCtx
54 derivedIdentity.addBaseIdentity(this);
55 derivedIdentitiesInit.add(derivedIdentity);
57 this.derivedIdentities = ImmutableSet.copyOf(derivedIdentitiesInit);
60 private void addBaseIdentity(final IdentityEffectiveStatementImpl baseIdentity) {
61 checkState(!sealed, "Attempt to modify sealed identity effective statement %s", getQName());
62 this.baseIdentities.add(baseIdentity);
66 public Set<IdentitySchemaNode> getBaseIdentities() {
67 checkState(sealed, "Attempt to get base identities from unsealed identity effective statement %s", getQName());
68 return baseIdentities;
72 public Set<IdentitySchemaNode> getDerivedIdentities() {
73 return Collections.unmodifiableSet(derivedIdentities);
77 public int hashCode() {
80 result = prime * result + Objects.hashCode(getQName());
81 result = prime * result + Objects.hashCode(getPath());
86 public boolean equals(final Object obj) {
93 if (getClass() != obj.getClass()) {
96 final IdentityEffectiveStatementImpl other = (IdentityEffectiveStatementImpl) obj;
97 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
101 public String toString() {
102 return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).toString();
108 baseIdentities = ImmutableSet.copyOf(baseIdentities);