Remove formattedRevision from serialization format
[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 = 2L;
27
28     //Nullable
29     private final URI namespace;
30
31     //Nullable
32     private final Date revision;
33
34     //Nullable
35     private transient volatile String formattedRevision;
36
37     private transient int hash;
38
39     private QNameModule(final URI namespace, final Date revision) {
40         this.namespace = namespace;
41         this.revision = revision;
42     }
43
44     /**
45      * Return an interned reference to a equivalent QNameModule.
46      *
47      * @return Interned reference, or this object if it was interned.
48      */
49     public QNameModule intern() {
50         return INTERNER.intern(this);
51     }
52
53     /**
54      * Create a new QName module instance with specified namespace/revision.
55      *
56      * @param namespace Module namespace
57      * @param revision Module revision
58      * @return A new, potentially shared, QNameModule instance
59      */
60     public static QNameModule create(final URI namespace, final Date revision) {
61         if (namespace == null && revision == null) {
62             return NULL_INSTANCE;
63         }
64
65         return new QNameModule(namespace, revision);
66     }
67
68     public String getFormattedRevision() {
69         if (revision == null) {
70             return null;
71         }
72
73         String ret = formattedRevision;
74         if (ret == null) {
75             ret = SimpleDateFormatUtil.getRevisionFormat().format(revision);
76             formattedRevision = ret;
77         }
78
79         return ret;
80     }
81
82     /**
83      * Returns the namespace of the module which is specified as argument of
84      * YANG Module <b><font color="#00FF00">namespace</font></b> keyword.
85      *
86      * @return URI format of the namespace of the module
87      */
88     public URI getNamespace() {
89         return namespace;
90     }
91
92     /**
93      * Returns the revision date for the module.
94      *
95      * @return date of the module revision which is specified as argument of
96      *         YANG Module <b><font color="#339900">revison</font></b> keyword
97      */
98     public Date getRevision() {
99         return revision;
100     }
101
102     @Override
103     public int hashCode() {
104         if (hash == 0) {
105             hash = Objects.hash(namespace, revision);
106         }
107         return hash;
108     }
109
110     @Override
111     public boolean equals(final Object obj) {
112         if (this == obj) {
113             return true;
114         }
115         if (!(obj instanceof QNameModule)) {
116             return false;
117         }
118         final QNameModule other = (QNameModule) obj;
119         return Objects.equals(revision, other.revision) && Objects.equals(namespace, other.namespace);
120     }
121
122     /**
123      * Returns a namespace in form defined by section 5.6.4. of {@link https
124      * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
125      * the method will return <code>null</code> <br>
126      * example "http://example.acme.com/system?revision=2008-04-01"
127      *
128      * @return namespace in form defined by section 5.6.4. of {@link https
129      *         ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
130      *         defined, the method will return <code>null</code>
131      *
132      */
133     URI getRevisionNamespace() {
134         if (namespace == null) {
135             return null;
136         }
137
138         final String query = revision == null ? "" : "revision=" + getFormattedRevision();
139         try {
140             return new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
141                     namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
142         } catch (final URISyntaxException e) {
143             LOG.error("Failed to construct URI for {}", this, e);
144             return null;
145         }
146     }
147
148     @Override
149     public String toString() {
150         return MoreObjects.toStringHelper(QNameModule.class).omitNullValues().add("ns", getNamespace()).add("rev", getFormattedRevision()).toString();
151     }
152 }