import com.google.common.base.MoreObjects;
import com.google.common.collect.Interner;
import com.google.common.collect.Interners;
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.concepts.Identifier;
import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.concepts.WritableObject;
-public final class QNameModule implements Comparable<QNameModule>, Immutable, Serializable {
+public final class QNameModule implements Comparable<QNameModule>, Immutable, Serializable, Identifier, WritableObject {
private static final Interner<QNameModule> INTERNER = Interners.newWeakInterner();
private static final long serialVersionUID = 3L;
- private final URI namespace;
+ private final @NonNull URI namespace;
//Nullable
private final Revision revision;
private transient int hash;
- private QNameModule(final URI namespace, final Revision revision) {
+ private QNameModule(final @NonNull URI namespace, final Revision revision) {
this.namespace = requireNonNull(namespace);
this.revision = revision;
}
return new QNameModule(namespace, revision);
}
+ /**
+ * Read a QNameModule from a DataInput. The format is expected to match the output format
+ * of {@link #writeTo(DataOutput)}.
+ *
+ * @param in DataInput to read
+ * @return A QNameModule instance
+ * @throws IOException if I/O error occurs
+ */
+ public static QNameModule readFrom(final DataInput in) throws IOException {
+ final String namespace = in.readUTF();
+ final String revision = in.readUTF();
+ return new QNameModule(URI.create(namespace), revision.isEmpty() ? null : Revision.of(revision));
+ }
+
/**
* Returns the namespace of the module which is specified as argument of
* YANG Module <b><font color="#00FF00">namespace</font></b> keyword.
return Revision.compare(revision, o.revision);
}
+ /**
+ * Returns a QNameModule with the same namespace, but with no revision. If this QNameModule does not have
+ * a revision, this object is returned.
+ *
+ * @return a QNameModule with the same namespace, but with no revision.
+ */
+ public QNameModule withoutRevision() {
+ return revision == null ? this : new QNameModule(namespace, null);
+ }
+
+ @Override
+ public void writeTo(final DataOutput out) throws IOException {
+ out.writeUTF(namespace.toString());
+ out.writeUTF(revision == null ? "" : revision.toString());
+ }
+
@Override
public int hashCode() {
if (hash == 0) {
return false;
}
final QNameModule other = (QNameModule) obj;
- return Objects.equals(revision, other.revision) && Objects.equals(namespace, other.namespace);
+ return Objects.equals(revision, other.revision) && namespace.equals(other.namespace);
}
/**
@Override
public String toString() {
- return MoreObjects.toStringHelper(QNameModule.class).omitNullValues().add("ns", getNamespace())
+ return MoreObjects.toStringHelper(QNameModule.class).omitNullValues().add("ns", namespace)
.add("rev", revision).toString();
}
}