*/
package org.opendaylight.yangtools.util;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.annotations.Beta;
-import com.google.common.base.Preconditions;
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 javax.annotation.Nonnull;
+import java.util.Spliterator;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
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
- public boolean contains(final Object o) {
- return o == null;
- }
-
- @Override
- public int hashCode() {
- return 0;
- }
-
- @Override
- public Object getElement() {
- return null;
- }
-
- @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
}
@Override
- public final Iterator<E> iterator() {
+ public final @NonNull Iterator<E> iterator() {
return Iterators.singletonIterator(getElement());
}
@Override
- public final Object[] toArray() {
+ public abstract @NonNull Spliterator<E> spliterator();
+
+ @Override
+ public final @NonNull Object[] toArray() {
return new Object[] { getElement() };
}
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
@Override
- public final <T> T[] toArray(final T[] a) {
+ public final <T> @NonNull T[] toArray(final T[] a) {
if (a.length > 0) {
a[0] = (T)getElement();
return a;
}
@Override
+ @SuppressWarnings("checkstyle:parameterName")
public final boolean add(final E e) {
throw new UnsupportedOperationException();
}
@Override
+ @SuppressWarnings("checkstyle:parameterName")
public final boolean remove(final Object o) {
throw new UnsupportedOperationException();
}
@Override
+ @SuppressWarnings("checkstyle:parameterName")
public final boolean containsAll(final Collection<?> c) {
- if (c.isEmpty()) {
- return true;
- }
- if (c.size() != 1) {
- return false;
- }
-
- return otherContains(c);
+ return c.isEmpty() || c.size() == 1 && otherContains(c);
}
@Override
+ @SuppressWarnings("checkstyle:parameterName")
public final boolean addAll(final Collection<? extends E> c) {
throw new UnsupportedOperationException();
}
@Override
+ @SuppressWarnings("checkstyle:parameterName")
public final boolean retainAll(final Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
+ @SuppressWarnings("checkstyle:parameterName")
public final boolean removeAll(final Collection<?> c) {
throw new UnsupportedOperationException();
}
public abstract int hashCode();
@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) {
}
}
- 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<>();
- RegularSingletonSet(final E element) {
- this.element = Preconditions.checkNotNull(element);
+ @Override
+ @SuppressWarnings("checkstyle:parameterName")
+ public boolean contains(final Object o) {
+ return o == null;
}
@Override
- public boolean contains(final Object o) {
- return element.equals(o);
+ @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 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 @Nullable Object o) {
+ return element.equals(o);
+ }
+
+ @Override
+ public @NonNull E getElement() {
return element;
}
@Override
+ @SuppressWarnings("checkstyle:equalsHashCode")
public int hashCode() {
return getElement().hashCode();
}
public String toString() {
return "[" + element + ']';
}
+
+ @Override
+ public Spliterator<E> spliterator() {
+ return SingletonSpliterators.immutableOf(element);
+ }
}
}