080155aaf46bbee5ee85d1baf653d8a11a1d4e10
[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 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.yang.common.Revision;
16
17 /**
18  * YANG Schema revision source identifier.
19  *
20  * <p>
21  * Simple transfer object represents revision identifier of source for YANG schema (module or submodule), which consists
22  * of
23  * <ul>
24  * <li>YANG schema name ({@link #getName()}
25  * <li>Module revision (optional) ({link {@link #getRevision()})
26  * </ul>
27  *
28  * <p>
29  * Revision source identifier is designated to be carry only necessary information to look-up YANG model source
30  * and to be used by various SchemaSourceProviders.
31  *
32  * <p>
33  * <b>Note:</b>On source retrieval layer it is impossible to distinguish between YANG module and/or submodule unless
34  * 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 RevisionSourceIdentifier extends SourceIdentifier {
42     private static final long serialVersionUID = 1L;
43
44     /**
45      * Creates new YANG Schema revision source identifier for sources without a revision.
46      *
47      * @param name Name of schema
48      */
49     RevisionSourceIdentifier(final String name) {
50         super(name);
51     }
52
53     /**
54      * Creates new YANG Schema revision source identifier.
55      *
56      * @param name Name of schema
57      * @param revision Revision of source, may be null
58      */
59     RevisionSourceIdentifier(final String name, final @Nullable Revision revision) {
60         super(name, revision);
61     }
62
63     /**
64      * Creates new YANG Schema revision source identifier.
65      *
66      * @param name Name of schema
67      * @param formattedRevision Revision of source, potentially not present
68      */
69     private RevisionSourceIdentifier(final String name, final Optional<Revision> revision) {
70         super(name, revision);
71     }
72
73     /**
74      * Creates new YANG Schema revision source identifier.
75      *
76      * @param moduleName Name of schema
77      * @param revision Revision of source in format YYYY-mm-dd. If not present, default value will be used.
78      */
79     public static @NonNull RevisionSourceIdentifier create(final String moduleName, final Optional<Revision> revision) {
80         return new RevisionSourceIdentifier(moduleName, revision);
81     }
82
83     /**
84      * Creates new YANG Schema revision source identifier.
85      *
86      * @param moduleName Name of schema
87      * @param revision Revision of source, may be null
88      */
89     public static @NonNull RevisionSourceIdentifier create(final String moduleName, final @Nullable Revision revision) {
90         return new RevisionSourceIdentifier(moduleName, revision);
91     }
92
93     /**
94      * Creates new YANG Schema revision source identifier for sources without
95      * a revision.
96      *
97      * @param moduleName
98      *            Name of schema
99      */
100     public static @NonNull RevisionSourceIdentifier create(final String moduleName) {
101         return new RevisionSourceIdentifier(moduleName);
102     }
103
104     @Override
105     public int hashCode() {
106         final int prime = 31;
107         int result = 1;
108         result = prime * result + Objects.hashCode(getName());
109         result = prime * result + Objects.hashCode(getRevision());
110         return result;
111     }
112
113     @Override
114     public boolean equals(final Object obj) {
115         if (this == obj) {
116             return true;
117         }
118         if (!(obj instanceof RevisionSourceIdentifier)) {
119             return false;
120         }
121         final RevisionSourceIdentifier other = (RevisionSourceIdentifier) obj;
122         return Objects.equals(getName(), other.getName()) && Objects.equals(getRevision(), other.getRevision());
123     }
124
125     @Override
126     public String toString() {
127         final StringBuilder sb = new StringBuilder("RevisionSourceIdentifier [name=");
128         sb.append(getName());
129
130         final Optional<Revision> rev = getRevision();
131         if (rev.isPresent()) {
132             sb.append('@').append(rev.get());
133         }
134         return sb.append(']').toString();
135     }
136 }