Merge "Cleanup warnings in pom.xml"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / AugmentationSchemaImpl.java
1 /*
2  * Copyright (c) 2013 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.builder.impl;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
12 import com.google.common.base.Optional;
13 import com.google.common.collect.ImmutableList;
14 import java.net.URI;
15 import java.util.Date;
16 import java.util.Iterator;
17 import java.util.List;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
21 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
24 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
25
26 final class AugmentationSchemaImpl extends AbstractDocumentedDataNodeContainer implements AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentationSchemaImpl> {
27     private final int order;
28     private final SchemaPath targetPath;
29     RevisionAwareXPath whenCondition;
30
31     URI namespace;
32     Date revision;
33     ImmutableList<UnknownSchemaNode> unknownNodes;
34     private AugmentationSchema copyOf;
35
36     public AugmentationSchemaImpl(final SchemaPath targetPath, final int order, final AugmentationSchemaBuilderImpl builder) {
37         super(builder);
38         this.targetPath = targetPath;
39         this.order = order;
40     }
41
42     public void setCopyOf(final AugmentationSchema build) {
43         this.copyOf = build;
44     }
45
46     @Override
47     public Optional<AugmentationSchema> getOriginalDefinition() {
48         return Optional.fromNullable(this.copyOf);
49     }
50
51     @Override
52     public SchemaPath getTargetPath() {
53         return targetPath;
54     }
55
56     @Override
57     public RevisionAwareXPath getWhenCondition() {
58         return whenCondition;
59     }
60
61     @Override
62     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
63         return unknownNodes;
64     }
65
66     @Override
67     public URI getNamespace() {
68         return namespace;
69     }
70
71     @Override
72     public Date getRevision() {
73         return revision;
74     }
75
76     @Override
77     public int hashCode() {
78         final int prime = 17;
79         int result = 1;
80         result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
81         result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
82         result = prime * result + getChildNodes().hashCode();
83         return result;
84     }
85
86     @Override
87     public boolean equals(final Object obj) {
88         if (this == obj) {
89             return true;
90         }
91         if (obj == null) {
92             return false;
93         }
94         if (getClass() != obj.getClass()) {
95             return false;
96         }
97         AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
98         if (targetPath == null) {
99             if (other.targetPath != null) {
100                 return false;
101             }
102         } else if (!targetPath.equals(other.targetPath)) {
103             return false;
104         }
105         if (whenCondition == null) {
106             if (other.whenCondition != null) {
107                 return false;
108             }
109         } else if (!whenCondition.equals(other.whenCondition)) {
110             return false;
111         }
112         if (!getChildNodes().equals(other.getChildNodes())) {
113             return false;
114         }
115         return true;
116     }
117
118     @Override
119     public String toString() {
120         StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
121         sb.append("[");
122         sb.append("targetPath=").append(targetPath);
123         sb.append(", when=").append(whenCondition);
124         sb.append("]");
125         return sb.toString();
126     }
127
128     @Override
129     public int compareTo(final AugmentationSchemaImpl o) {
130         checkNotNull(o);
131         Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
132         Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
133         while (thisIt.hasNext()) {
134             if (otherIt.hasNext()) {
135                 int comp = thisIt.next().compareTo(otherIt.next());
136                 if (comp != 0) {
137                     return comp;
138                 }
139             } else {
140                 return 1;
141             }
142         }
143         if (otherIt.hasNext()) {
144             return -1;
145         }
146         return this.order - o.order;
147     }
148 }