d2086a5b07f15e58955997c95f08b9330000bb61
[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.ArrayList;
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.DeviateDefinition;
16 import org.opendaylight.yangtools.yang.model.api.Deviation;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23
24 public class DeviationEffectiveStatementImpl
25         extends DeclaredEffectiveStatementBase<SchemaNodeIdentifier, DeviationStatement>
26         implements Deviation, Immutable {
27     private final SchemaPath targetPath;
28     private final String description;
29     private final String reference;
30     private final List<UnknownSchemaNode> unknownSchemaNodes;
31     private final List<DeviateDefinition> deviateDefinitions;
32
33     public DeviationEffectiveStatementImpl(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
34         super(ctx);
35         this.targetPath = ctx.getStatementArgument().asSchemaPath();
36
37         this.deviateDefinitions = ImmutableList.copyOf(allSubstatementsOfType(DeviateDefinition.class));
38
39         DescriptionEffectiveStatementImpl descriptionStmt = firstEffective(DescriptionEffectiveStatementImpl.class);
40         this.description = descriptionStmt == null ? null : descriptionStmt.argument();
41
42         ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
43         this.reference = referenceStmt == null ? null : referenceStmt.argument();
44
45         List<UnknownSchemaNode> unknownSchemaNodesInit = new ArrayList<>();
46         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
47             if (effectiveStatement instanceof UnknownSchemaNode) {
48                 unknownSchemaNodesInit.add((UnknownSchemaNode) effectiveStatement);
49             }
50         }
51         unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodesInit);
52     }
53
54     @Override
55     public SchemaPath getTargetPath() {
56         return targetPath;
57     }
58
59     @Override
60     public List<DeviateDefinition> getDeviates() {
61         return deviateDefinitions;
62     }
63
64     @Override
65     public String getDescription() {
66         return description;
67     }
68
69     @Override
70     public String getReference() {
71         return reference;
72     }
73
74     @Override
75     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
76         return unknownSchemaNodes;
77     }
78
79     @Override
80     public int hashCode() {
81         final int prime = 31;
82         int result = 1;
83         result = prime * result + Objects.hashCode(targetPath);
84         result = prime * result + Objects.hashCode(deviateDefinitions);
85         result = prime * result + Objects.hashCode(description);
86         result = prime * result + Objects.hashCode(reference);
87         return result;
88     }
89
90     @Override
91     public boolean equals(final Object obj) {
92         if (this == obj) {
93             return true;
94         }
95         if (obj == null) {
96             return false;
97         }
98         if (getClass() != obj.getClass()) {
99             return false;
100         }
101         DeviationEffectiveStatementImpl other = (DeviationEffectiveStatementImpl) obj;
102         if (!Objects.equals(targetPath, other.targetPath)) {
103             return false;
104         }
105         if (!Objects.equals(deviateDefinitions, other.deviateDefinitions)) {
106             return false;
107         }
108         if (!Objects.equals(description, other.description)) {
109             return false;
110         }
111         if (!Objects.equals(reference, other.reference)) {
112             return false;
113         }
114         return true;
115     }
116
117     @Override
118     public String toString() {
119         return DeviationEffectiveStatementImpl.class.getSimpleName() + "["
120                 + "targetPath=" + targetPath
121                 + ", deviates=" + deviateDefinitions
122                 + ", description=" + description
123                 + ", reference=" + reference
124                 + "]";
125     }
126 }