Introduce UnresolvedQName
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / UnresolvedQName.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.common;
9
10 import static java.util.Objects.requireNonNull;
11
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;
23
24 /**
25  * A {@link QName} equivalent which has not been resolved. There are two subclasses:
26  * <ol>
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>
29  * </ol>
30  */
31 // FIXME: sealed to allow Qualified and Unqualified only when we have JDK17+
32 @NonNullByDefault
33 public abstract class UnresolvedQName extends AbstractQName {
34     /**
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)}.
38      */
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();
42
43         private final String prefix;
44
45         private Qualified(final String prefix, final String localName) {
46             super(localName);
47             this.prefix = requireNonNull(prefix);
48         }
49
50         /**
51          * Read an QualifiedQName from a DataInput. The format is expected to match the output format of
52          * {@link #writeTo(DataOutput)}.
53          *
54          * @param in DataInput to read
55          * @return An QualifiedQName instance
56          * @throws IOException if I/O error occurs
57          */
58         public static Qualified readFrom(final DataInput in) throws IOException {
59             return qualified(in.readUTF(), in.readUTF());
60         }
61
62         @Override
63         public @NonNull String getPrefix() {
64             return prefix;
65         }
66
67         public Optional<QName> bindTo(final YangNamespaceContext namespaceContext) {
68             return namespaceContext.findNamespaceForPrefix(prefix).map(this::bindTo);
69         }
70
71         @Override
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);
79         }
80
81         @Override
82         @SuppressWarnings("checkstyle:parameterName")
83         public int compareTo(final Qualified o) {
84             return getLocalName().compareTo(o.getLocalName());
85         }
86
87         @Override
88         public void writeTo(final DataOutput out) throws IOException {
89             out.writeUTF(getLocalName());
90         }
91
92         @Override
93         public int hashCode() {
94             return getLocalName().hashCode();
95         }
96
97         @Override
98         public boolean equals(final @Nullable Object obj) {
99             return this == obj || obj instanceof Qualified && getLocalName().equals(((Qualified) obj).getLocalName());
100         }
101
102         @Override
103         public String toString() {
104             return MoreObjects.toStringHelper(this).add("localName", getLocalName()).toString();
105         }
106
107         @Override
108         Object writeReplace() {
109             return new QQNv1(this);
110         }
111     }
112
113     /**
114      * An unresolved, unqualified {@link QName}. It is guaranteed to hold a valid {@link #getLocalName()}, in the
115      * default namespace, which is not resolved. A resolved {@link QName} can be constructed through
116      * {@link #bindTo(QNameModule)}.
117      */
118     public static final class Unqualified extends UnresolvedQName implements Comparable<Unqualified> {
119         private static final long serialVersionUID = 1L;
120         private static final Interner<Unqualified> INTERNER = Interners.newWeakInterner();
121
122         private Unqualified(final String localName) {
123             super(localName);
124         }
125
126         /**
127          * Read an UnqualifiedQName from a DataInput. The format is expected to match the output format of
128          * {@link #writeTo(DataOutput)}.
129          *
130          * @param in DataInput to read
131          * @return An UnqualifiedQName instance
132          * @throws IOException if I/O error occurs
133          */
134         public static Unqualified readFrom(final DataInput in) throws IOException {
135             return unqualified(in.readUTF());
136         }
137
138         @Override
139         @SuppressFBWarnings(value = "ES_COMPARING_STRINGS_WITH_EQ", justification = "Interning identity check")
140         public Unqualified intern() {
141             // Make sure to intern the string and check whether it refers to the same name as we are
142             final String name = getLocalName();
143             final String internedName = name.intern();
144             final Unqualified template = internedName == name ? this : new Unqualified(internedName);
145             return INTERNER.intern(template);
146         }
147
148         @Override
149         @SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "Non-grok of @Nullable")
150         public @Nullable String getPrefix() {
151             return null;
152         }
153
154         @Override
155         @SuppressWarnings("checkstyle:parameterName")
156         public int compareTo(final Unqualified o) {
157             return getLocalName().compareTo(o.getLocalName());
158         }
159
160         @Override
161         public void writeTo(final DataOutput out) throws IOException {
162             out.writeUTF(getLocalName());
163         }
164
165         @Override
166         public int hashCode() {
167             return getLocalName().hashCode();
168         }
169
170         @Override
171         public boolean equals(final @Nullable Object obj) {
172             return this == obj || obj instanceof Unqualified
173                     && getLocalName().equals(((AbstractQName) obj).getLocalName());
174         }
175
176         @Override
177         public String toString() {
178             return MoreObjects.toStringHelper(this).add("localName", getLocalName()).toString();
179         }
180
181         @Override
182         Object writeReplace() {
183             return new UQNv1(this);
184         }
185     }
186
187     private static final long serialVersionUID = 1L;
188
189     private UnresolvedQName(final String localName) {
190         super(localName);
191     }
192
193     /**
194      * Create a new qualified unresolved QName.
195      *
196      * @param prefix The prefix on this qualified QName
197      * @param localName The local name of this qualified QName
198      * @return An UnqualifiedQName instance
199      * @throws NullPointerException if any argument is {@code null}
200      * @throws IllegalArgumentException if {@code localName} is not a valid YANG identifier
201      */
202     public static Qualified qualified(final String prefix, final String localName) {
203         return new Qualified(checkLocalName(prefix), checkLocalName(localName));
204     }
205
206     /**
207      * Create a new unqualified unresolved QName.
208      *
209      * @param localName The local name of this unqualified QName
210      * @return An UnqualifiedQName instance
211      * @throws NullPointerException if localName is {@code null}
212      * @throws IllegalArgumentException if {@code localName} is not a valid YANG identifier
213      */
214     public static Unqualified unqualified(final String localName) {
215         return new Unqualified(checkLocalName(localName));
216     }
217
218     /**
219      * Try to create a new unqualified QName.
220      *
221      * @param localName The local name of this unqualified QName
222      * @return An UnqualifiedQName instance, or null if localName is not valid
223      */
224     @SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "Non-grok of @Nullable")
225     public static @Nullable Unqualified tryLocalName(final String localName) {
226         return isValidLocalName(localName) ? new Unqualified(localName) : null;
227     }
228
229     @Override
230     public abstract UnresolvedQName intern();
231
232     /**
233      * Return the prefix of this unresolved QName.
234      *
235      * @return This QName's prefix
236      */
237     public abstract @Nullable String getPrefix();
238 }