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