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