BUG-4688: Make SourceIdentifier use Revision
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / SemVerSourceIdentifier.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.opendaylight.yangtools.concepts.SemVer;
14 import org.opendaylight.yangtools.yang.common.Revision;
15
16 /**
17  * YANG Schema source identifier with specified semantic version.
18  *
19  * <p>
20  * Simple transfer object represents identifier of source for YANG schema
21  * (module or submodule), which consists of
22  * <ul>
23  * <li>YANG schema name {@link #getName()}
24  * <li>Semantic version of yang schema {@link #getSemanticVersion()}
25  * <li>(Optional) Module revision ({link {@link #getRevision()}
26  * </ul>
27  *
28  * <p>
29  * Source identifier is designated to be carry only necessary information to
30  * look-up YANG model source and to be used by various 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 SemVerSourceIdentifier extends SourceIdentifier {
42     private static final long serialVersionUID = 1L;
43     private final SemVer semVer;
44
45     /**
46      * Creates new YANG Schema semVer source identifier.
47      *
48      * @param name
49      *            Name of schema
50      * @param revision
51      *            Revision of source, possibly not present
52      * @param semVer
53      *            semantic version of source
54      */
55     SemVerSourceIdentifier(final String name, final Optional<Revision> revision, final SemVer semVer) {
56         super(name, revision);
57         this.semVer = semVer;
58     }
59
60     /**
61      * Creates new YANG Schema semVer source identifier.
62      *
63      * @param name
64      *            Name of schema
65      * @param semVer
66      *            semantic version of source
67      */
68     SemVerSourceIdentifier(final String name, final SemVer semVer) {
69         this(name, Optional.empty(), semVer);
70     }
71
72     /**
73      * Returns semantic version of source if it was specified.
74      *
75      * @return revision of source.
76      */
77     public Optional<SemVer> getSemanticVersion() {
78         return Optional.ofNullable(semVer);
79     }
80
81     /**
82      * Creates new YANG Schema semVer source identifier.
83      *
84      * @param moduleName
85      *            Name of schema
86      * @param semVer
87      *            semantic version of source
88      */
89     public static SemVerSourceIdentifier create(final String moduleName, final SemVer semVer) {
90         return new SemVerSourceIdentifier(moduleName, semVer);
91     }
92
93     /**
94      * Creates new YANG Schema semVer source identifier.
95      *
96      * @param moduleName
97      *            Name of schema
98      * @param revision
99      *            Revision of source in format YYYY-mm-dd
100      * @param semVer
101      *            semantic version of source
102      */
103     public static SemVerSourceIdentifier create(final String moduleName, final Revision revision,
104             final SemVer semVer) {
105         return new SemVerSourceIdentifier(moduleName, Optional.ofNullable(revision), semVer);
106     }
107
108     /**
109      * Creates new YANG Schema semVer source identifier.
110      *
111      * @param moduleName
112      *            Name of schema
113      * @param revision
114      *            Optional of source revision in format YYYY-mm-dd. If not
115      *            present, default value will be used.
116      * @param semVer
117      *            semantic version of source
118      */
119     public static SemVerSourceIdentifier create(final String moduleName, final Optional<Revision> revision,
120             final SemVer semVer) {
121         return new SemVerSourceIdentifier(moduleName, revision, semVer);
122     }
123
124     @Override
125     public int hashCode() {
126         final int prime = 31;
127         int result = 1;
128         result = prime * result + Objects.hashCode(getName());
129         result = prime * result + Objects.hashCode(semVer);
130         return result;
131     }
132
133     @Override
134     public boolean equals(final Object obj) {
135         if (this == obj) {
136             return true;
137         }
138         if (!(obj instanceof SemVerSourceIdentifier)) {
139             return false;
140         }
141         final SemVerSourceIdentifier other = (SemVerSourceIdentifier) obj;
142         return Objects.equals(getName(), other.getName()) && Objects.equals(semVer, other.semVer);
143     }
144
145     @Override
146     public String toString() {
147         return "SemVerSourceIdentifier [name=" + getName() + "(" + semVer + ")" + "@" + getRevision() + "]";
148     }
149 }