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