Use Objects.equals() in effective statements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AugmentEffectiveStatementImpl.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 static com.google.common.base.Preconditions.checkNotNull;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.ImmutableList;
13 import java.net.URI;
14 import java.util.Collection;
15 import java.util.Date;
16 import java.util.Iterator;
17 import java.util.LinkedList;
18 import java.util.List;
19 import java.util.Objects;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
23 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
24 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
33
34 public class AugmentEffectiveStatementImpl
35         extends
36         AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
37         implements AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentEffectiveStatementImpl> {
38     private final SchemaPath targetPath;
39     private final URI namespace;
40     private final Date revision;
41     private final int order;
42     private ImmutableList<UnknownSchemaNode> unknownNodes;
43     private RevisionAwareXPath whenCondition;
44     private AugmentationSchema copyOf;
45
46     public AugmentEffectiveStatementImpl(
47             final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
48         super(ctx);
49
50         SchemaNodeIdentifier schemaNodeIdentifier = ctx.getStatementArgument();
51         this.targetPath = SchemaPath.create(
52                 schemaNodeIdentifier.getPathFromRoot(),
53                 schemaNodeIdentifier.isAbsolute());
54
55         QNameModule rootModuleQName = Utils.getRootModuleQName(ctx);
56         this.namespace = rootModuleQName.getNamespace();
57         this.revision = rootModuleQName.getRevision();
58
59         this.order = ctx.getOrder();
60
61         initCopyOf(ctx);
62         initSubstatementCollections();
63     }
64
65     private void initCopyOf(
66             final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
67         StatementContextBase<?, ?, ?> originalCtx = ctx.getOriginalCtx();
68         if (originalCtx != null) {
69             this.copyOf = (AugmentationSchema) originalCtx.buildEffective();
70         }
71     }
72
73     private void initSubstatementCollections() {
74         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
75
76         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
77
78         boolean initWhen = false;
79         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
80             if (effectiveStatement instanceof UnknownSchemaNode) {
81                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
82                 unknownNodesInit.add(unknownNode);
83             }
84             if(!initWhen && effectiveStatement instanceof WhenEffectiveStatementImpl) {
85                 WhenEffectiveStatementImpl whenStmt = (WhenEffectiveStatementImpl) effectiveStatement;
86                 whenCondition = whenStmt.argument();
87                 initWhen = true;
88             }
89         }
90
91         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
92     }
93
94     public void setCopyOf(final AugmentationSchema build) {
95         this.copyOf = build;
96     }
97
98     @Override
99     public Optional<AugmentationSchema> getOriginalDefinition() {
100         return Optional.fromNullable(this.copyOf);
101     }
102
103     @Override
104     public SchemaPath getTargetPath() {
105         return targetPath;
106     }
107
108     @Override
109     public RevisionAwareXPath getWhenCondition() {
110         return whenCondition;
111     }
112
113     @Override
114     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
115         return unknownNodes;
116     }
117
118     @Override
119     public URI getNamespace() {
120         return namespace;
121     }
122
123     @Override
124     public Date getRevision() {
125         return revision;
126     }
127
128     @Override
129     public int hashCode() {
130         final int prime = 17;
131         int result = 1;
132         result = prime * result + Objects.hashCode(targetPath);
133         result = prime * result + Objects.hashCode(whenCondition);
134         result = prime * result + getChildNodes().hashCode();
135         return result;
136     }
137
138     @Override
139     public boolean equals(final Object obj) {
140         if (this == obj) {
141             return true;
142         }
143         if (obj == null) {
144             return false;
145         }
146         if (getClass() != obj.getClass()) {
147             return false;
148         }
149         AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
150         if (!Objects.equals(targetPath, other.targetPath)) {
151             return false;
152         }
153         if (!Objects.equals(whenCondition, other.whenCondition)) {
154             return false;
155         }
156         if (!getChildNodes().equals(other.getChildNodes())) {
157             return false;
158         }
159         return true;
160     }
161
162     @Override
163     public String toString() {
164         StringBuilder sb = new StringBuilder(
165                 AugmentEffectiveStatementImpl.class.getSimpleName());
166         sb.append("[");
167         sb.append("targetPath=").append(targetPath);
168         sb.append(", when=").append(whenCondition);
169         sb.append("]");
170         return sb.toString();
171     }
172
173     @Override
174     public int compareTo(final AugmentEffectiveStatementImpl o) {
175         checkNotNull(o);
176         Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
177         Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
178         while (thisIt.hasNext()) {
179             if (otherIt.hasNext()) {
180                 int comp = thisIt.next().compareTo(otherIt.next());
181                 if (comp != 0) {
182                     return comp;
183                 }
184             } else {
185                 return 1;
186             }
187         }
188         if (otherIt.hasNext()) {
189             return -1;
190         }
191         return this.order - o.order;
192     }
193 }