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