Bug 4412: New yang parser effective statements cleanup
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / DeviationEffectiveStatementImpl.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.collect.ImmutableList;
11 import java.util.LinkedList;
12 import java.util.List;
13 import java.util.Objects;
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.yang.model.api.Deviation;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22
23 public class DeviationEffectiveStatementImpl extends EffectiveStatementBase<SchemaNodeIdentifier, DeviationStatement>
24         implements Deviation, Immutable {
25     private final SchemaPath targetPath;
26     private final Deviate deviate;
27     private final String reference;
28     private final List<UnknownSchemaNode> unknownSchemaNodes;
29
30     public DeviationEffectiveStatementImpl(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
31         super(ctx);
32         this.targetPath = ctx.getStatementArgument().asSchemaPath();
33
34         DeviateEffectiveStatementImpl deviateStmt = firstEffective(DeviateEffectiveStatementImpl.class);
35         this.deviate = (deviateStmt == null) ? null : deviateStmt.argument();
36
37         ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
38         this.reference = (referenceStmt == null) ? null : referenceStmt.argument();
39
40         List<UnknownSchemaNode> unknownSchemaNodesInit = new LinkedList<>();
41         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
42             if (effectiveStatement instanceof UnknownSchemaNode) {
43                 unknownSchemaNodesInit.add((UnknownSchemaNode) effectiveStatement);
44             }
45         }
46         unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodesInit);
47     }
48
49     @Override
50     public SchemaPath getTargetPath() {
51         return targetPath;
52     }
53
54     @Override
55     public Deviate getDeviate() {
56         return deviate;
57     }
58
59     @Override
60     public String getReference() {
61         return reference;
62     }
63
64     @Override
65     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
66         return unknownSchemaNodes;
67     }
68
69     @Override
70     public int hashCode() {
71         final int prime = 31;
72         int result = 1;
73         result = prime * result + Objects.hashCode(targetPath);
74         result = prime * result + Objects.hashCode(deviate);
75         result = prime * result + Objects.hashCode(reference);
76         return result;
77     }
78
79     @Override
80     public boolean equals(final Object obj) {
81         if (this == obj) {
82             return true;
83         }
84         if (obj == null) {
85             return false;
86         }
87         if (getClass() != obj.getClass()) {
88             return false;
89         }
90         DeviationEffectiveStatementImpl other = (DeviationEffectiveStatementImpl) obj;
91         if (!Objects.equals(targetPath, other.targetPath)) {
92             return false;
93         }
94         if (!Objects.equals(deviate, other.deviate)) {
95             return false;
96         }
97         if (!Objects.equals(reference, other.reference)) {
98             return false;
99         }
100         return true;
101     }
102
103     @Override
104     public String toString() {
105         StringBuilder sb = new StringBuilder(DeviationEffectiveStatementImpl.class.getSimpleName());
106         sb.append("[");
107         sb.append("targetPath=").append(targetPath);
108         sb.append(", deviate=").append(deviate);
109         sb.append(", reference=").append(reference);
110         sb.append("]");
111         return sb.toString();
112     }
113 }