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.base.MoreObjects;
13 import com.google.common.collect.Interner;
14 import com.google.common.collect.Interners;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.io.DataInput;
17 import java.io.DataOutput;
18 import java.io.IOException;
19 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
25 * A {@link QName} equivalent which has not been resolved. There are two subclasses:
27 * <li>{@link Unqualified}, which holds only a local name available through {@link #getLocalName()}.<li>
28 * <li>{@link Qualified}, which also holds a string prefix available via {@link Qualified#getPrefix()}.</li>
31 // FIXME: sealed to allow Qualified and Unqualified only when we have JDK17+
33 public abstract class UnresolvedQName extends AbstractQName {
35 * An unresolved, qualified {@link QName}. It is guaranteed to hold a valid {@link #getLocalName()} bound to a
36 * namespace identified through a prefix string, but remains unresolved. A resolved {@link QName} can be obtained
37 * through {@link #bindTo(YangNamespaceContext)}.
39 public static final class Qualified extends UnresolvedQName implements Comparable<Qualified> {
40 private static final long serialVersionUID = 1L;
41 private static final Interner<Qualified> INTERNER = Interners.newWeakInterner();
43 private final String prefix;
45 private Qualified(final String prefix, final String localName) {
47 this.prefix = requireNonNull(prefix);
51 * Read an QualifiedQName from a DataInput. The format is expected to match the output format of
52 * {@link #writeTo(DataOutput)}.
54 * @param in DataInput to read
55 * @return An QualifiedQName instance
56 * @throws IOException if I/O error occurs
58 public static Qualified readFrom(final DataInput in) throws IOException {
59 return qualified(in.readUTF(), in.readUTF());
63 public @NonNull String getPrefix() {
67 public Optional<QName> bindTo(final YangNamespaceContext namespaceContext) {
68 return namespaceContext.findNamespaceForPrefix(prefix).map(this::bindTo);
72 @SuppressFBWarnings(value = "ES_COMPARING_STRINGS_WITH_EQ", justification = "Interning identity check")
73 public Qualified intern() {
74 // Make sure to intern the string and check whether it refers to the same name as we are
75 final String name = getLocalName();
76 final String internedName = name.intern();
77 final Qualified template = internedName == name ? this : new Qualified(prefix.intern(), internedName);
78 return INTERNER.intern(template);
82 public Qualified withPrefix(final String newPrefix) {
83 return prefix.equals(newPrefix) ? this : new Qualified(newPrefix, getLocalName());
87 @SuppressWarnings("checkstyle:parameterName")
88 public int compareTo(final Qualified o) {
89 return getLocalName().compareTo(o.getLocalName());
93 public void writeTo(final DataOutput out) throws IOException {
94 out.writeUTF(getLocalName());
98 public int hashCode() {
99 return getLocalName().hashCode();
103 public boolean equals(final @Nullable Object obj) {
104 return this == obj || obj instanceof Qualified && getLocalName().equals(((Qualified) obj).getLocalName());
108 public String toString() {
109 return MoreObjects.toStringHelper(this).add("localName", getLocalName()).toString();
113 Object writeReplace() {
114 return new QQNv1(this);
119 * An unresolved, unqualified {@link QName}. It is guaranteed to hold a valid {@link #getLocalName()}, in the
120 * default namespace, which is not resolved. A resolved {@link QName} can be constructed through
121 * {@link #bindTo(QNameModule)}.
123 public static final class Unqualified extends UnresolvedQName implements Comparable<Unqualified> {
124 private static final long serialVersionUID = 1L;
125 private static final Interner<Unqualified> INTERNER = Interners.newWeakInterner();
127 private Unqualified(final String localName) {
132 * Read an UnqualifiedQName from a DataInput. The format is expected to match the output format of
133 * {@link #writeTo(DataOutput)}.
135 * @param in DataInput to read
136 * @return An UnqualifiedQName instance
137 * @throws IOException if I/O error occurs
139 public static Unqualified readFrom(final DataInput in) throws IOException {
140 return unqualified(in.readUTF());
144 @SuppressFBWarnings(value = "ES_COMPARING_STRINGS_WITH_EQ", justification = "Interning identity check")
145 public Unqualified intern() {
146 // Make sure to intern the string and check whether it refers to the same name as we are
147 final String name = getLocalName();
148 final String internedName = name.intern();
149 final Unqualified template = internedName == name ? this : new Unqualified(internedName);
150 return INTERNER.intern(template);
154 @SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "Non-grok of @Nullable")
155 public @Nullable String getPrefix() {
160 public Qualified withPrefix(final String newPrefix) {
161 return new Qualified(newPrefix, getLocalName());
165 @SuppressWarnings("checkstyle:parameterName")
166 public int compareTo(final Unqualified o) {
167 return getLocalName().compareTo(o.getLocalName());
171 public void writeTo(final DataOutput out) throws IOException {
172 out.writeUTF(getLocalName());
176 public int hashCode() {
177 return getLocalName().hashCode();
181 public boolean equals(final @Nullable Object obj) {
182 return this == obj || obj instanceof Unqualified
183 && getLocalName().equals(((AbstractQName) obj).getLocalName());
187 public String toString() {
188 return MoreObjects.toStringHelper(this).add("localName", getLocalName()).toString();
192 Object writeReplace() {
193 return new UQNv1(this);
197 private static final long serialVersionUID = 1L;
199 private UnresolvedQName(final String localName) {
204 * Create a new qualified unresolved QName.
206 * @param prefix The prefix on this qualified QName
207 * @param localName The local name of this qualified QName
208 * @return An UnqualifiedQName instance
209 * @throws NullPointerException if any argument is {@code null}
210 * @throws IllegalArgumentException if {@code localName} is not a valid YANG identifier
212 public static Qualified qualified(final String prefix, final String localName) {
213 return new Qualified(checkLocalName(prefix), checkLocalName(localName));
217 * Create a new unqualified unresolved QName.
219 * @param localName The local name of this unqualified QName
220 * @return An UnqualifiedQName instance
221 * @throws NullPointerException if localName is {@code null}
222 * @throws IllegalArgumentException if {@code localName} is not a valid YANG identifier
224 public static Unqualified unqualified(final String localName) {
225 return new Unqualified(checkLocalName(localName));
229 * Try to create a new unqualified QName.
231 * @param localName The local name of this unqualified QName
232 * @return An UnqualifiedQName instance, or null if localName is not valid
234 @SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "Non-grok of @Nullable")
235 public static @Nullable Unqualified tryLocalName(final String localName) {
236 return isValidLocalName(localName) ? new Unqualified(localName) : null;
240 public abstract UnresolvedQName intern();
243 * Return the prefix of this unresolved QName.
245 * @return This QName's prefix
247 public abstract @Nullable String getPrefix();
250 * Return a {@link Qualified} object bound to specified {@code prefix}.
252 * @return a {@link Qualified} object bound to specified {@code prefix}
253 * @throws NullPointerException if {@code newPrefix} is null
255 public abstract Qualified withPrefix(String newPrefix);