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