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