Seal SingletonSet
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / SingletonSet.java
index 51f65dfac6abd060a8e3ae4fdb188b8acddfe389..1d9936cea32cc177096ba4b02c15461786f51eea 100644 (file)
@@ -11,6 +11,8 @@ import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.Beta;
 import com.google.common.collect.Iterators;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.io.Serial;
 import java.io.Serializable;
 import java.util.Collection;
 import java.util.Iterator;
@@ -26,47 +28,13 @@ import org.opendaylight.yangtools.concepts.Immutable;
  * element -- which is desirable in some situations, as is the case in {@link SharedSingletonMap#entrySet()}.
  */
 @Beta
-public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable {
+public abstract sealed class SingletonSet<E> implements Set<E>, Immutable, Serializable {
+    @Serial
     private static final long serialVersionUID = 1L;
 
-    private static final SingletonSet<?> NULL_SINGLETON = new SingletonSet<>() {
-        private static final long serialVersionUID = 1L;
-
-        @Override
-        @SuppressWarnings("checkstyle:parameterName")
-        public boolean contains(final Object o) {
-            return o == null;
-        }
-
-        @Override
-        @SuppressWarnings("checkstyle:equalsHashCode")
-        public int hashCode() {
-            return 0;
-        }
-
-        @Override
-        public @Nullable Object getElement() {
-            return null;
-        }
-
-        @Override
-        public @NonNull Spliterator<Object> spliterator() {
-            return SingletonSpliterators.immutableOfNull();
-        }
-
-        @Override
-        public @NonNull String toString() {
-            return "[null]";
-        }
-
-        private Object readResolve() {
-            return NULL_SINGLETON;
-        }
-    };
-
     @SuppressWarnings("unchecked")
     public static <E> @NonNull SingletonSet<E> of(final @Nullable E element) {
-        return element == null ? (SingletonSet<E>) NULL_SINGLETON : new RegularSingletonSet<>(element);
+        return element == null ? (SingletonSet<E>) NullElement.INSTANCE : new Regular<>(element);
     }
 
     /**
@@ -157,17 +125,16 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
     @Override
     @SuppressWarnings("checkstyle:equalsHashCode")
     public final boolean equals(final Object obj) {
-        if (obj == this) {
-            return true;
-        }
-        if (!(obj instanceof Set)) {
-            return false;
-        }
+        return obj == this || obj instanceof Set<?> other && other.size() == 1 && otherContains(other);
+    }
 
-        final Set<?> s = (Set<?>)obj;
-        return s.size() == 1 && otherContains(s);
+    @Serial
+    final Object writeReplace() {
+        return new SSv1(getElement());
     }
 
+    @SuppressFBWarnings(value = "DCN_NULLPOINTER_EXCEPTION",
+        justification = "https://github.com/spotbugs/spotbugs/issues/1954")
     private boolean otherContains(final @NonNull Collection<?> other) {
         try {
             return other.contains(getElement());
@@ -176,13 +143,47 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
         }
     }
 
+    private static final class NullElement<E> extends SingletonSet<E> {
+        @Serial
+        private static final long serialVersionUID = 1L;
+        static final @NonNull NullElement<?> INSTANCE = new NullElement<>();
+
+        @Override
+        @SuppressWarnings("checkstyle:parameterName")
+        public boolean contains(final Object o) {
+            return o == null;
+        }
+
+        @Override
+        @SuppressWarnings("checkstyle:equalsHashCode")
+        public int hashCode() {
+            return 0;
+        }
+
+        @Override
+        public E getElement() {
+            return null;
+        }
+
+        @Override
+        public Spliterator<E> spliterator() {
+            return SingletonSpliterators.immutableOfNull();
+        }
+
+        @Override
+        public String toString() {
+            return "[null]";
+        }
+    }
+
     @NonNullByDefault
-    private static final class RegularSingletonSet<E> extends SingletonSet<E> {
+    private static final class Regular<E> extends SingletonSet<E> {
+        @Serial
         private static final long serialVersionUID = 1L;
 
-        private final E element;
+        private final @NonNull E element;
 
-        RegularSingletonSet(final E element) {
+        Regular(final E element) {
             this.element = requireNonNull(element);
         }
 
@@ -193,7 +194,7 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
         }
 
         @Override
-        public E getElement() {
+        public @NonNull E getElement() {
             return element;
         }