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