Bug 4662: Introduce a SemanticVersion concept - SchemaContextFactory
[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/epl-v10.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 import com.google.common.collect.Interner;
14 import com.google.common.collect.Interners;
15 import java.util.regex.Pattern;
16 import org.opendaylight.yangtools.concepts.Identifier;
17 import org.opendaylight.yangtools.concepts.Immutable;
18 import org.opendaylight.yangtools.objcache.ObjectCache;
19 import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
20 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
21
22 /**
23  * Base class of YANG Schema source identifiers.
24  *
25  * Source identifiers are designated to be carry only necessary information to
26  * look-up YANG model source and to be used by various SchemaSourceProviders.
27  *
28  * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2
29  * and http://tools.ietf.org/html/rfc6022#section-3.1 ).
30  */
31 @Beta
32 public abstract class SourceIdentifier implements Identifier, Immutable {
33     /**
34      * Default revision for sources without specified revision. Marks the source
35      * as oldest.
36      */
37     public static final String NOT_PRESENT_FORMATTED_REVISION = "0000-00-00";
38
39     /**
40      *
41      * Simplified compiled revision pattern in format YYYY-mm-dd, which checks
42      * only distribution of number elements.
43      * <p>
44      * For checking if supplied string is real date, use
45      * {@link SimpleDateFormatUtil} instead.
46      *
47      */
48     public static final Pattern REVISION_PATTERN = Pattern.compile("\\d\\d\\d\\d-\\d\\d-\\d\\d");
49
50     private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(SourceIdentifier.class);
51     private static final Interner<SourceIdentifier> INTERNER = Interners.newWeakInterner();
52
53     private static final long serialVersionUID = 1L;
54     private final String revision;
55     private final String name;
56
57     /**
58      *
59      * Creates new YANG Schema source identifier for sources without revision.
60      * {@link SourceIdentifier#NOT_PRESENT_FORMATTED_REVISION} as default
61      * revision.
62      *
63      * @param name
64      *            Name of schema
65      */
66     SourceIdentifier(final String name) {
67         this(name, NOT_PRESENT_FORMATTED_REVISION);
68     }
69
70     /**
71      * Creates new YANG Schema source identifier.
72      *
73      * @param name
74      *            Name of schema
75      * @param formattedRevision
76      *            Revision of source in format YYYY-mm-dd
77      */
78     SourceIdentifier(final String name, final String formattedRevision) {
79         this.name = Preconditions.checkNotNull(name);
80         this.revision = Preconditions.checkNotNull(formattedRevision);
81     }
82
83     /**
84      *
85      * Creates new YANG Schema source identifier.
86      *
87      * @param name
88      *            Name of schema
89      * @param formattedRevision
90      *            Revision of source in format YYYY-mm-dd. If not present,
91      *            default value will be used.
92      */
93     SourceIdentifier(final String name, final Optional<String> formattedRevision) {
94         this(name, formattedRevision.or(NOT_PRESENT_FORMATTED_REVISION));
95     }
96
97     /**
98      * Return a cached reference to an object equal to this object.
99      *
100      * @return A potentially shared reference, not guaranteed to be unique.
101      */
102     @Deprecated
103     public SourceIdentifier cachedReference() {
104         return CACHE.getReference(this);
105     }
106
107     /**
108      * Return an interned reference to a equivalent SemVerSourceIdentifier.
109      *
110      * @return Interned reference, or this object if it was interned.
111      */
112     public SourceIdentifier intern() {
113         return INTERNER.intern(this);
114     }
115
116     /**
117      * Returns model name
118      *
119      * @return model name
120      */
121     public String getName() {
122         return name;
123     }
124
125     /**
126      * Returns revision of source or null if revision was not supplied.
127      *
128      * @return revision of source or null if revision was not supplied.
129      */
130     public String getRevision() {
131         return revision;
132     }
133
134     @Deprecated
135     public static SourceIdentifier create(final String moduleName, final Optional<String> revision) {
136         return new RevisionSourceIdentifier(moduleName, revision);
137     }
138
139     /**
140      * Returns filename for this YANG module as specified in RFC 6020.
141      *
142      * Returns filename in format <code>name ['@' revision] '.yang'</code>
143      * <p>
144      * Where revision is date in format YYYY-mm-dd.
145      * <p>
146      *
147      * @see <a href="http://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
148      *
149      * @return Filename for this source identifier.
150      */
151     public String toYangFilename() {
152         return toYangFileName(name, Optional.fromNullable(revision));
153     }
154
155     /**
156      * Returns filename for this YANG module as specified in RFC 6020.
157      *
158      * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>
159      *
160      * Where Where revision-date is in format YYYY-mm-dd.
161      *
162      * <p>
163      * See http://tools.ietf.org/html/rfc6020#section-5.2
164      *
165      * @return Filename for this source identifier.
166      */
167     public static String toYangFileName(final String moduleName, final Optional<String> revision) {
168         StringBuilder filename = new StringBuilder(moduleName);
169         if (revision.isPresent()) {
170             filename.append('@');
171             filename.append(revision.get());
172         }
173         filename.append(".yang");
174         return filename.toString();
175     }
176 }