d24ed22b1ce99c3d74f3e1dba811347ef3244539
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / AbstractQName.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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.CharMatcher;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.concepts.Identifier;
18 import org.opendaylight.yangtools.concepts.WritableObject;
19
20 /**
21  * Abstract superclass for sharing QName references, which can either be resolved {@link QName}s or unresolved
22  * {@link UnqualifiedQName} and {@link QualifiedQName}s.
23  */
24 @Beta
25 @NonNullByDefault
26 public abstract class AbstractQName implements Identifier, WritableObject {
27     private static final long serialVersionUID = 1L;
28     private static final CharMatcher IDENTIFIER_START =
29             CharMatcher.inRange('A', 'Z').or(CharMatcher.inRange('a', 'z').or(CharMatcher.is('_'))).precomputed();
30     private static final CharMatcher NOT_IDENTIFIER_PART =
31             IDENTIFIER_START.or(CharMatcher.inRange('0', '9')).or(CharMatcher.anyOf("-.")).negate().precomputed();
32
33     private final String localName;
34
35     AbstractQName(final String localName) {
36         this.localName = requireNonNull(localName);
37     }
38
39     /**
40      * Returns YANG schema identifier which were defined for this node in the YANG module.
41      *
42      * @return YANG schema identifier which were defined for this node in the YANG module
43      */
44     public final String getLocalName() {
45         return localName;
46     }
47
48     /**
49      * Return an interned reference to an equivalent object.
50      *
51      * @return Interned reference, or this object if it was interned.
52      */
53     public abstract AbstractQName intern();
54
55     @Override
56     public abstract int hashCode();
57
58     @Override
59     public abstract boolean equals(@Nullable Object obj);
60
61     @Override
62     public abstract String toString();
63
64     /**
65      * Returns a QName with the specified namespace and the same local name as this one.
66      *
67      * @param namespace New namespace to use
68      * @return a QName with specified QNameModule and same local name as this one
69      * @throws NullPointerException if namespace is null
70      */
71     public QName bindTo(final QNameModule namespace) {
72         return new QName(namespace, getLocalName());
73     }
74
75     /**
76      * Check whether a string is a valid {@code localName}.
77      *
78      * @param str String to check
79      * @return True if the string usable as a local name, false otherwise
80      */
81     public static final boolean isValidLocalName(final @Nullable String str) {
82         return str != null && !str.isEmpty() && checkContent(str);
83     }
84
85     abstract Object writeReplace();
86
87     static final String checkLocalName(final @Nullable String localName) {
88         checkArgument(!localName.isEmpty(), "Parameter 'localName' must be a non-empty string.");
89         checkArgument(checkContent(localName), "String '%s' is not a valid identifier", localName);
90         return localName;
91     }
92
93     private static boolean checkContent(final String localName) {
94         return IDENTIFIER_START.matches(localName.charAt(0)) && NOT_IDENTIFIER_PART.indexIn(localName, 1) == -1;
95     }
96 }