Bug 4662: Introduce a SemanticVersion concept - SchemaContextFactory
[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      * revision. {@link SourceIdentifier#NOT_PRESENT_FORMATTED_REVISION} as
44      * default revision.
45      *
46      * @param name
47      *            Name of schema
48      */
49     RevisionSourceIdentifier(final String name) {
50         this(name, NOT_PRESENT_FORMATTED_REVISION);
51     }
52
53     /**
54      * Creates new YANG Schema revision source identifier.
55      *
56      * @param name
57      *            Name of schema
58      * @param formattedRevision
59      *            Revision of source in format YYYY-mm-dd
60      */
61     RevisionSourceIdentifier(final String name, final String formattedRevision) {
62         super(Preconditions.checkNotNull(name), Preconditions.checkNotNull(formattedRevision));
63     }
64
65     /**
66      *
67      * Creates new YANG Schema revision source identifier.
68      *
69      * @param name
70      *            Name of schema
71      * @param formattedRevision
72      *            Revision of source in format YYYY-mm-dd. If not present,
73      *            default value will be used.
74      */
75     RevisionSourceIdentifier(final String name, final Optional<String> formattedRevision) {
76         this(name, formattedRevision.or(NOT_PRESENT_FORMATTED_REVISION));
77     }
78
79     /**
80      *
81      * Creates new YANG Schema revision source identifier.
82      *
83      * @param moduleName
84      *            Name of schema
85      * @param revision
86      *            Revision of source in format YYYY-mm-dd. If not present,
87      *            default value will be used.
88      */
89     public static RevisionSourceIdentifier create(final String moduleName,
90             final Optional<String> 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 in format YYYY-mm-dd
101      */
102     public static RevisionSourceIdentifier create(final String moduleName, final String revision) {
103         return new RevisionSourceIdentifier(moduleName, revision);
104     }
105
106     /**
107      *
108      * Creates new YANG Schema revision source identifier for sources without
109      * revision. {@link SourceIdentifier#NOT_PRESENT_FORMATTED_REVISION} as
110      * default revision.
111      *
112      * @param moduleName
113      *            Name of schema
114      */
115     public static RevisionSourceIdentifier create(final String moduleName) {
116         return new RevisionSourceIdentifier(moduleName);
117     }
118
119     @Override
120     public int hashCode() {
121         final int prime = 31;
122         int result = 1;
123         result = prime * result + Objects.hashCode(getName());
124         result = prime * result + Objects.hashCode(getRevision());
125         return result;
126     }
127
128     @Override
129     public boolean equals(final Object obj) {
130         if (this == obj) {
131             return true;
132         }
133         if (!(obj instanceof RevisionSourceIdentifier)) {
134             return false;
135         }
136         final RevisionSourceIdentifier other = (RevisionSourceIdentifier) obj;
137         return Objects.equals(getName(), other.getName()) && Objects.equals(getRevision(), other.getRevision());
138     }
139
140     @Override
141     public String toString() {
142         return "RevisionSourceIdentifier [name=" + getName() + "@" + getRevision() + "]";
143     }
144 }