BUG-1794: use QName.cachedReference in static references
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / SourceIdentifier.java
1 /*
2  * Copyright (c) 2014 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/eplv10.html
7  */
8 package org.opendaylight.yangtools.yang.model.repo.api;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13
14 import org.opendaylight.yangtools.concepts.Identifier;
15 import org.opendaylight.yangtools.concepts.Immutable;
16
17 /**
18  * YANG Schema source identifier
19  *
20  * Simple transfer object represents identifier of source for YANG schema (module or submodule),
21  * which consists of
22  * <ul>
23  * <li>YANG schema name ({@link #getName()}
24  * <li>Module revision (optional) ({link {@link #getRevision()})
25  * </ul>
26  *
27  * Source identifier is designated to be carry only necessary information
28  * to look-up YANG model source and to be used by {@link AdvancedSchemaSourceProvider}
29  * and similar.
30  *
31  * <b>Note:</b>On source retrieval layer it is impossible to distinguish
32  * between YANG module and/or submodule unless source is present.
33  *
34  * <p>
35  * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2 and
36  * http://tools.ietf.org/html/rfc6022#section-3.1 ).
37  */
38 @Beta
39 public final class SourceIdentifier implements Identifier, Immutable {
40
41     /**
42      * Default revision for sources without specified revision.
43      * Marks the source as oldest.
44      */
45     public static final String NOT_PRESENT_FORMATTED_REVISION = "0000-00-00";
46
47     private static final long serialVersionUID = 1L;
48     private final String revision;
49     private final String name;
50
51     /**
52      *
53      * Creates new YANG Schema source identifier.
54      *
55      * @param name Name of schema
56      * @param formattedRevision Revision of source in format YYYY-mm-dd
57      */
58     public SourceIdentifier(final String name, final String formattedRevision) {
59         super();
60         this.name = Preconditions.checkNotNull(name);
61         this.revision = Preconditions.checkNotNull(formattedRevision);
62     }
63
64     /**
65      *
66      * Creates new YANG Schema source identifier.
67      *
68      * @param name Name of schema
69      * @param formattedRevision Revision of source in format YYYY-mm-dd. If not present, default value will be used.
70      */
71     public SourceIdentifier(final String name, final Optional<String> formattedRevision) {
72         this(name, formattedRevision.or(NOT_PRESENT_FORMATTED_REVISION));
73     }
74
75     /**
76      *
77      * Creates new YANG Schema source identifier for sources without revision.
78      * {@link SourceIdentifier#NOT_PRESENT_FORMATTED_REVISION} as default revision.
79      *
80      * @param name Name of schema
81      */
82     public SourceIdentifier(final String name) {
83         this(name, NOT_PRESENT_FORMATTED_REVISION);
84     }
85
86
87     /**
88      * Returns model name
89      *
90      * @return model name
91      */
92     public String getName() {
93         return name;
94     }
95
96     /**
97      * Returns revision of source or null if revision was not supplied.
98      *
99      * @return revision of source or null if revision was not supplied.
100      */
101     public String getRevision() {
102         return revision;
103     }
104
105     @Override
106     public int hashCode() {
107         final int prime = 31;
108         int result = 1;
109         result = prime * result + ((name == null) ? 0 : name.hashCode());
110         result = prime * result + ((revision == null) ? 0 : revision.hashCode());
111         return result;
112     }
113
114     @Override
115     public boolean equals(final Object obj) {
116         if (this == obj) {
117             return true;
118         }
119         if (obj == null) {
120             return false;
121         }
122         if (getClass() != obj.getClass()) {
123             return false;
124         }
125         SourceIdentifier other = (SourceIdentifier) obj;
126         if (name == null) {
127             if (other.name != null) {
128                 return false;
129             }
130         } else if (!name.equals(other.name)) {
131             return false;
132         }
133         if (revision == null) {
134             if (other.revision != null) {
135                 return false;
136             }
137         } else if (!revision.equals(other.revision)) {
138             return false;
139         }
140         return true;
141     }
142
143     public static SourceIdentifier create(final String moduleName, final Optional<String> revision) {
144         return new SourceIdentifier(moduleName, revision);
145     }
146
147     /**
148      * Returns filename for this YANG module as specified in RFC 6020.
149      *
150      * Returns filename in format
151      * <code>name ['@' revision] '.yang'</code>
152      * <p>
153      * Where revision is  date in format YYYY-mm-dd.
154      * <p>
155      *
156      * @see http://tools.ietf.org/html/rfc6020#section-5.2
157      *
158      * @return Filename for this source identifier.
159      */
160     public String toYangFilename() {
161         return toYangFileName(name, Optional.fromNullable(revision));
162     }
163
164     @Override
165     public String toString() {
166         return "SourceIdentifier [name=" + name + "@" + revision + "]";
167     }
168
169     /**
170      * Returns filename for this YANG module as specified in RFC 6020.
171      *
172      * Returns filename in format
173      * <code>moduleName ['@' revision] '.yang'</code>
174      *
175      * Where Where revision-date is in format YYYY-mm-dd.
176      *
177      * <p>
178      * See
179      * http://tools.ietf.org/html/rfc6020#section-5.2
180      *
181      * @return Filename for this source identifier.
182      */
183     public static final String toYangFileName(final String moduleName, final Optional<String> revision) {
184         StringBuilder filename = new StringBuilder(moduleName);
185         if (revision.isPresent()) {
186             filename.append('@');
187             filename.append(revision.get());
188         }
189         filename.append(".yang");
190         return filename.toString();
191     }
192
193 }