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