Use String concatenation instead of StringBuffer/Builder
[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.collect.ImmutableSet;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Objects;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedIdentitiesNamespace;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22
23 public final class IdentityEffectiveStatementImpl extends AbstractEffectiveSchemaNode<IdentityStatement>
24         implements IdentitySchemaNode {
25     private IdentitySchemaNode baseIdentity;
26     private final Set<IdentitySchemaNode> derivedIdentities;
27
28     public IdentityEffectiveStatementImpl(
29             final StmtContext<QName, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> ctx) {
30         super(ctx);
31
32         // initDerivedIdentities
33         Set<IdentitySchemaNode> derivedIdentitiesInit = new HashSet<IdentitySchemaNode>();
34         List<StmtContext<?, ?, ?>> derivedIdentitiesCtxList = ctx.getFromNamespace(
35                 DerivedIdentitiesNamespace.class, ctx.getStatementArgument());
36         if (derivedIdentitiesCtxList == null) {
37             this.derivedIdentities = ImmutableSet.of();
38             return;
39         }
40         for (StmtContext<?, ?, ?> derivedIdentityCtx : derivedIdentitiesCtxList) {
41             IdentityEffectiveStatementImpl derivedIdentity = (IdentityEffectiveStatementImpl) derivedIdentityCtx
42                     .buildEffective();
43             derivedIdentity.initBaseIdentity(this);
44             derivedIdentitiesInit.add(derivedIdentity);
45         }
46         this.derivedIdentities = ImmutableSet.copyOf(derivedIdentitiesInit);
47     }
48
49     private void initBaseIdentity(final IdentityEffectiveStatementImpl baseIdentity) {
50         this.baseIdentity = baseIdentity;
51     }
52
53     @Override
54     public IdentitySchemaNode getBaseIdentity() {
55         return baseIdentity;
56     }
57
58     @Override
59     public Set<IdentitySchemaNode> getDerivedIdentities() {
60         return Collections.unmodifiableSet(derivedIdentities);
61     }
62
63     @Override
64     public int hashCode() {
65         final int prime = 31;
66         int result = 1;
67         result = prime * result + Objects.hashCode(getQName());
68         result = prime * result + Objects.hashCode(getPath());
69         return result;
70     }
71
72     @Override
73     public boolean equals(final Object obj) {
74         if (this == obj) {
75             return true;
76         }
77         if (obj == null) {
78             return false;
79         }
80         if (getClass() != obj.getClass()) {
81             return false;
82         }
83         IdentityEffectiveStatementImpl other = (IdentityEffectiveStatementImpl) obj;
84         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
85     }
86
87     @Override
88     public String toString() {
89         return IdentityEffectiveStatementImpl.class.getSimpleName() + "[" +
90                 "base=" + baseIdentity +
91                 ", qname=" + getQName() +
92                 "]";
93     }
94 }