Separate out {Identity,Equality}QueuedNotificationManager
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / SingletonSet.java
index af024857da5c39de2437a13ab4169ec6ac512ca0..914268a45a21c74b3f4db06a05ce7561067d6128 100644 (file)
@@ -7,14 +7,18 @@
  */
 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 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;
 
 /**
@@ -41,12 +45,17 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
         }
 
         @Override
-        public Object getElement() {
+        public @Nullable Object getElement() {
             return null;
         }
 
         @Override
-        public String toString() {
+        public @NonNull Spliterator<Object> spliterator() {
+            return SingletonSpliterators.immutableOfNull();
+        }
+
+        @Override
+        public @NonNull String toString() {
             return "[null]";
         }
 
@@ -56,14 +65,11 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
     };
 
     @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>) NULL_SINGLETON : new RegularSingletonSet<>(element);
     }
 
-    public abstract E getElement();
+    public abstract @Nullable E getElement();
 
     @Override
     public final int size() {
@@ -76,20 +82,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());
     }
 
-    @Nonnull
     @Override
-    public final Object[] toArray() {
+    public abstract @NonNull Spliterator<E> spliterator();
+
+    @Override
+    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;
@@ -112,32 +119,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();
     }
 
@@ -163,7 +163,7 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
         return s.size() == 1 && otherContains(s);
     }
 
-    private boolean otherContains(final Collection<?> other) {
+    private boolean otherContains(final @NonNull Collection<?> other) {
         try {
             return other.contains(getElement());
         } catch (ClassCastException | NullPointerException e) {
@@ -171,17 +171,19 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
         }
     }
 
+    @NonNullByDefault
     private static final class RegularSingletonSet<E> extends SingletonSet<E> {
         private static final long serialVersionUID = 1L;
+
         private final E element;
 
         RegularSingletonSet(final E element) {
-            this.element = Preconditions.checkNotNull(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);
         }
 
@@ -200,5 +202,10 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
         public String toString() {
             return "[" + element + ']';
         }
+
+        @Override
+        public Spliterator<E> spliterator() {
+            return SingletonSpliterators.immutableOf(element);
+        }
     }
 }