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