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.Collection;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Objects;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
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(final StmtContext<QName, IdentityStatement, IdentityEffectiveStatement> 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.coerceStatementArgument());
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 checkState(!sealed, "Attempt to modify sealed identity effective statement %s", getQName());
61 this.baseIdentities.add(baseIdentity);
65 public Collection<? extends IdentitySchemaNode> getBaseIdentities() {
66 checkState(sealed, "Attempt to get base identities from unsealed identity effective statement %s", getQName());
67 return baseIdentities;
71 public Collection<? extends IdentitySchemaNode> getDerivedIdentities() {
72 return Collections.unmodifiableSet(derivedIdentities);
76 public int hashCode() {
79 result = prime * result + Objects.hashCode(getQName());
80 result = prime * result + Objects.hashCode(getPath());
85 public boolean equals(final Object obj) {
92 if (getClass() != obj.getClass()) {
95 final IdentityEffectiveStatementImpl other = (IdentityEffectiveStatementImpl) obj;
96 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
100 public String toString() {
101 return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).toString();
107 baseIdentities = ImmutableSet.copyOf(baseIdentities);