Remove RevisionSourceIdentifier
[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 org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.concepts.Identifier;
15 import org.opendaylight.yangtools.yang.common.Revision;
16 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
17 import org.opendaylight.yangtools.yang.common.YangConstants;
18
19 /**
20  * Base class of YANG Schema source identifiers. Source identifiers are designated to be carry only necessary
21  * information to look up YANG module (or submodule) source and to be used by various SchemaSourceProviders.
22  *
23  * <p>
24  * For further reference see: <a href="https://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
25  * and <a href="https://tools.ietf.org/html/rfc6022#section-3.1">RFC6022</a>.
26  */
27 public record SourceIdentifier(@NonNull Unqualified name, @Nullable Revision revision) implements Identifier {
28     private static final long serialVersionUID = 3L;
29
30     /**
31      * Creates new YANG Schema source identifier for sources with or without a revision.
32      *
33      * @param name Name of schema
34      * @param revision Revision of schema
35      * @throws NullPointerException if {@code name} is null
36      */
37     public SourceIdentifier {
38         requireNonNull(name);
39     }
40
41     /**
42      * Creates new YANG Schema source identifier for sources without a revision.
43      *
44      * @param name Name of schema
45      * @throws NullPointerException if {@code name} is null
46      */
47     public SourceIdentifier(final @NonNull Unqualified name) {
48         this(name, null);
49     }
50
51     /**
52      * Creates new YANG Schema source identifier for sources without a revision.
53      *
54      * @param name Name of schema
55      * @throws NullPointerException if {@code name} is null
56      * @throws IllegalArgumentException if {@code name} is not a valid YANG identifier
57      */
58     public SourceIdentifier(final @NonNull String name) {
59         this(Unqualified.of(name));
60     }
61
62     /**
63      * Creates new YANG Schema source identifier for sources with or without a revision.
64      *
65      * @param name Name of schema
66      * @throws NullPointerException if {@code name} is null
67      * @throws IllegalArgumentException if {@code name} is not a valid YANG identifier
68      */
69     public SourceIdentifier(final @NonNull String name, final @Nullable String revision) {
70         this(Unqualified.of(name), revision != null ? Revision.of(revision) : null);
71     }
72
73     /**
74      * Returns filename for this YANG module as specified in RFC 6020.
75      *
76      * <p>
77      * Returns filename in format <code>name ['@' revision] '.yang'</code>, where revision is date in format YYYY-mm-dd.
78      *
79      * <p>
80      * @see <a href="http://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
81      *
82      * @return Filename for this source identifier.
83      */
84     public @NonNull String toYangFilename() {
85         return toYangFileName(name.getLocalName(), revision);
86     }
87
88     @Override
89     public String toString() {
90         final var sb = new StringBuilder("SourceIdentifier [").append(name.getLocalName());
91         if (revision != null) {
92             sb.append('@').append(revision);
93         }
94         return sb.append(']').toString();
95     }
96
97     /**
98      * Returns filename for this YANG module as specified in RFC 6020.
99      *
100      * <p>
101      * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>,
102      * where Where revision-date is in format YYYY-mm-dd.
103      *
104      * <p>
105      * See http://tools.ietf.org/html/rfc6020#section-5.2
106      *
107      * @param moduleName module name
108      * @param revision optional revision
109      * @return Filename for this source identifier.
110      */
111     public static @NonNull String toYangFileName(final @NonNull String moduleName, final @Nullable Revision revision) {
112         final StringBuilder sb = new StringBuilder(moduleName);
113         if (revision != null) {
114             sb.append('@').append(revision);
115         }
116         return sb.append(YangConstants.RFC6020_YANG_FILE_EXTENSION).toString();
117     }
118 }