YANGTOOLS-706: Split up base utility classes into rfc6020.util
[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         DescriptionEffectiveStatement descriptionStmt = firstEffective(DescriptionEffectiveStatement.class);
45         this.description = descriptionStmt == null ? null : descriptionStmt.argument();
46
47         ReferenceEffectiveStatement referenceStmt = firstEffective(ReferenceEffectiveStatement.class);
48         this.reference = referenceStmt == null ? null : referenceStmt.argument();
49
50         List<UnknownSchemaNode> unknownSchemaNodesInit = new ArrayList<>();
51         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
52             if (effectiveStatement instanceof UnknownSchemaNode) {
53                 unknownSchemaNodesInit.add((UnknownSchemaNode) effectiveStatement);
54             }
55         }
56         unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodesInit);
57     }
58
59     @Override
60     public SchemaPath getTargetPath() {
61         return targetPath;
62     }
63
64     @Override
65     public List<DeviateDefinition> getDeviates() {
66         return deviateDefinitions;
67     }
68
69     @Override
70     public Optional<String> getDescription() {
71         return Optional.ofNullable(description);
72     }
73
74     @Override
75     public Optional<String> getReference() {
76         return Optional.ofNullable(reference);
77     }
78
79     @Override
80     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
81         return unknownSchemaNodes;
82     }
83
84     @Override
85     public int hashCode() {
86         final int prime = 31;
87         int result = 1;
88         result = prime * result + Objects.hashCode(targetPath);
89         result = prime * result + Objects.hashCode(deviateDefinitions);
90         result = prime * result + Objects.hashCode(description);
91         result = prime * result + Objects.hashCode(reference);
92         return result;
93     }
94
95     @Override
96     public boolean equals(final Object obj) {
97         if (this == obj) {
98             return true;
99         }
100         if (obj == null) {
101             return false;
102         }
103         if (getClass() != obj.getClass()) {
104             return false;
105         }
106         DeviationEffectiveStatementImpl other = (DeviationEffectiveStatementImpl) obj;
107         if (!Objects.equals(targetPath, other.targetPath)) {
108             return false;
109         }
110         if (!Objects.equals(deviateDefinitions, other.deviateDefinitions)) {
111             return false;
112         }
113         if (!Objects.equals(description, other.description)) {
114             return false;
115         }
116         if (!Objects.equals(reference, other.reference)) {
117             return false;
118         }
119         return true;
120     }
121
122     @Override
123     public String toString() {
124         return DeviationEffectiveStatementImpl.class.getSimpleName() + "["
125                 + "targetPath=" + targetPath
126                 + ", deviates=" + deviateDefinitions
127                 + ", description=" + description
128                 + ", reference=" + reference
129                 + "]";
130     }
131 }