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