Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / anyxml / AnyxmlEffectiveStatementImpl.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.anyxml;
9
10 import com.google.common.collect.ImmutableSet;
11 import java.util.Collection;
12 import java.util.Objects;
13 import java.util.Optional;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
18 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDataSchemaNode;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23
24 final class AnyxmlEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<AnyxmlStatement>
25         implements AnyxmlEffectiveStatement, AnyxmlSchemaNode, DerivableSchemaNode {
26
27     private final ImmutableSet<MustDefinition> mustConstraints;
28     private final AnyxmlSchemaNode original;
29     private final boolean mandatory;
30
31     AnyxmlEffectiveStatementImpl(final StmtContext<QName, AnyxmlStatement, AnyxmlEffectiveStatement> ctx) {
32         super(ctx);
33         this.original = (AnyxmlSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
34         mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
35                 .booleanValue();
36         mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
37     }
38
39     @Override
40     public boolean isMandatory() {
41         return mandatory;
42     }
43
44     @Override
45     public Collection<MustDefinition> getMustConstraints() {
46         return mustConstraints;
47     }
48
49     @Override
50     public Optional<AnyxmlSchemaNode> getOriginal() {
51         return Optional.ofNullable(original);
52     }
53
54     @Override
55     public int hashCode() {
56         final int prime = 31;
57         int result = 1;
58         result = prime * result + Objects.hashCode(getQName());
59         result = prime * result + Objects.hashCode(getPath());
60         return result;
61     }
62
63     @Override
64     public boolean equals(final Object obj) {
65         if (this == obj) {
66             return true;
67         }
68         if (obj == null) {
69             return false;
70         }
71         if (getClass() != obj.getClass()) {
72             return false;
73         }
74
75         AnyxmlEffectiveStatementImpl other = (AnyxmlEffectiveStatementImpl) obj;
76         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
77     }
78
79     @Override
80     public String toString() {
81         return AnyxmlEffectiveStatementImpl.class.getSimpleName() + "["
82                 + "qname=" + getQName()
83                 + ", path=" + getPath()
84                 + "]";
85     }
86 }