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