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