BUG-4688: Make SourceIdentifier use Revision
[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 import javax.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.common.Revision;
17
18 /**
19  * YANG Schema revision source identifier.
20  *
21  * <p>
22  * Simple transfer object represents revision identifier of source for YANG
23  * schema (module or submodule), which consists 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
31  * information to look-up YANG model source and to be used by various
32  * SchemaSourceProviders.
33  *
34  * <p>
35  * <b>Note:</b>On source retrieval layer it is impossible to distinguish between
36  * YANG module and/or submodule unless source is present.
37  *
38  * <p>
39  * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2
40  * and http://tools.ietf.org/html/rfc6022#section-3.1 ).
41  */
42 @Beta
43 public final class RevisionSourceIdentifier extends SourceIdentifier {
44     private static final long serialVersionUID = 1L;
45
46     /**
47      * Creates new YANG Schema revision source identifier for sources without
48      * a revision.
49      *
50      * @param name
51      *            Name of schema
52      */
53     RevisionSourceIdentifier(final String name) {
54         super(name);
55     }
56
57     /**
58      * Creates new YANG Schema revision source identifier.
59      *
60      * @param name
61      *            Name of schema
62      * @param revision
63      *            Revision of source, may be null
64      */
65     RevisionSourceIdentifier(final String name, @Nullable final Revision revision) {
66         super(requireNonNull(name), revision);
67     }
68
69     /**
70      * Creates new YANG Schema revision source identifier.
71      *
72      * @param name
73      *            Name of schema
74      * @param formattedRevision
75      *            Revision of source, potentially not present
76      */
77     private RevisionSourceIdentifier(final String name, final Optional<Revision> revision) {
78         super(name, revision);
79     }
80
81     /**
82      * Creates new YANG Schema revision source identifier.
83      *
84      * @param moduleName
85      *            Name of schema
86      * @param revision
87      *            Revision of source in format YYYY-mm-dd. If not present,
88      *            default value will be used.
89      */
90     public static RevisionSourceIdentifier create(final String moduleName, final Optional<Revision> 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, may be null
101      */
102     public static RevisionSourceIdentifier create(final String moduleName, @Nullable final Revision 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 Optional<Revision> rev = getRevision();
144         if (rev.isPresent()) {
145             sb.append('@').append(rev.get());
146         }
147         return sb.append(']').toString();
148     }
149 }