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