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 java.util.Objects.requireNonNull;
12 import com.google.common.collect.Interner;
13 import com.google.common.collect.Interners;
14 import java.io.Serializable;
16 import java.net.URISyntaxException;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.concepts.Immutable;
21 * A simple type capture of {@code namespace} statement's argument according to
22 * <a href="https://tools.ietf.org/html/rfc6020#section-7.1.3">RFC6020</a>.
24 public final class XMLNamespace implements Comparable<XMLNamespace>, Immutable, Serializable {
25 private static final Interner<XMLNamespace> INTERNER = Interners.newWeakInterner();
26 private static final long serialVersionUID = 1L;
28 private final String namespace;
30 private XMLNamespace(final String namespace) {
31 this.namespace = requireNonNull(namespace);
34 // FIXME: add documentation
35 public static @NonNull XMLNamespace of(final String namespace) {
39 } catch (final URISyntaxException e) {
40 throw new IllegalArgumentException("Namespace '" + namespace + "' is not a valid URI", e);
43 return new XMLNamespace(namespace);
47 * Return an interned reference to a equivalent XMLNamespace.
49 * @return Interned reference, or this object if it was interned.
51 public @NonNull XMLNamespace intern() {
52 return INTERNER.intern(this);
56 @SuppressWarnings("checkstyle:parameterName")
57 public int compareTo(final XMLNamespace o) {
58 return namespace.compareTo(o.namespace);
62 public int hashCode() {
63 return namespace.hashCode();
67 public boolean equals(final Object obj) {
68 return this == obj || obj instanceof XMLNamespace && namespace.equals(((XMLNamespace) obj).namespace);
72 public String toString() {
76 Object writeReplace() {
77 return new XNv1(this);