Introduce SchemaNodeIdentifier.asSchemaPath()
[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
26     private final SchemaPath targetPath;
27     private Deviate deviate;
28     private String reference;
29     private final ImmutableList<UnknownSchemaNode> unknownSchemaNodes;
30
31     public DeviationEffectiveStatementImpl(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
32         super(ctx);
33
34         List<UnknownSchemaNode> unknownSchemaNodesInit = new LinkedList<>();
35
36         targetPath = ctx.getStatementArgument().asSchemaPath();
37
38         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
39             if (effectiveStatement instanceof DeviateEffectiveStatementImpl) {
40                 deviate = ((DeviateEffectiveStatementImpl) effectiveStatement).argument();
41             }
42             if (effectiveStatement instanceof ReferenceEffectiveStatementImpl) {
43                 reference = ((ReferenceEffectiveStatementImpl) effectiveStatement).argument();
44             }
45             if (effectiveStatement instanceof UnknownSchemaNode) {
46                 unknownSchemaNodesInit.add((UnknownSchemaNode) effectiveStatement);
47             }
48         }
49
50         unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodesInit);
51     }
52
53     @Override
54     public SchemaPath getTargetPath() {
55         return targetPath;
56     }
57
58     @Override
59     public Deviate getDeviate() {
60         return deviate;
61     }
62
63     @Override
64     public String getReference() {
65         return reference;
66     }
67
68     @Override
69     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
70         return unknownSchemaNodes;
71     }
72
73     @Override
74     public int hashCode() {
75         final int prime = 31;
76         int result = 1;
77         result = prime * result + Objects.hashCode(targetPath);
78         result = prime * result + Objects.hashCode(deviate);
79         result = prime * result + Objects.hashCode(reference);
80         return result;
81     }
82
83     @Override
84     public boolean equals(final Object obj) {
85         if (this == obj) {
86             return true;
87         }
88         if (obj == null) {
89             return false;
90         }
91         if (getClass() != obj.getClass()) {
92             return false;
93         }
94         DeviationEffectiveStatementImpl other = (DeviationEffectiveStatementImpl) obj;
95         if (!Objects.equals(targetPath, other.targetPath)) {
96             return false;
97         }
98         if (!Objects.equals(deviate, other.deviate)) {
99             return false;
100         }
101         if (!Objects.equals(reference, other.reference)) {
102             return false;
103         }
104         return true;
105     }
106
107     @Override
108     public String toString() {
109         StringBuilder sb = new StringBuilder(DeviationEffectiveStatementImpl.class.getSimpleName());
110         sb.append("[");
111         sb.append("targetPath=").append(targetPath);
112         sb.append(", deviate=").append(deviate);
113         sb.append(", reference=").append(reference);
114         sb.append("]");
115         return sb.toString();
116     }
117 }