Review and refactor the ArrayUtils and CollectionUtils support classes.

* Annotate the API with Spring's @NonNull and @Nullable annotations.
* Edit Javadoc.
This commit is contained in:
John Blum
2022-04-04 13:01:06 -07:00
parent cd2643711c
commit 8ce280ca8f
3 changed files with 44 additions and 53 deletions

View File

@@ -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> T[] defaultIfEmpty(T[] array, T[] defaultArray) {
return !ObjectUtils.isEmpty(array) ? array : defaultArray;
public static @Nullable <T> 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> T getFirst(T[] array) {
public static @Nullable <T> 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> T getFirst(T[] array, T defaultValue) {
public static @Nullable <T> 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> T[] nullSafeArray(T[] array, Class<T> componentType) {
public static <T> T[] nullSafeArray(@Nullable T[] array, @NonNull Class<T> 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 extends Comparable<T>> T[] sort(T[] array) {
public static @NonNull <T extends Comparable<T>> T[] sort(@NonNull T[] array) {
Arrays.sort(array);
@@ -217,7 +217,7 @@ public abstract class ArrayUtils {
* @see java.lang.Iterable
*/
@SuppressWarnings("unchecked")
public static <T> Iterable<T> toIterable(@NonNull T... array) {
public static @NonNull <T> Iterable<T> toIterable(@NonNull T... array) {
return IterableArray.of(array);
}

View File

@@ -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 <E, T extends Collection<E>> T addAll(@NonNull T collection, @Nullable Iterable<E> iterable) {
public static @NonNull <E, T extends Collection<E>> T addAll(@NonNull T collection, @Nullable Iterable<E> 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 <T> Set<T> asSet(@NonNull T... elements) {
public static @NonNull <T> Set<T> asSet(@NonNull T... elements) {
Set<T> 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 <T> Iterable<T> emptyIterable() {
public static @NonNull <T> Iterable<T> 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 <T> Iterable<T> iterable(@Nullable Enumeration<T> enumeration) {
public static @NonNull <T> Iterable<T> iterable(@Nullable Enumeration<T> 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 <T> Iterable<T> iterable(@Nullable Iterator<T> iterator) {
public static @NonNull <T> Iterable<T> iterable(@Nullable Iterator<T> 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 <T> Collection<T> nullSafeCollection(@Nullable Collection<T> collection) {
public static @NonNull <T> Collection<T> nullSafeCollection(@Nullable Collection<T> 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 <T> Enumeration<T> nullSafeEnumeration(@Nullable Enumeration<T> enumeration) {
public static @NonNull <T> Enumeration<T> nullSafeEnumeration(@Nullable Enumeration<T> 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 <T> Iterable<T> nullSafeIterable(@Nullable Iterable<T> iterable) {
public static @NonNull <T> Iterable<T> nullSafeIterable(@Nullable Iterable<T> 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 <T> concrete {@link Class} type of the {@link Iterable}.
* @param <E> {@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 <E, T extends Iterable<E>> T nullSafeIterable(@Nullable T iterable, @Nullable T defaultIterable) {
return Optional.ofNullable(iterable).filter(it -> it.iterator().hasNext()).orElse(defaultIterable);
public static @Nullable <E, T extends Iterable<E>> 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 <T> Iterator<T> nullSafeIterator(@Nullable Iterator<T> iterator) {
public static @NonNull <T> Iterator<T> nullSafeIterator(@Nullable Iterator<T> 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 <T> List<T> nullSafeList(@Nullable List<T> list) {
public static @NonNull <T> List<T> nullSafeList(@Nullable List<T> 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 <K, V> Map<K, V> nullSafeMap(@Nullable Map<K, V> map) {
public static @NonNull <K, V> Map<K, V> nullSafeMap(@Nullable Map<K, V> map) {
return map != null ? map : Collections.<K, V>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 <T> Set<T> nullSafeSet(@Nullable Set<T> set) {
public static @NonNull <T> Set<T> nullSafeSet(@Nullable Set<T> 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 <T extends Comparable<T>> List<T> sort(@NonNull List<T> list) {
public static @NonNull <T extends Comparable<T>> List<T> sort(@NonNull List<T> 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 <T> List<T> subList(@NonNull List<T> source, int... indices) {
public static @NonNull <T> List<T> subList(@NonNull List<T> source, int... indices) {
Assert.notNull(source, "List is required");
Assert.notNull(source, "List must not be null");
List<T> 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));
}
}

View File

@@ -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;