Use pattern matching on instanceof in yang-common
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / UnresolvedQName.java
index 58ea59f8b7c96182c81f9c144eb1ad26f46f0108..bdf614eb873247db377152449527f0af9ebfcaf1 100644 (file)
@@ -28,9 +28,8 @@ import org.eclipse.jdt.annotation.Nullable;
  *   <li>{@link Qualified}, which also holds a string prefix available via {@link Qualified#getPrefix()}.</li>
  * </ol>
  */
-// FIXME: sealed to allow Qualified and Unqualified only when we have JDK17+
 @NonNullByDefault
-public abstract class UnresolvedQName extends AbstractQName {
+public abstract sealed class UnresolvedQName extends AbstractQName {
     /**
      * An unresolved, qualified {@link QName}. It is guaranteed to hold a valid {@link #getLocalName()} bound to a
      * namespace identified through a prefix string, but remains unresolved. A resolved {@link QName} can be obtained
@@ -78,6 +77,11 @@ public abstract class UnresolvedQName extends AbstractQName {
             return INTERNER.intern(template);
         }
 
+        @Override
+        public Qualified withPrefix(final String newPrefix) {
+            return prefix.equals(newPrefix) ? this : new Qualified(newPrefix, getLocalName());
+        }
+
         @Override
         @SuppressWarnings("checkstyle:parameterName")
         public int compareTo(final Qualified o) {
@@ -96,7 +100,7 @@ public abstract class UnresolvedQName extends AbstractQName {
 
         @Override
         public boolean equals(final @Nullable Object obj) {
-            return this == obj || obj instanceof Qualified && getLocalName().equals(((Qualified) obj).getLocalName());
+            return this == obj || obj instanceof Qualified other && getLocalName().equals(other.getLocalName());
         }
 
         @Override
@@ -151,6 +155,11 @@ public abstract class UnresolvedQName extends AbstractQName {
             return null;
         }
 
+        @Override
+        public Qualified withPrefix(final String newPrefix) {
+            return new Qualified(newPrefix, getLocalName());
+        }
+
         @Override
         @SuppressWarnings("checkstyle:parameterName")
         public int compareTo(final Unqualified o) {
@@ -169,8 +178,7 @@ public abstract class UnresolvedQName extends AbstractQName {
 
         @Override
         public boolean equals(final @Nullable Object obj) {
-            return this == obj || obj instanceof Unqualified
-                    && getLocalName().equals(((AbstractQName) obj).getLocalName());
+            return this == obj || obj instanceof Unqualified other && getLocalName().equals(other.getLocalName());
         }
 
         @Override
@@ -235,4 +243,12 @@ public abstract class UnresolvedQName extends AbstractQName {
      * @return This QName's prefix
      */
     public abstract @Nullable String getPrefix();
+
+    /**
+     * Return a {@link Qualified} object bound to specified {@code prefix}.
+     *
+     * @return a {@link Qualified} object bound to specified {@code prefix}
+     * @throws NullPointerException if {@code newPrefix} is null
+     */
+    public abstract Qualified withPrefix(String newPrefix);
 }