YANGTOOLS-706: Retrofit EffectiveStatement interfaces into parser
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / IdentityEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
9
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;
17 import java.util.Set;
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.spi.meta.DerivedIdentitiesNamespace;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27
28 public final class IdentityEffectiveStatementImpl extends AbstractEffectiveSchemaNode<IdentityStatement>
29         implements IdentityEffectiveStatement, IdentitySchemaNode, MutableStatement {
30     private final Set<IdentitySchemaNode> derivedIdentities;
31     private Set<IdentitySchemaNode> baseIdentities;
32     private boolean sealed;
33
34     public IdentityEffectiveStatementImpl(
35             final StmtContext<QName, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> ctx) {
36         super(ctx);
37
38         this.baseIdentities = new HashSet<>();
39         ((StmtContext.Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
40
41         // initDerivedIdentities
42         final Set<IdentitySchemaNode> derivedIdentitiesInit = new HashSet<>();
43         final List<StmtContext<?, ?, ?>> derivedIdentitiesCtxList = ctx.getFromNamespace(
44                 DerivedIdentitiesNamespace.class, ctx.getStatementArgument());
45         if (derivedIdentitiesCtxList == null) {
46             this.derivedIdentities = ImmutableSet.of();
47             return;
48         }
49         for (final StmtContext<?, ?, ?> derivedIdentityCtx : derivedIdentitiesCtxList) {
50             final IdentityEffectiveStatementImpl derivedIdentity = (IdentityEffectiveStatementImpl) derivedIdentityCtx
51                     .buildEffective();
52             derivedIdentity.addBaseIdentity(this);
53             derivedIdentitiesInit.add(derivedIdentity);
54         }
55         this.derivedIdentities = ImmutableSet.copyOf(derivedIdentitiesInit);
56     }
57
58     private void addBaseIdentity(final IdentityEffectiveStatementImpl baseIdentity) {
59         Preconditions.checkState(!sealed, "Attempt to modify sealed identity effective statement %s", getQName());
60         this.baseIdentities.add(baseIdentity);
61     }
62
63     @Nonnull
64     @Override
65     public Set<IdentitySchemaNode> getBaseIdentities() {
66         Preconditions.checkState(sealed,
67                 "Attempt to get base identities from unsealed identity effective statement %s", getQName());
68         return baseIdentities;
69     }
70
71     @Override
72     public Set<IdentitySchemaNode> getDerivedIdentities() {
73         return Collections.unmodifiableSet(derivedIdentities);
74     }
75
76     @Override
77     public int hashCode() {
78         final int prime = 31;
79         int result = 1;
80         result = prime * result + Objects.hashCode(getQName());
81         result = prime * result + Objects.hashCode(getPath());
82         return result;
83     }
84
85     @Override
86     public boolean equals(final Object obj) {
87         if (this == obj) {
88             return true;
89         }
90         if (obj == null) {
91             return false;
92         }
93         if (getClass() != obj.getClass()) {
94             return false;
95         }
96         final IdentityEffectiveStatementImpl other = (IdentityEffectiveStatementImpl) obj;
97         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
98     }
99
100     @Override
101     public String toString() {
102         return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).toString();
103     }
104
105     @Override
106     public void seal() {
107         if (!sealed) {
108             baseIdentities = ImmutableSet.copyOf(baseIdentities);
109             sealed = true;
110         }
111     }
112 }