Merge "Bug 560: Fixed incoherent API implementation."
[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     /**
63      * Returns the namespace of the module which is specified as argument of
64      * YANG {@link Module <b><font color="#00FF00">namespace</font></b>}
65      * keyword.
66      *
67      * @return URI format of the namespace of the module
68      */
69     public URI getNamespace() {
70         return namespace;
71     }
72
73     /**
74      * Returns the revision date for the module.
75      *
76      * @return date of the module revision which is specified as argument of
77      *         YANG {@link Module <b><font color="#339900">revison</font></b>}
78      *         keyword
79      */
80     public Date getRevision() {
81         return revision;
82     }
83
84     @Override
85     public int hashCode() {
86         final int prime = 31;
87         int result = (namespace == null) ? 0 : namespace.hashCode();
88         result = prime * result + ((revision == null) ? 0 : revision.hashCode());
89         return result;
90     }
91
92     @Override
93     public boolean equals(final Object obj) {
94         if (this == obj) {
95             return true;
96         }
97         if (!(obj instanceof QNameModule)) {
98             return false;
99         }
100         final QNameModule other = (QNameModule) obj;
101         if (namespace == null) {
102             if (other.namespace != null) {
103                 return false;
104             }
105         } else if (!namespace.equals(other.namespace)) {
106             return false;
107         }
108         if (revision == null) {
109             if (other.revision != null) {
110                 return false;
111             }
112         } else if (!revision.equals(other.revision)) {
113             return false;
114         }
115         return true;
116     }
117
118     /**
119      * Returns a namespace in form defined by section 5.6.4. of {@link https
120      * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
121      * the method will return <code>null</code> <br>
122      * example "http://example.acme.com/system?revision=2008-04-01"
123      *
124      * @return namespace in form defined by section 5.6.4. of {@link https
125      *         ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
126      *         defined, the method will return <code>null</code>
127      *
128      */
129     URI getRevisionNamespace() {
130
131         if (namespace == null) {
132             return null;
133         }
134
135         String query = "";
136         if (revision != null) {
137             query = "revision=" + getFormattedRevision();
138         }
139
140         URI compositeURI = null;
141         try {
142             compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
143                     namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
144         } catch (URISyntaxException e) {
145             LOG.error("", e);
146         }
147         return compositeURI;
148     }
149 }