Merge branch 'master' of ../controller
[yangtools.git] / yang / 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     public static UnqualifiedQName of(final String localName) {
36         return new UnqualifiedQName(checkLocalName(localName));
37     }
38
39     /**
40      * Read an UnboundQName from a DataInput. The format is expected to match the output format of
41      * {@link #writeTo(DataOutput)}.
42      *
43      * @param in DataInput to read
44      * @return An UnboundQName instance
45      * @throws IOException if I/O error occurs
46      */
47     public static UnqualifiedQName readFrom(final DataInput in) throws IOException {
48         return of(in.readUTF());
49     }
50
51     public QName bindTo(final QNameModule namespace) {
52         return new QName(namespace, getLocalName());
53     }
54
55     @Override
56     @SuppressFBWarnings(value = "ES_COMPARING_STRINGS_WITH_EQ", justification = "Interning identity check")
57     public UnqualifiedQName intern() {
58         // Make sure to intern the string and check whether it refers to the same name as we are
59         final String name = getLocalName();
60         final String internedName = name.intern();
61         final UnqualifiedQName template = internedName == name ? this : new UnqualifiedQName(internedName);
62         return INTERNER.intern(template);
63     }
64
65     @Override
66     @SuppressWarnings("checkstyle:parameterName")
67     public int compareTo(final UnqualifiedQName o) {
68         return getLocalName().compareTo(o.getLocalName());
69     }
70
71     @Override
72     public void writeTo(final DataOutput out) throws IOException {
73         out.writeUTF(getLocalName());
74     }
75
76     @Override
77     public int hashCode() {
78         return getLocalName().hashCode();
79     }
80
81     @Override
82     public boolean equals(final @Nullable Object obj) {
83         return this == obj || obj instanceof UnqualifiedQName
84                 && getLocalName().equals(((AbstractQName) obj).getLocalName());
85     }
86
87     @Override
88     public String toString() {
89         return MoreObjects.toStringHelper(this).add("localName", getLocalName()).toString();
90     }
91
92     @Override
93     Object writeReplace() {
94         return new UQNv1(this);
95     }
96 }