69eafb5d6ceb3ba6d4216d7da18810f867d6b8e1
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / extension / UnrecognizedEffectiveStatementImpl.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.extension;
9
10 import java.util.Objects;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
14 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
17 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.UnknownEffectiveStatementBase;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
20
21 final class UnrecognizedEffectiveStatementImpl extends UnknownEffectiveStatementBase<String, UnrecognizedStatement>
22         implements UnrecognizedEffectiveStatement {
23
24     private final QName maybeQNameArgument;
25     private final SchemaPath path;
26
27     UnrecognizedEffectiveStatementImpl(final StmtContext<String, UnrecognizedStatement, ?> ctx) {
28         super(ctx);
29
30         // FIXME: Remove following section after fixing 4380
31         final UnknownSchemaNode original = (UnknownSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective)
32                 .orElse(null);
33         if (original != null) {
34             this.maybeQNameArgument = original.getQName();
35         } else {
36             QName maybeQNameArgumentInit = null;
37             try {
38                 maybeQNameArgumentInit = StmtContextUtils.qnameFromArgument(ctx, argument());
39             } catch (IllegalArgumentException e) {
40                 maybeQNameArgumentInit = getNodeType();
41             }
42             this.maybeQNameArgument = maybeQNameArgumentInit;
43         }
44         path = ctx.getParentContext().getSchemaPath().get().createChild(maybeQNameArgument);
45     }
46
47     @Nonnull
48     @Override
49     public QName getQName() {
50         return maybeQNameArgument;
51     }
52
53     @Nonnull
54     @Override
55     public SchemaPath getPath() {
56         return path;
57     }
58
59     @Override
60     public int hashCode() {
61         final int prime = 31;
62         int result = 1;
63         result = prime * result + Objects.hashCode(maybeQNameArgument);
64         result = prime * result + Objects.hashCode(path);
65         result = prime * result + Objects.hashCode(getNodeType());
66         result = prime * result + Objects.hashCode(getNodeParameter());
67         return result;
68     }
69
70     @Override
71     public boolean equals(final Object obj) {
72         if (this == obj) {
73             return true;
74         }
75         if (!(obj instanceof UnrecognizedEffectiveStatementImpl)) {
76             return false;
77         }
78         UnrecognizedEffectiveStatementImpl other = (UnrecognizedEffectiveStatementImpl) obj;
79         return Objects.equals(maybeQNameArgument, other.maybeQNameArgument) && Objects.equals(path, other.path)
80                 && Objects.equals(getNodeType(), other.getNodeType())
81                 && Objects.equals(getNodeParameter(), other.getNodeParameter());
82     }
83 }