Removed unused dependency.
[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.MoreObjects;
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 Module <b><font color="#00FF00">namespace</font></b> keyword.
83      *
84      * @return URI format of the namespace of the module
85      */
86     public URI getNamespace() {
87         return namespace;
88     }
89
90     /**
91      * Returns the revision date for the module.
92      *
93      * @return date of the module revision which is specified as argument of
94      *         YANG Module <b><font color="#339900">revison</font></b> keyword
95      */
96     public Date getRevision() {
97         return revision;
98     }
99
100     @Override
101     public int hashCode() {
102         final int prime = 31;
103         int result = (namespace == null) ? 0 : namespace.hashCode();
104         result = prime * result + ((revision == null) ? 0 : revision.hashCode());
105         return result;
106     }
107
108     @Override
109     public boolean equals(final Object obj) {
110         if (this == obj) {
111             return true;
112         }
113         if (!(obj instanceof QNameModule)) {
114             return false;
115         }
116         final QNameModule other = (QNameModule) obj;
117         if (revision == null) {
118             if (other.revision != null) {
119                 return false;
120             }
121         } else if (!revision.equals(other.revision)) {
122             return false;
123         }
124         if (namespace == null) {
125             if (other.namespace != null) {
126                 return false;
127             }
128         } else if (!namespace.equals(other.namespace)) {
129             return false;
130         }
131         return true;
132     }
133
134     /**
135      * Returns a namespace in form defined by section 5.6.4. of {@link https
136      * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
137      * the method will return <code>null</code> <br>
138      * example "http://example.acme.com/system?revision=2008-04-01"
139      *
140      * @return namespace in form defined by section 5.6.4. of {@link https
141      *         ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
142      *         defined, the method will return <code>null</code>
143      *
144      */
145     URI getRevisionNamespace() {
146
147         if (namespace == null) {
148             return null;
149         }
150
151         String query = "";
152         if (revision != null) {
153             query = "revision=" + getFormattedRevision();
154         }
155
156         URI compositeURI = null;
157         try {
158             compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
159                     namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
160         } catch (URISyntaxException e) {
161             LOG.error("", e);
162         }
163         return compositeURI;
164     }
165
166     @Override
167     public String toString() {
168         return MoreObjects.toStringHelper(this).omitNullValues().add("ns", getNamespace()).add("rev", getFormattedRevision()).toString();
169     }
170 }