Allow rebinding the prefix of an UnresolvedQName
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / UnqualifiedQName.java
1 /*
2  * Copyright (c) 2019 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 com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.collect.Interner;
13 import com.google.common.collect.Interners;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.io.DataInput;
16 import java.io.DataOutput;
17 import java.io.IOException;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * An unresolved, unqualified {@link QName}. It is guaranteed to hold a valid {@link #getLocalName()}, in the default
23  * namespace, which is not resolved. A resolved {@link QName} can be constructed through {@link #bindTo(QNameModule)}.
24  */
25 @Beta
26 @NonNullByDefault
27 public final class UnqualifiedQName extends AbstractQName implements Comparable<UnqualifiedQName> {
28     private static final long serialVersionUID = 1L;
29     private static final Interner<UnqualifiedQName> INTERNER = Interners.newWeakInterner();
30
31     private UnqualifiedQName(final String localName) {
32         super(localName);
33     }
34
35     /**
36      * Create a new unqualified QName.
37      *
38      * @param localName The local name of this unqualified QName
39      * @return An UnqualifiedQName instance
40      * @throws NullPointerException if localName is null
41      * @throws IllegalArgumentException if localName is not a valid YANG identifier
42      */
43     public static UnqualifiedQName of(final String localName) {
44         return new UnqualifiedQName(checkLocalName(localName));
45     }
46
47     /**
48      * Create a new unqualified QName.
49      *
50      * @param localName The local name of this unqualified QName
51      * @return An UnqualifiedQName instance, or null if localName is not valid
52      */
53     @SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "Non-grok of @Nullable")
54     public static @Nullable UnqualifiedQName tryCreate(final String localName) {
55         return isValidLocalName(localName) ? new UnqualifiedQName(localName) : null;
56     }
57
58     /**
59      * Return a {@link QualifiedQName} object bound to specified {@code prefix}.
60      *
61      * @return a {@link QualifiedQName} object bound to specified {@code prefix}
62      * @throws NullPointerException if {@code newPrefix} is null
63      */
64     public QualifiedQName withPrefix(final String newPrefix) {
65         return new QualifiedQName(newPrefix, getLocalName());
66     }
67
68     /**
69      * Read an UnqualifiedQName from a DataInput. The format is expected to match the output format of
70      * {@link #writeTo(DataOutput)}.
71      *
72      * @param in DataInput to read
73      * @return An UnqualifiedQName instance
74      * @throws IOException if I/O error occurs
75      */
76     public static UnqualifiedQName readFrom(final DataInput in) throws IOException {
77         return of(in.readUTF());
78     }
79
80     @Override
81     @SuppressFBWarnings(value = "ES_COMPARING_STRINGS_WITH_EQ", justification = "Interning identity check")
82     public UnqualifiedQName intern() {
83         // Make sure to intern the string and check whether it refers to the same name as we are
84         final String name = getLocalName();
85         final String internedName = name.intern();
86         final UnqualifiedQName template = internedName == name ? this : new UnqualifiedQName(internedName);
87         return INTERNER.intern(template);
88     }
89
90     @Override
91     @SuppressWarnings("checkstyle:parameterName")
92     public int compareTo(final UnqualifiedQName o) {
93         return getLocalName().compareTo(o.getLocalName());
94     }
95
96     @Override
97     public void writeTo(final DataOutput out) throws IOException {
98         out.writeUTF(getLocalName());
99     }
100
101     @Override
102     public int hashCode() {
103         return getLocalName().hashCode();
104     }
105
106     @Override
107     public boolean equals(final @Nullable Object obj) {
108         return this == obj || obj instanceof UnqualifiedQName
109                 && getLocalName().equals(((AbstractQName) obj).getLocalName());
110     }
111
112     @Override
113     public String toString() {
114         return MoreObjects.toStringHelper(this).add("localName", getLocalName()).toString();
115     }
116
117     @Override
118     Object writeReplace() {
119         return new UQNv1(this);
120     }
121 }