Merge "Resolved some sonar issues: If Stmts Must Use Braces"
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / QNameModule.java
1 /*
2  * Copyright (c) 2013 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.common;
9
10 import com.google.common.base.Objects;
11 import java.io.Serializable;
12 import java.net.URI;
13 import java.net.URISyntaxException;
14 import java.util.Date;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.objcache.ObjectCache;
17 import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public final class QNameModule implements Immutable, Serializable {
22     private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QNameModule.class);
23     private static final Logger LOG = LoggerFactory.getLogger(QNameModule.class);
24     private static final QNameModule NULL_INSTANCE = new QNameModule(null, null);
25     private static final long serialVersionUID = 1L;
26
27     //Nullable
28     private final URI namespace;
29
30     //Nullable
31     private final Date revision;
32
33     //Nullable
34     private volatile String formattedRevision;
35
36     private QNameModule(final URI namespace, final Date revision) {
37         this.namespace = namespace;
38         this.revision = revision;
39     }
40
41     /**
42      * Look up specified module in the global cache and return a shared reference.
43      *
44      * @param module Module instance
45      * @return Cached instance, according to {@link ObjectCache} policy.
46      */
47     public static QNameModule cachedReference(final QNameModule module) {
48         return CACHE.getReference(module);
49     }
50
51     /**
52      * Create a new QName module instance with specified namespace/revision.
53      *
54      * @param namespace Module namespace
55      * @param revision Module revision
56      * @return A new, potentially shared, QNameModule instance
57      */
58     public static QNameModule create(final URI namespace, final Date revision) {
59         if (namespace == null && revision == null) {
60             return NULL_INSTANCE;
61         }
62
63         return new QNameModule(namespace, revision);
64     }
65
66     public String getFormattedRevision() {
67         if (revision == null) {
68             return null;
69         }
70
71         String ret = formattedRevision;
72         if (ret == null) {
73             ret = SimpleDateFormatUtil.getRevisionFormat().format(revision);
74             formattedRevision = ret;
75         }
76
77         return ret;
78     }
79
80     /**
81      * Returns the namespace of the module which is specified as argument of
82      * YANG {@link Module <b><font color="#00FF00">namespace</font></b>}
83      * keyword.
84      *
85      * @return URI format of the namespace of the module
86      */
87     public URI getNamespace() {
88         return namespace;
89     }
90
91     /**
92      * Returns the revision date for the module.
93      *
94      * @return date of the module revision which is specified as argument of
95      *         YANG {@link Module <b><font color="#339900">revison</font></b>}
96      *         keyword
97      */
98     public Date getRevision() {
99         return revision;
100     }
101
102     @Override
103     public int hashCode() {
104         final int prime = 31;
105         int result = (namespace == null) ? 0 : namespace.hashCode();
106         result = prime * result + ((revision == null) ? 0 : revision.hashCode());
107         return result;
108     }
109
110     @Override
111     public boolean equals(final Object obj) {
112         if (this == obj) {
113             return true;
114         }
115         if (!(obj instanceof QNameModule)) {
116             return false;
117         }
118         final QNameModule other = (QNameModule) obj;
119         if (revision == null) {
120             if (other.revision != null) {
121                 return false;
122             }
123         } else if (!revision.equals(other.revision)) {
124             return false;
125         }
126         if (namespace == null) {
127             if (other.namespace != null) {
128                 return false;
129             }
130         } else if (!namespace.equals(other.namespace)) {
131             return false;
132         }
133         return true;
134     }
135
136     /**
137      * Returns a namespace in form defined by section 5.6.4. of {@link https
138      * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
139      * the method will return <code>null</code> <br>
140      * example "http://example.acme.com/system?revision=2008-04-01"
141      *
142      * @return namespace in form defined by section 5.6.4. of {@link https
143      *         ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
144      *         defined, the method will return <code>null</code>
145      *
146      */
147     URI getRevisionNamespace() {
148
149         if (namespace == null) {
150             return null;
151         }
152
153         String query = "";
154         if (revision != null) {
155             query = "revision=" + getFormattedRevision();
156         }
157
158         URI compositeURI = null;
159         try {
160             compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
161                     namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
162         } catch (URISyntaxException e) {
163             LOG.error("", e);
164         }
165         return compositeURI;
166     }
167
168     @Override
169     public String toString() {
170         return Objects.toStringHelper(this).omitNullValues().add("ns", getNamespace()).add("rev", getFormattedRevision()).toString();
171     }
172 }