Merge "Bug 2480: Union objects are generated incorrectly when using bits type"
[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 import org.opendaylight.yangtools.objcache.ObjectCache;
17 import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
18
19 /**
20  * YANG Schema source identifier
21  *
22  * Simple transfer object represents identifier of source for YANG schema (module or submodule),
23  * which consists of
24  * <ul>
25  * <li>YANG schema name ({@link #getName()}
26  * <li>Module revision (optional) ({link {@link #getRevision()})
27  * </ul>
28  *
29  * Source identifier is designated to be carry only necessary information
30  * to look-up YANG model source and to be used by various SchemaSourceProviders.
31  *
32  * <b>Note:</b>On source retrieval layer it is impossible to distinguish
33  * between YANG module and/or submodule unless source is present.
34  *
35  * <p>
36  * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2 and
37  * http://tools.ietf.org/html/rfc6022#section-3.1 ).
38  */
39 @Beta
40 public final class SourceIdentifier implements Identifier, Immutable {
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 ObjectCache CACHE = ObjectCacheFactory.getObjectCache(SourceIdentifier.class);
48     private static final long serialVersionUID = 1L;
49     private final String revision;
50     private final String name;
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         this.name = Preconditions.checkNotNull(name);
60         this.revision = Preconditions.checkNotNull(formattedRevision);
61     }
62
63     /**
64      *
65      * Creates new YANG Schema source identifier.
66      *
67      * @param name Name of schema
68      * @param formattedRevision Revision of source in format YYYY-mm-dd. If not present, default value will be used.
69      */
70     public SourceIdentifier(final String name, final Optional<String> formattedRevision) {
71         this(name, formattedRevision.or(NOT_PRESENT_FORMATTED_REVISION));
72     }
73
74     /**
75      * Return a cached reference to an object equal to this object.
76      *
77      * @return A potentially shared reference, not guaranteed to be unique.
78      */
79     public SourceIdentifier cachedReference() {
80         return CACHE.getReference(this);
81     }
82
83     /**
84      *
85      * Creates new YANG Schema source identifier for sources without revision.
86      * {@link SourceIdentifier#NOT_PRESENT_FORMATTED_REVISION} as default revision.
87      *
88      * @param name Name of schema
89      */
90     public SourceIdentifier(final String name) {
91         this(name, NOT_PRESENT_FORMATTED_REVISION);
92     }
93
94     /**
95      * Returns model name
96      *
97      * @return model name
98      */
99     public String getName() {
100         return name;
101     }
102
103     /**
104      * Returns revision of source or null if revision was not supplied.
105      *
106      * @return revision of source or null if revision was not supplied.
107      */
108     public String getRevision() {
109         return revision;
110     }
111
112     @Override
113     public int hashCode() {
114         final int prime = 31;
115         int result = 1;
116         result = prime * result + ((name == null) ? 0 : name.hashCode());
117         result = prime * result + ((revision == null) ? 0 : revision.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         SourceIdentifier other = (SourceIdentifier) obj;
133         if (name == null) {
134             if (other.name != null) {
135                 return false;
136             }
137         } else if (!name.equals(other.name)) {
138             return false;
139         }
140         if (revision == null) {
141             if (other.revision != null) {
142                 return false;
143             }
144         } else if (!revision.equals(other.revision)) {
145             return false;
146         }
147         return true;
148     }
149
150     public static SourceIdentifier create(final String moduleName, final Optional<String> revision) {
151         return new SourceIdentifier(moduleName, revision);
152     }
153
154     /**
155      * Returns filename for this YANG module as specified in RFC 6020.
156      *
157      * Returns filename in format
158      * <code>name ['@' revision] '.yang'</code>
159      * <p>
160      * Where revision is  date in format YYYY-mm-dd.
161      * <p>
162      *
163      * @see <a href="http://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
164      *
165      * @return Filename for this source identifier.
166      */
167     public String toYangFilename() {
168         return toYangFileName(name, Optional.fromNullable(revision));
169     }
170
171     @Override
172     public String toString() {
173         return "SourceIdentifier [name=" + name + "@" + revision + "]";
174     }
175
176     /**
177      * Returns filename for this YANG module as specified in RFC 6020.
178      *
179      * Returns filename in format
180      * <code>moduleName ['@' revision] '.yang'</code>
181      *
182      * Where Where revision-date is in format YYYY-mm-dd.
183      *
184      * <p>
185      * See
186      * http://tools.ietf.org/html/rfc6020#section-5.2
187      *
188      * @return Filename for this source identifier.
189      */
190     public static final String toYangFileName(final String moduleName, final Optional<String> revision) {
191         StringBuilder filename = new StringBuilder(moduleName);
192         if (revision.isPresent()) {
193             filename.append('@');
194             filename.append(revision.get());
195         }
196         filename.append(".yang");
197         return filename.toString();
198     }
199
200 }