a67f166ea2342a55373ea512272349075084a01c
[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.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.concepts.SemVer;
16 import org.opendaylight.yangtools.yang.common.Revision;
17
18 /**
19  * YANG Schema source identifier with specified semantic version.
20  *
21  * <p>
22  * Simple transfer object represents identifier of source for YANG schema (module or submodule), which consists of
23  * <ul>
24  * <li>YANG schema name {@link #getName()}
25  * <li>Semantic version of yang schema {@link #getSemanticVersion()}
26  * <li>(Optional) Module revision ({link {@link #getRevision()}
27  * </ul>
28  *
29  * <p>
30  * Source identifier is designated to be carry only necessary information to look-up YANG model source and to be used
31  * by various SchemaSourceProviders.
32  *
33  * <p>
34  * <b>Note:</b>On source retrieval layer it is impossible to distinguish between YANG module and/or submodule unless
35  * source is present.
36  *
37  * <p>
38  * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2
39  * and http://tools.ietf.org/html/rfc6022#section-3.1 ).
40  */
41 @Beta
42 public final class SemVerSourceIdentifier extends SourceIdentifier {
43     private static final long serialVersionUID = 1L;
44     private final SemVer semVer;
45
46     /**
47      * Creates new YANG Schema semVer source identifier.
48      *
49      * @param name Name of schema
50      * @param revision Revision of source, possibly not present
51      * @param semVer semantic version of source
52      */
53     SemVerSourceIdentifier(final String name, final Optional<Revision> revision, final @Nullable SemVer semVer) {
54         super(name, revision);
55         this.semVer = semVer;
56     }
57
58     /**
59      * Creates new YANG Schema semVer source identifier.
60      *
61      * @param name Name of schema
62      * @param semVer semantic version of source
63      */
64     SemVerSourceIdentifier(final String name, final @Nullable SemVer semVer) {
65         this(name, Optional.empty(), semVer);
66     }
67
68     /**
69      * Returns semantic version of source if it was specified.
70      *
71      * @return revision of source.
72      */
73     public Optional<SemVer> getSemanticVersion() {
74         return Optional.ofNullable(semVer);
75     }
76
77     /**
78      * Creates new YANG Schema semVer source identifier.
79      *
80      * @param moduleName Name of schema
81      * @param semVer semantic version of source
82      */
83     public static @NonNull SemVerSourceIdentifier create(final String moduleName, final SemVer semVer) {
84         return new SemVerSourceIdentifier(moduleName, semVer);
85     }
86
87     /**
88      * Creates new YANG Schema semVer source identifier.
89      *
90      * @param moduleName Name of schema
91      * @param revision Revision of source in format YYYY-mm-dd
92      * @param semVer semantic version of source
93      */
94     public static @NonNull SemVerSourceIdentifier create(final String moduleName, final Revision revision,
95             final SemVer semVer) {
96         return new SemVerSourceIdentifier(moduleName, Optional.ofNullable(revision), semVer);
97     }
98
99     /**
100      * Creates new YANG Schema semVer source identifier.
101      *
102      * @param moduleName Name of schema
103      * @param revision Optional of source revision in format YYYY-mm-dd. If not present, default value will be used.
104      * @param semVer semantic version of source
105      */
106     public static @NonNull SemVerSourceIdentifier create(final String moduleName, final Optional<Revision> revision,
107             final SemVer semVer) {
108         return new SemVerSourceIdentifier(moduleName, revision, semVer);
109     }
110
111     @Override
112     public int hashCode() {
113         final int prime = 31;
114         int result = 1;
115         result = prime * result + Objects.hashCode(getName());
116         result = prime * result + Objects.hashCode(semVer);
117         return result;
118     }
119
120     @Override
121     public boolean equals(final Object obj) {
122         if (this == obj) {
123             return true;
124         }
125         if (!(obj instanceof SemVerSourceIdentifier)) {
126             return false;
127         }
128         final SemVerSourceIdentifier other = (SemVerSourceIdentifier) obj;
129         return Objects.equals(getName(), other.getName()) && Objects.equals(semVer, other.semVer);
130     }
131
132     @Override
133     public String toString() {
134         return "SemVerSourceIdentifier [name=" + getName() + "(" + semVer + ")" + "@" + getRevision() + "]";
135     }
136 }