7d22a2fcb0f4ff5568ff3fe97f2dfe8366ad70f3
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.Interner;
14 import com.google.common.collect.Interners;
15 import java.util.Optional;
16 import javax.annotation.Nullable;
17 import org.opendaylight.yangtools.concepts.Identifier;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.yang.common.Revision;
20 import org.opendaylight.yangtools.yang.common.YangConstants;
21
22 /**
23  * Base class of YANG Schema source identifiers.
24  *
25  * <p>
26  * Source identifiers are designated to be carry only necessary information to
27  * look-up YANG model source and to be used by various SchemaSourceProviders.
28  *
29  * <p>
30  * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2
31  * and http://tools.ietf.org/html/rfc6022#section-3.1 ).
32  */
33 @Beta
34 public abstract class SourceIdentifier implements Identifier, Immutable {
35     private static final Interner<SourceIdentifier> INTERNER = Interners.newWeakInterner();
36     private static final long serialVersionUID = 2L;
37
38     private final Revision revision;
39     private final String name;
40
41     /**
42      * Creates new YANG Schema source identifier for sources without revision.
43      *
44      * @param name
45      *            Name of schema
46      */
47     SourceIdentifier(final String name) {
48         this(name, (Revision) null);
49     }
50
51     /**
52      * Creates new YANG Schema source identifier.
53      *
54      * @param name
55      *            Name of schema
56      * @param revision
57      *            Revision of source, may be null
58      */
59     SourceIdentifier(final String name, @Nullable final Revision revision) {
60         this.name = requireNonNull(name);
61         this.revision = revision;
62     }
63
64     /**
65      * Creates new YANG Schema source identifier.
66      *
67      * @param name
68      *            Name of schema
69      * @param revision
70      *            Revision of source, possibly not present
71      */
72     SourceIdentifier(final String name, final Optional<Revision> revision) {
73         this(name, revision.orElse(null));
74     }
75
76     /**
77      * Return an interned reference to a equivalent SemVerSourceIdentifier.
78      *
79      * @return Interned reference, or this object if it was interned.
80      */
81     public SourceIdentifier intern() {
82         return INTERNER.intern(this);
83     }
84
85     /**
86      * Returns model name.
87      *
88      * @return model name
89      */
90     public String getName() {
91         return name;
92     }
93
94     /**
95      * Returns revision of source or null if revision was not supplied.
96      *
97      * @return revision of source or null if revision was not supplied.
98      */
99     public Optional<Revision> getRevision() {
100         return Optional.ofNullable(revision);
101     }
102
103     /**
104      * Returns filename for this YANG module as specified in RFC 6020.
105      *
106      * <p>
107      * Returns filename in format <code>name ['@' revision] '.yang'</code>,
108      * where revision is date in format YYYY-mm-dd.
109      *
110      * <p>
111      * @see <a href="http://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
112      *
113      * @return Filename for this source identifier.
114      */
115     public String toYangFilename() {
116         return toYangFileName(name, Optional.ofNullable(revision));
117     }
118
119     /**
120      * Returns filename for this YANG module as specified in RFC 6020.
121      *
122      * <p>
123      * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>,
124      * where Where revision-date is in format YYYY-mm-dd.
125      *
126      * <p>
127      * See http://tools.ietf.org/html/rfc6020#section-5.2
128      *
129      * @return Filename for this source identifier.
130      */
131     public static String toYangFileName(final String moduleName, final Optional<Revision> revision) {
132         StringBuilder filename = new StringBuilder(moduleName);
133         if (revision.isPresent()) {
134             filename.append('@');
135             filename.append(revision.get());
136         }
137         filename.append(YangConstants.RFC6020_YANG_FILE_EXTENSION);
138         return filename.toString();
139     }
140 }