Migrate common/util to use JDT annotations
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / SingletonSet.java
index 8f036931329cce6ec99db8e9ec4595677f8f10a2..b809978ee0a879e9db2940a535b5d4ff7144037e 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;
 
 /**
@@ -29,22 +33,29 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
         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() {
+        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]";
         }
 
@@ -54,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() {
@@ -74,37 +82,43 @@ 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 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;
         }
 
-        return (T[]) new Object[] { (T) getElement() };
+        return (T[]) new Object[] {getElement()};
     }
 
     @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;
@@ -117,16 +131,19 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
     }
 
     @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();
     }
@@ -140,6 +157,7 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
     public abstract int hashCode();
 
     @Override
+    @SuppressWarnings("checkstyle:equalsHashCode")
     public final boolean equals(final Object obj) {
         if (obj == this) {
             return true;
@@ -152,7 +170,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) {
@@ -160,16 +178,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
-        public boolean contains(final Object o) {
+        @SuppressWarnings("checkstyle:parameterName")
+        public boolean contains(final @Nullable Object o) {
             return element.equals(o);
         }
 
@@ -179,6 +200,7 @@ public abstract class SingletonSet<E> implements Set<E>, Immutable, Serializable
         }
 
         @Override
+        @SuppressWarnings("checkstyle:equalsHashCode")
         public int hashCode() {
             return getElement().hashCode();
         }
@@ -187,5 +209,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);
+        }
     }
 }