diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/util/ArrayUtils.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/util/ArrayUtils.java index 21f94b1a..cd2a9d2c 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/util/ArrayUtils.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/util/ArrayUtils.java @@ -17,8 +17,8 @@ import java.util.Arrays; import java.util.Iterator; import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import org.springframework.util.ObjectUtils; /** * {@link ArrayUtils} is an abstract utility class used to work with {@link Object} arrays. @@ -50,8 +50,8 @@ public abstract class ArrayUtils { * @param defaultArray array to return if the given {@code array} is {@literal null} or empty. * @return the given {@code array} if not {@literal null} or empty otherwise return the {@code defaultArray}. */ - public static T[] defaultIfEmpty(T[] array, T[] defaultArray) { - return !ObjectUtils.isEmpty(array) ? array : defaultArray; + public static @Nullable T[] defaultIfEmpty(@Nullable T[] array, @Nullable T[] defaultArray) { + return isNotEmpty(array) ? array : defaultArray; } /** @@ -63,7 +63,7 @@ public abstract class ArrayUtils { * @return the first element in the array or {@literal null} if the array is null or empty. * @see #getFirst(Object[], Object) */ - public static T getFirst(T[] array) { + public static @Nullable T getFirst(@Nullable T[] array) { return getFirst(array, null); } @@ -75,9 +75,9 @@ public abstract class ArrayUtils { * @param array the array from which to extract the first element. * @param defaultValue value to return if the array is {@literal null} or empty. * @return the first element in the array or {@code defaultValue} if the array is {@literal null} or empty. - * @see #getFirst(Object[], Object) + * @see #getFirst(Object[]) */ - public static T getFirst(T[] array, T defaultValue) { + public static @Nullable T getFirst(@Nullable T[] array, @Nullable T defaultValue) { return isEmpty(array) ? defaultValue : array[0]; } @@ -92,7 +92,7 @@ public abstract class ArrayUtils { * @see java.lang.System#arraycopy(Object, int, Object, int, int) * @see java.lang.reflect.Array#newInstance(Class, int) */ - public static Object[] insert(Object[] originalArray, int position, Object element) { + public static @NonNull Object[] insert(@NonNull Object[] originalArray, int position, Object element) { Object[] newArray = (Object[]) Array.newInstance(originalArray.getClass().getComponentType(), originalArray.length + 1); @@ -123,7 +123,7 @@ public abstract class ArrayUtils { * @return a boolean value indicating whether the given array is empty. * @see #length(Object...) */ - public static boolean isEmpty(Object[] array) { + public static boolean isEmpty(@Nullable Object[] array) { return length(array) == 0; } @@ -134,7 +134,7 @@ public abstract class ArrayUtils { * @return a boolean value indicating whether the given array is empty. * @see #isEmpty(Object[]) */ - public static boolean isNotEmpty(Object[] array) { + public static boolean isNotEmpty(@Nullable Object[] array) { return !isEmpty(array); } @@ -144,7 +144,7 @@ public abstract class ArrayUtils { * @param array the array to determine it's length. * @return the length of the given array or 0 if the array reference is null. */ - public static int length(Object[] array) { + public static int length(@Nullable Object[] array) { return array != null ? array.length : 0; } @@ -159,7 +159,7 @@ public abstract class ArrayUtils { * @see java.lang.reflect.Array#newInstance(Class, int) */ @SuppressWarnings("unchecked") - public static T[] nullSafeArray(T[] array, Class componentType) { + public static T[] nullSafeArray(@Nullable T[] array, @NonNull Class componentType) { return array != null ? array : (T[]) Array.newInstance(componentType, 0); } @@ -173,7 +173,7 @@ public abstract class ArrayUtils { * @see java.lang.System#arraycopy(Object, int, Object, int, int) * @see java.lang.reflect.Array#newInstance(Class, int) */ - public static Object[] remove(Object[] originalArray, int position) { + public static Object[] remove(@NonNull Object[] originalArray, int position) { Object[] newArray = (Object[]) Array.newInstance(originalArray.getClass().getComponentType(), originalArray.length - 1); @@ -200,7 +200,7 @@ public abstract class ArrayUtils { * @return the sorted array of elements. * @see java.util.Arrays#sort(Object[]) */ - public static > T[] sort(T[] array) { + public static @NonNull > T[] sort(@NonNull T[] array) { Arrays.sort(array); @@ -217,7 +217,7 @@ public abstract class ArrayUtils { * @see java.lang.Iterable */ @SuppressWarnings("unchecked") - public static Iterable toIterable(@NonNull T... array) { + public static @NonNull Iterable toIterable(@NonNull T... array) { return IterableArray.of(array); } diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/util/CollectionUtils.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/util/CollectionUtils.java index bc2a7159..b5c7cd3d 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/util/CollectionUtils.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/util/CollectionUtils.java @@ -16,7 +16,6 @@ package org.springframework.data.gemfire.util; import static java.util.stream.StreamSupport.stream; -import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray; import java.util.ArrayList; import java.util.Arrays; @@ -39,7 +38,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** - * {@link CollectionUtils} is an abstract utility class used to workin with the Java Collections Framework and classes. + * Abstract utility class used to operate on Java Collections Framework and classes. * * @author John Blum * @see java.util.Collection @@ -67,7 +66,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see java.lang.Iterable * @see java.util.Collection */ - public static > T addAll(@NonNull T collection, @Nullable Iterable iterable) { + public static @NonNull > T addAll(@NonNull T collection, @Nullable Iterable iterable) { Assert.notNull(collection, "Collection is required"); @@ -84,7 +83,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @return an unmodifiable {@link Set} containing the elements from the given object array. */ @SafeVarargs - public static Set asSet(@NonNull T... elements) { + public static @NonNull Set asSet(@NonNull T... elements) { Set set = new HashSet<>(elements.length); @@ -103,7 +102,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio */ public static boolean containsAny(@Nullable Collection collection, @Nullable Object... elements) { - return Arrays.asList(nullSafeArray(elements, Object.class)).stream() + return Arrays.asList(ArrayUtils.nullSafeArray(elements, Object.class)).stream() .anyMatch(element -> nullSafeCollection(collection).contains(element)); } @@ -115,8 +114,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see java.lang.Iterable * @see #nullSafeIterable(Iterable) */ - @NonNull - public static Iterable emptyIterable() { + public static @NonNull Iterable emptyIterable() { return Collections::emptyIterator; } @@ -129,8 +127,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see java.lang.Iterable * @see java.util.Enumeration */ - @NonNull - public static Iterable iterable(@Nullable Enumeration enumeration) { + public static @NonNull Iterable iterable(@Nullable Enumeration enumeration) { return () -> toIterator(nullSafeEnumeration(enumeration)); } @@ -143,8 +140,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see java.lang.Iterable * @see java.util.Iterator */ - @NonNull - public static Iterable iterable(@Nullable Iterator iterator) { + public static @NonNull Iterable iterable(@Nullable Iterator iterator) { return () -> nullSafeIterator(iterator); } @@ -159,8 +155,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see java.util.Collections#emptyList() * @see java.util.Collection */ - @NonNull - public static Collection nullSafeCollection(@Nullable Collection collection) { + public static @NonNull Collection nullSafeCollection(@Nullable Collection collection) { return collection != null ? collection : Collections.emptyList(); } @@ -175,8 +170,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see java.util.Collections#emptyEnumeration() * @see java.util.Enumeration */ - @NonNull - public static Enumeration nullSafeEnumeration(@Nullable Enumeration enumeration) { + public static @NonNull Enumeration nullSafeEnumeration(@Nullable Enumeration enumeration) { return enumeration != null ? enumeration : Collections.emptyEnumeration(); } @@ -190,13 +184,13 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see #emptyIterable() * @see java.lang.Iterable */ - @NonNull - public static Iterable nullSafeIterable(@Nullable Iterable iterable) { + public static @NonNull Iterable nullSafeIterable(@Nullable Iterable iterable) { return iterable != null ? iterable : emptyIterable(); } /** - * Returns the given {@link Iterable} if not {@literal null} or empty, otherwise returns the {@code defaultIterable}. + * Returns the given {@link Iterable} if not {@literal null} or {@literal empty}, + * otherwise returns the {@code defaultIterable}. * * @param concrete {@link Class} type of the {@link Iterable}. * @param {@link Class} type of the elements in the {@link Iterable Iterables}. @@ -205,9 +199,12 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @return {@code iterable} if not {@literal null} or empty otherwise return {@code defaultIterable}. * @see java.lang.Iterable */ - @Nullable - public static > T nullSafeIterable(@Nullable T iterable, @Nullable T defaultIterable) { - return Optional.ofNullable(iterable).filter(it -> it.iterator().hasNext()).orElse(defaultIterable); + public static @Nullable > T nullSafeIterable(@Nullable T iterable, + @Nullable T defaultIterable) { + + return Optional.ofNullable(iterable) + .filter(it -> it.iterator().hasNext()) + .orElse(defaultIterable); } /** @@ -221,8 +218,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see java.util.Collections#emptyIterator() * @see java.util.Iterator */ - @NonNull - public static Iterator nullSafeIterator(@Nullable Iterator iterator) { + public static @NonNull Iterator nullSafeIterator(@Nullable Iterator iterator) { return iterator != null ? iterator : Collections.emptyIterator(); } @@ -236,8 +232,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see java.util.Collections#emptyList() * @see java.util.List */ - @NonNull - public static List nullSafeList(@Nullable List list) { + public static @NonNull List nullSafeList(@Nullable List list) { return list != null ? list : Collections.emptyList(); } @@ -253,8 +248,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see java.util.Map */ @SuppressWarnings("all") - @NonNull - public static Map nullSafeMap(@Nullable Map map) { + public static @NonNull Map nullSafeMap(@Nullable Map map) { return map != null ? map : Collections.emptyMap(); } @@ -268,8 +262,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see java.util.Collections#emptySet() * @see java.util.Set */ - @NonNull - public static Set nullSafeSet(@Nullable Set set) { + public static @NonNull Set nullSafeSet(@Nullable Set set) { return set != null ? set : Collections.emptySet(); } @@ -355,9 +348,9 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see java.util.Collections#sort(List) * @see java.util.List */ - public static > List sort(@NonNull List list) { + public static @NonNull > List sort(@NonNull List list) { - Assert.notNull(list, "List is required"); + Assert.notNull(list, "List must not be null"); Collections.sort(list); @@ -377,9 +370,9 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @throws NullPointerException if either the list or indexes are null. * @see java.util.List */ - public static List subList(@NonNull List source, int... indices) { + public static @NonNull List subList(@NonNull List source, int... indices) { - Assert.notNull(source, "List is required"); + Assert.notNull(source, "List must not be null"); List result = new ArrayList<>(indices.length); @@ -398,8 +391,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio * @see #newSortedMap(Map) * @see java.util.Map */ - @NonNull - public static String toString(@Nullable Map map) { + public static @NonNull String toString(@Nullable Map map) { StringBuilder builder = new StringBuilder("{\n"); @@ -418,8 +410,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio return builder.toString(); } - @NonNull - private static SortedMap newSortedMap(@Nullable Map map) { + private static @NonNull SortedMap newSortedMap(@Nullable Map map) { return new TreeMap<>(nullSafeMap(map)); } } diff --git a/spring-data-geode/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsUnitTests.java b/spring-data-geode/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsUnitTests.java index b9f73c3c..7896e908 100644 --- a/spring-data-geode/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsUnitTests.java +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsUnitTests.java @@ -633,7 +633,7 @@ public class CollectionUtilsUnitTests { } catch (IllegalArgumentException expected) { - assertThat(expected).hasMessage("List is required"); + assertThat(expected).hasMessage("List must not be null"); assertThat(expected).hasNoCause(); throw expected; @@ -674,7 +674,7 @@ public class CollectionUtilsUnitTests { } catch (IllegalArgumentException expected) { - assertThat(expected).hasMessage("List is required"); + assertThat(expected).hasMessage("List must not be null"); assertThat(expected).hasNoCause(); throw expected;