Seal SingletonSet
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / SingletonSet.java
index 091ae4c1dba6b3e4ca67a3021a36c43b5043fbea..1d9936cea32cc177096ba4b02c15461786f51eea 100644 (file)
@@ -11,12 +11,16 @@ 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;
 import java.util.Set;
 import java.util.Spliterator;
-import javax.annotation.Nonnull;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.Immutable;
 
 /**
@@ -24,52 +28,20 @@ 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<Object>() {
-        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 Object getElement() {
-            return null;
-        }
-
-        @Override
-        public Spliterator<Object> spliterator() {
-            return SingletonSpliterators.immutableOfNull();
-        }
-
-        @Override
-        public String toString() {
-            return "[null]";
-        }
-
-        private Object readResolve() {
-            return NULL_SINGLETON;
-        }
-    };
-
     @SuppressWarnings("unchecked")
-    public static <E> SingletonSet<E> of(@Nonnull final E element) {
-        if (element == null) {
-            return (SingletonSet<E>) NULL_SINGLETON;
-        }
-        return new RegularSingletonSet<>(element);
+    public static <E> @NonNull SingletonSet<E> of(final @Nullable E element) {
+        return element == null ? (SingletonSet<E>) NullElement.INSTANCE : new Regular<>(element);
     }
 
+    /**
+     * Return the single element contained in this set.
+     *
+     * @return This set's element.
+     */
     public abstract E getElement();
 
     @Override
@@ -83,23 +55,21 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
     }
 
     @Override
-    public final Iterator<E> iterator() {
+    public final @NonNull Iterator<E> iterator() {
         return Iterators.singletonIterator(getElement());
     }
 
     @Override
-    public abstract Spliterator<E> spliterator();
+    public abstract @NonNull Spliterator<E> spliterator();
 
-    @Nonnull
     @Override
-    public final Object[] toArray() {
+    public final @NonNull Object[] toArray() {
         return new Object[] { getElement() };
     }
 
-    @Nonnull
     @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
     @Override
-    public final <T> T[] toArray(@Nonnull final T[] a) {
+    public final <T> @NonNull T[] toArray(final T[] a) {
         if (a.length > 0) {
             a[0] = (T)getElement();
             return a;
@@ -122,32 +92,25 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public final boolean containsAll(@Nonnull final Collection<?> c) {
-        if (c.isEmpty()) {
-            return true;
-        }
-        if (c.size() != 1) {
-            return false;
-        }
-
-        return otherContains(c);
+    public final boolean containsAll(final Collection<?> c) {
+        return c.isEmpty() || c.size() == 1 && otherContains(c);
     }
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public final boolean addAll(@Nonnull final Collection<? extends E> c) {
+    public final boolean addAll(final Collection<? extends E> c) {
         throw new UnsupportedOperationException();
     }
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public final boolean retainAll(@Nonnull final Collection<?> c) {
+    public final boolean retainAll(final Collection<?> c) {
         throw new UnsupportedOperationException();
     }
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public final boolean removeAll(@Nonnull final Collection<?> c) {
+    public final boolean removeAll(final Collection<?> c) {
         throw new UnsupportedOperationException();
     }
 
@@ -162,18 +125,17 @@ 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());
     }
 
-    private boolean otherContains(final Collection<?> other) {
+    @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());
         } catch (ClassCastException | NullPointerException e) {
@@ -181,22 +143,58 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
         }
     }
 
-    private static final class RegularSingletonSet<E> extends SingletonSet<E> {
+    private static final class NullElement<E> extends SingletonSet<E> {
+        @Serial
         private static final long serialVersionUID = 1L;
-        private final E element;
+        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;
+        }
 
-        RegularSingletonSet(final E element) {
+        @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 Regular<E> extends SingletonSet<E> {
+        @Serial
+        private static final long serialVersionUID = 1L;
+
+        private final @NonNull E element;
+
+        Regular(final E element) {
             this.element = requireNonNull(element);
         }
 
         @Override
         @SuppressWarnings("checkstyle:parameterName")
-        public boolean contains(final Object o) {
+        public boolean contains(final @Nullable Object o) {
             return element.equals(o);
         }
 
         @Override
-        public E getElement() {
+        public @NonNull E getElement() {
             return element;
         }