5ec07d04ef64b32874522bbb1f3151469272405f
[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  * Simple transfer object represents revision identifier of source for YANG
19  * schema (module or submodule), which consists of
20  * <ul>
21  * <li>YANG schema name ({@link #getName()}
22  * <li>Module revision (optional) ({link {@link #getRevision()})
23  * </ul>
24  *
25  * Revision source identifier is designated to be carry only necessary
26  * information to look-up YANG model source and to be used by various
27  * SchemaSourceProviders.
28  *
29  * <b>Note:</b>On source retrieval layer it is impossible to distinguish between
30  * YANG module and/or submodule unless source is present.
31  *
32  * <p>
33  * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2
34  * and http://tools.ietf.org/html/rfc6022#section-3.1 ).
35  */
36 @Beta
37 public final class RevisionSourceIdentifier extends SourceIdentifier {
38     private static final long serialVersionUID = 1L;
39
40     /**
41      *
42      * Creates new YANG Schema revision source identifier for sources without
43      * a revision.
44      *
45      * @param name
46      *            Name of schema
47      */
48     RevisionSourceIdentifier(final String name) {
49         super(name);
50     }
51
52     /**
53      * Creates new YANG Schema revision source identifier.
54      *
55      * @param name
56      *            Name of schema
57      * @param formattedRevision
58      *            Revision of source in format YYYY-mm-dd
59      */
60     RevisionSourceIdentifier(final String name, final String formattedRevision) {
61         super(Preconditions.checkNotNull(name), Preconditions.checkNotNull(formattedRevision));
62     }
63
64     /**
65      *
66      * Creates new YANG Schema revision source identifier.
67      *
68      * @param name
69      *            Name of schema
70      * @param formattedRevision
71      *            Revision of source in format YYYY-mm-dd. If not present,
72      *            default value will be used.
73      */
74     RevisionSourceIdentifier(final String name, final Optional<String> formattedRevision) {
75         super(name, formattedRevision);
76     }
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 }