2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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 com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
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.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import java.io.ObjectStreamException;
21 import java.io.Serial;
22 import java.io.Serializable;
24 import java.net.URISyntaxException;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.yangtools.concepts.Immutable;
28 import org.opendaylight.yangtools.concepts.WritableObject;
31 * A simple type capture of {@code namespace} statement's argument according to
32 * <a href="https://www.rfc-editor.org/rfc/rfc6020#section-7.1.3">RFC6020</a>.
35 public final class XMLNamespace implements Comparable<XMLNamespace>, Immutable, Serializable, WritableObject {
37 private static final long serialVersionUID = 1L;
38 private static final Interner<XMLNamespace> INTERNER = Interners.newWeakInterner();
40 private final String namespace;
42 private XMLNamespace(final String namespace) {
43 this.namespace = requireNonNull(namespace);
46 // FIXME: add documentation
47 public static XMLNamespace of(final String namespace) {
49 // FIXME: we want this validation, can we get it without the object allocation?
50 verifyNotNull(new URI(namespace));
51 } catch (final URISyntaxException e) {
52 throw new IllegalArgumentException("Namespace '" + namespace + "' is not a valid URI", e);
55 return new XMLNamespace(namespace);
59 * Return an interned reference to a equivalent XMLNamespace.
61 * @return Interned reference, or this object if it was interned.
63 public XMLNamespace intern() {
64 return INTERNER.intern(this);
68 @SuppressWarnings("checkstyle:parameterName")
69 public int compareTo(final XMLNamespace o) {
70 return namespace.compareTo(o.namespace);
74 public void writeTo(final DataOutput out) throws IOException {
75 out.writeUTF(namespace);
78 public static XMLNamespace readFrom(final DataInput in) throws IOException {
80 return of(in.readUTF());
81 } catch (IllegalArgumentException e) {
82 throw new IOException("Invalid namespace", e);
87 public int hashCode() {
88 return namespace.hashCode();
92 public boolean equals(final @Nullable Object obj) {
93 return this == obj || obj instanceof XMLNamespace other && namespace.equals(other.namespace);
97 public String toString() {
102 Object writeReplace() {
103 return new XNv1(this);
107 private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
112 private void readObjectNoData() throws ObjectStreamException {
117 private void writeObject(final ObjectOutputStream stream) throws IOException {