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;
* 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);
}
/**
@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());
}
}
+ 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);
}
}
@Override
- public E getElement() {
+ public @NonNull E getElement() {
return element;
}