02869cbc4ee2f36e0f0a7e5dfc423180fcd0f1f1
[yangtools.git] / yang / yang-repo-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / RevisionSourceIdentifier.java
1 /*
2  * Copyright (c) 2016 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 java.util.Objects;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.common.Revision;
16 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
17
18 /**
19  * YANG Schema revision source identifier.
20  *
21  * <p>
22  * Simple transfer object represents revision identifier of source for YANG schema (module or submodule), which consists
23  * of
24  * <ul>
25  * <li>YANG schema name ({@link #getName()}
26  * <li>Module revision (optional) ({link {@link #getRevision()})
27  * </ul>
28  *
29  * <p>
30  * Revision source identifier is designated to be carry only necessary information to look-up YANG model source
31  * and to be used by various SchemaSourceProviders.
32  *
33  * <p>
34  * <b>Note:</b>On source retrieval layer it is impossible to distinguish between YANG module and/or submodule unless
35  * source is present.
36  *
37  * <p>
38  * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2
39  * and http://tools.ietf.org/html/rfc6022#section-3.1 ).
40  */
41 @Beta
42 public final class RevisionSourceIdentifier extends SourceIdentifier {
43     private static final long serialVersionUID = 1L;
44
45     /**
46      * Creates new YANG Schema revision source identifier for sources without a revision.
47      *
48      * @param name Name of schema
49      */
50     RevisionSourceIdentifier(final String name) {
51         super(name);
52     }
53
54     /**
55      * Creates new YANG Schema revision source identifier.
56      *
57      * @param name Name of schema
58      * @param revision Revision of source, may be null
59      */
60     RevisionSourceIdentifier(final String name, final @Nullable Revision revision) {
61         super(name, revision);
62     }
63
64     /**
65      * Creates new YANG Schema revision source identifier.
66      *
67      * @param name Name of schema
68      * @param formattedRevision Revision of source, potentially not present
69      */
70     private RevisionSourceIdentifier(final String name, final Optional<Revision> revision) {
71         super(name, revision);
72     }
73
74     /**
75      * Creates new YANG Schema revision source identifier.
76      *
77      * @param moduleName Name of schema
78      * @param revision Revision of source in format YYYY-mm-dd. If not present, default value will be used.
79      * @return A RevisionSourceIdentifier
80      */
81     public static @NonNull RevisionSourceIdentifier create(final String moduleName, final Optional<Revision> revision) {
82         return new RevisionSourceIdentifier(moduleName, revision);
83     }
84
85     public static @NonNull RevisionSourceIdentifier create(final Unqualified moduleName,
86             final Optional<Revision> revision) {
87         return create(moduleName.getLocalName(), revision);
88     }
89
90     /**
91      * Creates new YANG Schema revision source identifier.
92      *
93      * @param moduleName Name of schema
94      * @param revision Revision of source, may be null
95      * @return A RevisionSourceIdentifier
96      */
97     public static @NonNull RevisionSourceIdentifier create(final String moduleName, final @Nullable Revision revision) {
98         return new RevisionSourceIdentifier(moduleName, revision);
99     }
100
101     public static @NonNull RevisionSourceIdentifier create(final Unqualified moduleName,
102             final @Nullable Revision revision) {
103         return create(moduleName.getLocalName(), revision);
104     }
105
106     /**
107      * Creates new YANG Schema revision source identifier for sources without
108      * a revision.
109      *
110      * @param moduleName Name of schema
111      * @return A RevisionSourceIdentifier
112      */
113     public static @NonNull RevisionSourceIdentifier create(final String moduleName) {
114         return new RevisionSourceIdentifier(moduleName);
115     }
116
117     @Override
118     public int hashCode() {
119         final int prime = 31;
120         int result = 1;
121         result = prime * result + Objects.hashCode(getName());
122         result = prime * result + Objects.hashCode(getRevision());
123         return result;
124     }
125
126     @Override
127     public boolean equals(final Object obj) {
128         if (this == obj) {
129             return true;
130         }
131         if (!(obj instanceof RevisionSourceIdentifier other)) {
132             return false;
133         }
134         return Objects.equals(getName(), other.getName()) && Objects.equals(getRevision(), other.getRevision());
135     }
136
137     @Override
138     public String toString() {
139         final StringBuilder sb = new StringBuilder("RevisionSourceIdentifier [name=");
140         sb.append(getName());
141
142         final Optional<Revision> rev = getRevision();
143         if (rev.isPresent()) {
144             sb.append('@').append(rev.get());
145         }
146         return sb.append(']').toString();
147     }
148 }