Fixing sonar issues 3
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / QName.java
index 0d17c76eb9d6184a7343c850c50d43ef81dafa26..99069d27bde67c685c0ba65fceac32ffaeb80d41 100644 (file)
-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.yangtools.yang.common;\r
-\r
-import java.net.URI;\r
-import java.net.URISyntaxException;\r
-\r
-import java.text.SimpleDateFormat;\r
-import java.util.Date;\r
-\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-/**\r
- * The QName from XML consists of local name of element and XML namespace, but\r
- * for our use, we added module revision to it.\r
- * \r
- * In YANG context QName is full name of defined node, type, procedure or\r
- * notification. QName consists of XML namespace, YANG model revision and local\r
- * name of defined type. It is used to prevent name clashes between nodes with\r
- * same local name, but from different schemas.\r
- * \r
- * <ul>\r
- * <li><b>XMLNamespace</b> - the namespace assigned to the YANG module which\r
- * defined element, type, procedure or notification.</li>\r
- * <li><b>Revision</b> - the revision of the YANG module which describes the\r
- * element</li>\r
- * <li><b>LocalName</b> - the YANG schema identifier which were defined for this\r
- * node in the YANG module</li>\r
- * </ul>\r
- * \r
- * \r
- */\r
-public final class QName {\r
-    protected static final Logger logger = LoggerFactory\r
-        .getLogger(QName.class);\r
-\r
-    private SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");\r
-\r
-    final URI namespace;\r
-    final String localName;\r
-    final String prefix;\r
-    final Date revision;\r
-\r
-    /**\r
-     * QName Constructor.\r
-     * \r
-     * @param namespace\r
-     *            the namespace assigned to the YANG module\r
-     * @param revision\r
-     *            the revision of the YANG module\r
-     * @param prefix\r
-     *            locally defined prefix assigned to local name\r
-     * @param localName\r
-     *            YANG schema identifier\r
-     */\r
-    public QName(URI namespace, Date revision, String prefix, String localName) {\r
-        this.namespace = namespace;\r
-        this.localName = localName;\r
-        this.revision = revision;\r
-        this.prefix = prefix;\r
-    }\r
-\r
-    /**\r
-     * QName Constructor.\r
-     * \r
-     * @param namespace\r
-     *            the namespace assigned to the YANG module\r
-     * @param localName\r
-     *            YANG schema identifier\r
-     */\r
-    public QName(URI namespace, String localName) {\r
-        this(namespace, null, "", localName);\r
-    }\r
-\r
-    /**\r
-     * QName Constructor.\r
-     * \r
-     * @param namespace\r
-     *            the namespace assigned to the YANG module\r
-     * @param revision\r
-     *            the revision of the YANG module\r
-     * @param localName\r
-     *            YANG schema identifier\r
-     */\r
-    public QName(URI namespace, Date revision, String localName) {\r
-        this(namespace, revision, null, localName);\r
-    }\r
-\r
-    public QName(QName base, String localName) {\r
-        this(base.getNamespace(), base.getRevision(), base.getPrefix(),\r
-                localName);\r
-    }\r
-\r
-    /**\r
-     * Returns XMLNamespace assigned to the YANG module.\r
-     * \r
-     * @return XMLNamespace assigned to the YANG module.\r
-     */\r
-    public URI getNamespace() {\r
-        return namespace;\r
-    }\r
-\r
-    /**\r
-     * Returns YANG schema identifier which were defined for this node in the\r
-     * YANG module\r
-     * \r
-     * @return YANG schema identifier which were defined for this node in the\r
-     *         YANG module\r
-     */\r
-    public String getLocalName() {\r
-        return localName;\r
-    }\r
-\r
-    /**\r
-     * Returns revision of the YANG module if the module has defined revision,\r
-     * otherwise returns <code>null</code>\r
-     * \r
-     * @return revision of the YANG module if the module has defined revision,\r
-     *         otherwise returns <code>null</code>\r
-     */\r
-    public Date getRevision() {\r
-        return revision;\r
-    }\r
-\r
-    /**\r
-     * Returns locally defined prefix assigned to local name\r
-     * \r
-     * @return locally defined prefix assigned to local name\r
-     */\r
-    public String getPrefix() {\r
-        return prefix;\r
-    }\r
-\r
-    @Override\r
-    public int hashCode() {\r
-        final int prime = 31;\r
-        int result = 1;\r
-        result = prime * result\r
-                + ((localName == null) ? 0 : localName.hashCode());\r
-        result = prime * result\r
-                + ((namespace == null) ? 0 : namespace.hashCode());\r
-        result = prime * result\r
-                + ((revision == null) ? 0 : revision.hashCode());\r
-        return result;\r
-    }\r
-\r
-    @Override\r
-    public boolean equals(Object obj) {\r
-        if (this == obj)\r
-            return true;\r
-        if (obj == null)\r
-            return false;\r
-        if (getClass() != obj.getClass())\r
-            return false;\r
-        QName other = (QName) obj;\r
-        if (localName == null) {\r
-            if (other.localName != null)\r
-                return false;\r
-        } else if (!localName.equals(other.localName))\r
-            return false;\r
-        if (namespace == null) {\r
-            if (other.namespace != null)\r
-                return false;\r
-        } else if (!namespace.equals(other.namespace))\r
-            return false;\r
-        if (revision == null) {\r
-            if (other.revision != null)\r
-                return false;\r
-        } else if (!revision.equals(other.revision))\r
-            return false;\r
-        return true;\r
-    }\r
-\r
-    @Override\r
-    public String toString() {\r
-        StringBuilder sb = new StringBuilder();\r
-        if (namespace != null) {\r
-            sb.append("(" + namespace);\r
-\r
-            if (revision != null) {\r
-                sb.append("?revision=" + revisionFormat.format(revision));\r
-            }\r
-            sb.append(")");\r
-        }\r
-        sb.append(localName);\r
-        return sb.toString();\r
-    }\r
-\r
-    /**\r
-     * Returns a namespace in form defined by section 5.6.4. of {@link https\r
-     * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,\r
-     * the method will return <code>null</code> <br>\r
-     * example "http://example.acme.com/system?revision=2008-04-01"\r
-     * \r
-     * @return namespace in form defined by section 5.6.4. of {@link https\r
-     *         ://tools.ietf.org/html/rfc6020}, if namespace is not correctly\r
-     *         defined, the method will return <code>null</code>\r
-     * \r
-     */\r
-    URI getRevisionNamespace() {\r
-\r
-        if (namespace == null)\r
-            return null;\r
-\r
-        String query = "";\r
-        if (revision != null) {\r
-            query = "revision=" + revisionFormat.format(revision);\r
-        }\r
-\r
-        URI compositeURI = null;\r
-        try {\r
-            compositeURI = new URI(namespace.getScheme(),\r
-                    namespace.getUserInfo(), namespace.getHost(),\r
-                    namespace.getPort(), namespace.getPath(), query,\r
-                    namespace.getFragment());\r
-        } catch (URISyntaxException e) {\r
-            logger.error("",e);\r
-        }\r
-        return compositeURI;\r
-    }\r
-}\r
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.common;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The QName from XML consists of local name of element and XML namespace, but
+ * for our use, we added module revision to it.
+ * 
+ * In YANG context QName is full name of defined node, type, procedure or
+ * notification. QName consists of XML namespace, YANG model revision and local
+ * name of defined type. It is used to prevent name clashes between nodes with
+ * same local name, but from different schemas.
+ * 
+ * <ul>
+ * <li><b>XMLNamespace</b> - the namespace assigned to the YANG module which
+ * defined element, type, procedure or notification.</li>
+ * <li><b>Revision</b> - the revision of the YANG module which describes the
+ * element</li>
+ * <li><b>LocalName</b> - the YANG schema identifier which were defined for this
+ * node in the YANG module</li>
+ * </ul>
+ * 
+ * 
+ */
+public final class QName {
+    protected static final Logger LOGGER = LoggerFactory.getLogger(QName.class);
+
+    private SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
+
+    private final URI namespace;
+    private final String localName;
+    private final String prefix;
+    private final Date revision;
+
+    /**
+     * QName Constructor.
+     * 
+     * @param namespace
+     *            the namespace assigned to the YANG module
+     * @param revision
+     *            the revision of the YANG module
+     * @param prefix
+     *            locally defined prefix assigned to local name
+     * @param localName
+     *            YANG schema identifier
+     */
+    public QName(URI namespace, Date revision, String prefix, String localName) {
+        this.namespace = namespace;
+        this.localName = localName;
+        this.revision = revision;
+        this.prefix = prefix;
+    }
+
+    /**
+     * QName Constructor.
+     * 
+     * @param namespace
+     *            the namespace assigned to the YANG module
+     * @param localName
+     *            YANG schema identifier
+     */
+    public QName(URI namespace, String localName) {
+        this(namespace, null, "", localName);
+    }
+
+    /**
+     * QName Constructor.
+     * 
+     * @param namespace
+     *            the namespace assigned to the YANG module
+     * @param revision
+     *            the revision of the YANG module
+     * @param localName
+     *            YANG schema identifier
+     */
+    public QName(URI namespace, Date revision, String localName) {
+        this(namespace, revision, null, localName);
+    }
+
+    public QName(QName base, String localName) {
+        this(base.getNamespace(), base.getRevision(), base.getPrefix(), localName);
+    }
+
+    /**
+     * Returns XMLNamespace assigned to the YANG module.
+     * 
+     * @return XMLNamespace assigned to the YANG module.
+     */
+    public URI getNamespace() {
+        return namespace;
+    }
+
+    /**
+     * Returns YANG schema identifier which were defined for this node in the
+     * YANG module
+     * 
+     * @return YANG schema identifier which were defined for this node in the
+     *         YANG module
+     */
+    public String getLocalName() {
+        return localName;
+    }
+
+    /**
+     * Returns revision of the YANG module if the module has defined revision,
+     * otherwise returns <code>null</code>
+     * 
+     * @return revision of the YANG module if the module has defined revision,
+     *         otherwise returns <code>null</code>
+     */
+    public Date getRevision() {
+        return revision;
+    }
+
+    /**
+     * Returns locally defined prefix assigned to local name
+     * 
+     * @return locally defined prefix assigned to local name
+     */
+    public String getPrefix() {
+        return prefix;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((localName == null) ? 0 : localName.hashCode());
+        result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
+        result = prime * result + ((revision == null) ? 0 : revision.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        QName other = (QName) obj;
+        if (localName == null) {
+            if (other.localName != null) {
+                return false;
+            }
+        } else if (!localName.equals(other.localName)) {
+            return false;
+        }
+        if (namespace == null) {
+            if (other.namespace != null) {
+                return false;
+            }
+        } else if (!namespace.equals(other.namespace)) {
+            return false;
+        }
+        if (revision == null) {
+            if (other.revision != null) {
+                return false;
+            }
+        } else if (!revision.equals(other.revision)) {
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        if (namespace != null) {
+            sb.append("(" + namespace);
+
+            if (revision != null) {
+                sb.append("?revision=" + revisionFormat.format(revision));
+            }
+            sb.append(")");
+        }
+        sb.append(localName);
+        return sb.toString();
+    }
+
+    /**
+     * Returns a namespace in form defined by section 5.6.4. of {@link https
+     * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
+     * the method will return <code>null</code> <br>
+     * example "http://example.acme.com/system?revision=2008-04-01"
+     * 
+     * @return namespace in form defined by section 5.6.4. of {@link https
+     *         ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
+     *         defined, the method will return <code>null</code>
+     * 
+     */
+    URI getRevisionNamespace() {
+
+        if (namespace == null) {
+            return null;
+        }
+
+        String query = "";
+        if (revision != null) {
+            query = "revision=" + revisionFormat.format(revision);
+        }
+
+        URI compositeURI = null;
+        try {
+            compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
+                    namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
+        } catch (URISyntaxException e) {
+            LOGGER.error("", e);
+        }
+        return compositeURI;
+    }
+}