Centralize must contraint definitions
[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 java.util.Objects;
11 import java.util.Optional;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
18 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveMustConstraintAwareDataSchemaNode;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20
21 /**
22  * Internal implementation of AnyxmlEffectiveStatement.
23  *
24  * @deprecated This class is visible only for historical purposes and is going to be hidden.
25  */
26 // FIXME: 4.0.0: hide this class and make it final
27 @Deprecated
28 public class AnyxmlEffectiveStatementImpl extends AbstractEffectiveMustConstraintAwareDataSchemaNode<AnyxmlStatement>
29         implements AnyxmlEffectiveStatement, AnyXmlSchemaNode, DerivableSchemaNode {
30
31     private final AnyXmlSchemaNode original;
32     private final boolean mandatory;
33
34     AnyxmlEffectiveStatementImpl(final StmtContext<QName, AnyxmlStatement, AnyxmlEffectiveStatement> ctx) {
35         super(ctx);
36         this.original = (AnyXmlSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
37         mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
38                 .booleanValue();
39     }
40
41     @Override
42     public boolean isMandatory() {
43         return mandatory;
44     }
45
46     @Override
47     public Optional<AnyXmlSchemaNode> getOriginal() {
48         return Optional.ofNullable(original);
49     }
50
51     @Override
52     public int hashCode() {
53         final int prime = 31;
54         int result = 1;
55         result = prime * result + Objects.hashCode(getQName());
56         result = prime * result + Objects.hashCode(getPath());
57         return result;
58     }
59
60     @Override
61     public boolean equals(final Object obj) {
62         if (this == obj) {
63             return true;
64         }
65         if (obj == null) {
66             return false;
67         }
68         if (getClass() != obj.getClass()) {
69             return false;
70         }
71
72         AnyxmlEffectiveStatementImpl other = (AnyxmlEffectiveStatementImpl) obj;
73         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
74     }
75
76     @Override
77     public String toString() {
78         return AnyxmlEffectiveStatementImpl.class.getSimpleName() + "["
79                 + "qname=" + getQName()
80                 + ", path=" + getPath()
81                 + "]";
82     }
83 }