BUG-7052: reduce StatementContextBase proliferation
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / CaseEffectiveStatementImpl.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.Optional;
11 import java.util.Collection;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
15 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.CaseStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19
20 public final class CaseEffectiveStatementImpl extends AbstractEffectiveSimpleDataNodeContainer<CaseStatement> implements
21         ChoiceCaseNode, DerivableSchemaNode {
22
23     private final ChoiceCaseNode original;
24     private final boolean configuration;
25
26     public CaseEffectiveStatementImpl(
27             final StmtContext<QName, CaseStatement, EffectiveStatement<QName, CaseStatement>> ctx) {
28         super(ctx);
29         this.original = ctx.getOriginalCtx() == null ? null : (ChoiceCaseNode) ctx.getOriginalCtx().buildEffective();
30
31         if (ctx.isConfiguration()) {
32             configuration = isAtLeastOneChildConfiguration(ctx.declaredSubstatements())
33                     || isAtLeastOneChildConfiguration(ctx.effectiveSubstatements());
34         } else {
35             configuration = false;
36         }
37     }
38
39     private static boolean isAtLeastOneChildConfiguration(final Collection<? extends StmtContext<?, ?, ?>> children) {
40         return children.stream().anyMatch(StmtContext::isConfiguration);
41     }
42
43     @Override
44     public Optional<ChoiceCaseNode> getOriginal() {
45         return Optional.fromNullable(original);
46     }
47
48     @Override
49     public boolean isConfiguration() {
50         return configuration;
51     }
52
53     @Override
54     public int hashCode() {
55         final int prime = 31;
56         int result = 1;
57         result = prime * result + Objects.hashCode(getQName());
58         result = prime * result + Objects.hashCode(getPath());
59         return result;
60     }
61
62     @Override
63     public boolean equals(final Object obj) {
64         if (this == obj) {
65             return true;
66         }
67         if (obj == null) {
68             return false;
69         }
70         if (getClass() != obj.getClass()) {
71             return false;
72         }
73         CaseEffectiveStatementImpl other = (CaseEffectiveStatementImpl) obj;
74         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
75     }
76
77     @Override
78     public String toString() {
79         return CaseEffectiveStatementImpl.class.getSimpleName() + "[" +
80                 "qname=" +
81                 getQName() +
82                 "]";
83     }
84 }