2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.repo.api;
10 import static java.util.Objects.requireNonNull;
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;
23 * Base class of YANG Schema source identifiers.
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.
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 ).
34 public abstract class SourceIdentifier implements Identifier, Immutable {
35 private static final Interner<SourceIdentifier> INTERNER = Interners.newWeakInterner();
36 private static final long serialVersionUID = 2L;
38 private final Revision revision;
39 private final String name;
42 * Creates new YANG Schema source identifier for sources without revision.
47 SourceIdentifier(final String name) {
48 this(name, (Revision) null);
52 * Creates new YANG Schema source identifier.
57 * Revision of source, may be null
59 SourceIdentifier(final String name, @Nullable final Revision revision) {
60 this.name = requireNonNull(name);
61 this.revision = revision;
65 * Creates new YANG Schema source identifier.
70 * Revision of source, possibly not present
72 SourceIdentifier(final String name, final Optional<Revision> revision) {
73 this(name, revision.orElse(null));
77 * Return an interned reference to a equivalent SemVerSourceIdentifier.
79 * @return Interned reference, or this object if it was interned.
81 public SourceIdentifier intern() {
82 return INTERNER.intern(this);
90 public String getName() {
95 * Returns revision of source or null if revision was not supplied.
97 * @return revision of source or null if revision was not supplied.
99 public Optional<Revision> getRevision() {
100 return Optional.ofNullable(revision);
104 * Returns filename for this YANG module as specified in RFC 6020.
107 * Returns filename in format <code>name ['@' revision] '.yang'</code>,
108 * where revision is date in format YYYY-mm-dd.
111 * @see <a href="http://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
113 * @return Filename for this source identifier.
115 public String toYangFilename() {
116 return toYangFileName(name, Optional.ofNullable(revision));
120 * Returns filename for this YANG module as specified in RFC 6020.
123 * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>,
124 * where Where revision-date is in format YYYY-mm-dd.
127 * See http://tools.ietf.org/html/rfc6020#section-5.2
129 * @return Filename for this source identifier.
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());
137 filename.append(YangConstants.RFC6020_YANG_FILE_EXTENSION);
138 return filename.toString();