Reuse a single QNameModule for NULL
[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 java.io.Serializable;
11 import java.net.URI;
12 import java.net.URISyntaxException;
13 import java.util.Date;
14
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public final class QNameModule implements Immutable, Serializable {
20     private static final Logger LOG = LoggerFactory.getLogger(QNameModule.class);
21     private static final QNameModule NULL_INSTANCE = new QNameModule(null, null);
22     private static final long serialVersionUID = 1L;
23
24     //Nullable
25     private final URI namespace;
26
27     //Nullable
28     private final Date revision;
29
30     //Nullable
31     private String formattedRevision;
32
33     private QNameModule(final URI namespace, final Date revision) {
34         this.namespace = namespace;
35         this.revision = revision;
36     }
37
38     public static QNameModule create(final URI namespace, final Date revision) {
39         if (namespace == null && revision == null) {
40             return NULL_INSTANCE;
41         }
42
43         return new QNameModule(namespace, revision);
44     }
45
46     public String getFormattedRevision() {
47         if (revision == null) {
48             return null;
49         }
50
51         if (formattedRevision == null) {
52             synchronized (this) {
53                 if (formattedRevision == null) {
54                     formattedRevision = SimpleDateFormatUtil.getRevisionFormat().format(revision);
55                 }
56             }
57         }
58
59         return formattedRevision;
60     }
61
62     public URI getNamespace() {
63         return namespace;
64     }
65
66     public Date getRevision() {
67         return revision;
68     }
69
70     @Override
71     public int hashCode() {
72         final int prime = 31;
73         int result = (namespace == null) ? 0 : namespace.hashCode();
74         result = prime * result + ((revision == null) ? 0 : revision.hashCode());
75         return result;
76     }
77
78     @Override
79     public boolean equals(final Object obj) {
80         if (this == obj) {
81             return true;
82         }
83         if (!(obj instanceof QNameModule)) {
84             return false;
85         }
86         final QNameModule other = (QNameModule) obj;
87         if (namespace == null) {
88             if (other.namespace != null) {
89                 return false;
90             }
91         } else if (!namespace.equals(other.namespace)) {
92             return false;
93         }
94         if (revision == null) {
95             if (other.revision != null) {
96                 return false;
97             }
98         } else if (!revision.equals(other.revision)) {
99             return false;
100         }
101         return true;
102     }
103
104     /**
105      * Returns a namespace in form defined by section 5.6.4. of {@link https
106      * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
107      * the method will return <code>null</code> <br>
108      * example "http://example.acme.com/system?revision=2008-04-01"
109      *
110      * @return namespace in form defined by section 5.6.4. of {@link https
111      *         ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
112      *         defined, the method will return <code>null</code>
113      *
114      */
115     URI getRevisionNamespace() {
116
117         if (namespace == null) {
118             return null;
119         }
120
121         String query = "";
122         if (revision != null) {
123             query = "revision=" + getFormattedRevision();
124         }
125
126         URI compositeURI = null;
127         try {
128             compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
129                     namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
130         } catch (URISyntaxException e) {
131             LOG.error("", e);
132         }
133         return compositeURI;
134     }
135 }