2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.collect.Interner;
14 import com.google.common.collect.Interners;
15 import java.io.DataInput;
16 import java.io.DataOutput;
17 import java.io.IOException;
18 import java.io.Serializable;
20 import java.util.Objects;
21 import java.util.Optional;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.opendaylight.yangtools.concepts.Identifier;
25 import org.opendaylight.yangtools.concepts.Immutable;
26 import org.opendaylight.yangtools.concepts.WritableObject;
28 public final class QNameModule implements Comparable<QNameModule>, Immutable, Serializable, Identifier, WritableObject {
29 private static final Interner<QNameModule> INTERNER = Interners.newWeakInterner();
30 private static final long serialVersionUID = 3L;
32 private final @NonNull URI namespace;
33 private final @Nullable Revision revision;
35 private transient int hash = 0;
37 private QNameModule(final URI namespace, final @Nullable Revision revision) {
38 this.namespace = requireNonNull(namespace);
39 this.revision = revision;
43 * Return an interned reference to a equivalent QNameModule.
45 * @return Interned reference, or this object if it was interned.
47 public @NonNull QNameModule intern() {
48 return INTERNER.intern(this);
52 * Create a new QName module instance with specified namespace/revision.
54 * @param namespace Module namespace
55 * @param revision Module revision
56 * @return A new, potentially shared, QNameModule instance
57 * @throws NullPointerException if any argument is null
59 public static @NonNull QNameModule create(final URI namespace, final Optional<Revision> revision) {
60 return new QNameModule(namespace, revision.orElse(null));
64 * Create a new QName module instance with specified namespace and no revision.
66 * @param namespace Module namespace
67 * @return A new, potentially shared, QNameModule instance
68 * @throws NullPointerException if {@code namespace} is null
70 public static @NonNull QNameModule create(final URI namespace) {
71 return new QNameModule(namespace, null);
75 * Create a new QName module instance with specified namespace/revision.
77 * @param namespace Module namespace
78 * @param revision Module revision
79 * @return A new, potentially shared, QNameModule instance
80 * @throws NullPointerException if any argument is null
82 public static @NonNull QNameModule create(final URI namespace, final @Nullable Revision revision) {
83 return new QNameModule(namespace, revision);
87 * Read a QNameModule from a DataInput. The format is expected to match the output format
88 * of {@link #writeTo(DataOutput)}.
90 * @param in DataInput to read
91 * @return A QNameModule instance
92 * @throws IOException if I/O error occurs
94 public static @NonNull QNameModule readFrom(final DataInput in) throws IOException {
95 final String namespace = in.readUTF();
96 final String revision = in.readUTF();
97 return new QNameModule(URI.create(namespace), revision.isEmpty() ? null : Revision.of(revision));
101 * Returns the namespace of the module which is specified as argument of YANG Module {@code namespace} keyword.
103 * @return URI format of the namespace of the module
105 public @NonNull URI getNamespace() {
110 * Returns the revision date for the module.
112 * @return date of the module revision which is specified as argument of YANG Module {@code revision} keyword
114 public @NonNull Optional<Revision> getRevision() {
115 return Optional.ofNullable(revision);
119 @SuppressWarnings("checkstyle:parameterName")
120 public int compareTo(final QNameModule o) {
121 int cmp = namespace.compareTo(o.namespace);
125 return Revision.compare(revision, o.revision);
129 * Returns a QNameModule with the same namespace, but with no revision. If this QNameModule does not have
130 * a revision, this object is returned.
132 * @return a QNameModule with the same namespace, but with no revision.
134 public @NonNull QNameModule withoutRevision() {
135 return revision == null ? this : new QNameModule(namespace, null);
139 public void writeTo(final DataOutput out) throws IOException {
140 out.writeUTF(namespace.toString());
141 out.writeUTF(revision == null ? "" : revision.toString());
145 public int hashCode() {
147 hash = Objects.hash(namespace, revision);
153 public boolean equals(final Object obj) {
157 if (!(obj instanceof QNameModule)) {
160 final QNameModule other = (QNameModule) obj;
161 return Objects.equals(revision, other.revision) && namespace.equals(other.namespace);
165 public @NonNull String toString() {
166 return MoreObjects.toStringHelper(QNameModule.class).omitNullValues().add("ns", namespace)
167 .add("rev", revision).toString();
170 Object writeReplace() {
171 return new NSv1(this);