f43ced86c6f7dc038de4ac8172d29117efcb06dd
[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 com.google.common.base.Optional;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.concepts.SemVer;
14 import org.opendaylight.yangtools.yang.model.api.Module;
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 formattedRevision
51      *            Optional of source revision in format YYYY-mm-dd. If not
52      *            present, default value will be used.
53      * @param semVer
54      *            semantic version of source
55      */
56     SemVerSourceIdentifier(final String name, final Optional<String> formattedRevision, final SemVer semVer) {
57         super(name, formattedRevision);
58         this.semVer = semVer == null ? Module.DEFAULT_SEMANTIC_VERSION : semVer;
59     }
60
61     /**
62      * Creates new YANG Schema semVer source identifier.
63      *
64      * @param name
65      *            Name of schema
66      * @param semVer
67      *            semantic version of source
68      */
69     SemVerSourceIdentifier(final String name, final SemVer semVer) {
70         this(name, Optional.absent(), semVer);
71     }
72
73     /**
74      * Returns semantic version of source or
75      * {@link Module#DEFAULT_SEMANTIC_VERSION} if semantic version was not
76      * supplied.
77      *
78      * @return revision of source or {@link Module#DEFAULT_SEMANTIC_VERSION} if
79      *         revision was not supplied.
80      */
81     public SemVer getSemanticVersion() {
82         return semVer;
83     }
84
85     /**
86      * Creates new YANG Schema semVer source identifier.
87      *
88      * @param moduleName
89      *            Name of schema
90      * @param semVer
91      *            semantic version of source
92      */
93     public static SemVerSourceIdentifier create(final String moduleName, final SemVer semVer) {
94         return new SemVerSourceIdentifier(moduleName, semVer);
95     }
96
97     /**
98      * Creates new YANG Schema semVer source identifier.
99      *
100      * @param moduleName
101      *            Name of schema
102      * @param revision
103      *            Revision of source in format YYYY-mm-dd
104      * @param semVer
105      *            semantic version of source
106      */
107     public static SemVerSourceIdentifier create(final String moduleName, final String revision,
108             final SemVer semVer) {
109         return new SemVerSourceIdentifier(moduleName, Optional.of(revision), semVer);
110     }
111
112     /**
113      * Creates new YANG Schema semVer source identifier.
114      *
115      * @param moduleName
116      *            Name of schema
117      * @param revision
118      *            Optional of source revision in format YYYY-mm-dd. If not
119      *            present, default value will be used.
120      * @param semVer
121      *            semantic version of source
122      */
123     public static SemVerSourceIdentifier create(final String moduleName,
124             final Optional<String> revision, final SemVer semVer) {
125         return new SemVerSourceIdentifier(moduleName, revision, semVer);
126     }
127
128     @Override
129     public int hashCode() {
130         final int prime = 31;
131         int result = 1;
132         result = prime * result + Objects.hashCode(getName());
133         result = prime * result + Objects.hashCode(semVer);
134         return result;
135     }
136
137     @Override
138     public boolean equals(final Object obj) {
139         if (this == obj) {
140             return true;
141         }
142         if (!(obj instanceof SemVerSourceIdentifier)) {
143             return false;
144         }
145         final SemVerSourceIdentifier other = (SemVerSourceIdentifier) obj;
146         return Objects.equals(getName(), other.getName()) && Objects.equals(semVer, other.semVer);
147     }
148
149     @Override
150     public String toString() {
151         return "SemVerSourceIdentifier [name=" + getName() + "(" + semVer + ")" + "@" + getRevision() + "]";
152     }
153 }