SGF-515 - Add methods to return null-safe arrays, Lists, Maps and Sets.
Add method to convert an array of elements into an unmodifiable Set.
This commit is contained in:
@@ -99,7 +99,7 @@ public class ConnectionEndpointList extends AbstractList<ConnectionEndpoint> {
|
||||
public static ConnectionEndpointList parse(int defaultPort, String... hostsPorts) {
|
||||
List<ConnectionEndpoint> connectionEndpoints = new ArrayList<ConnectionEndpoint>(hostsPorts.length);
|
||||
|
||||
for (String hostPort : ArrayUtils.nullSafeArray(hostsPorts)) {
|
||||
for (String hostPort : ArrayUtils.nullSafeArray(hostsPorts, String.class)) {
|
||||
connectionEndpoints.add(ConnectionEndpoint.parse(hostPort, defaultPort));
|
||||
}
|
||||
|
||||
|
||||
@@ -79,16 +79,18 @@ public abstract class ArrayUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Null-safe, empty array operation returning the given Object array if not null or an empty Object array
|
||||
* Null-safe, empty array operation returning the given object array if not null or an empty object array
|
||||
* if the array argument is null.
|
||||
*
|
||||
* @param <T> the element Class type of the array.
|
||||
* @param array the Object array on which a null check is performed.
|
||||
* @return the given Object array if not null, otherwise return an empty Object array.
|
||||
* @param <T> Class type of the array elements.
|
||||
* @param array array of objects on which a null check is performed.
|
||||
* @param componentType Class type of the array elements.
|
||||
* @return the given object array if not null, otherwise return an empty object array.
|
||||
* @see java.lang.reflect.Array#newInstance(Class, int)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T[] nullSafeArray(T[] array) {
|
||||
return (array != null ? array : (T[]) new Object[0]);
|
||||
public static <T> T[] nullSafeArray(T[] array, Class<T> componentType) {
|
||||
return (array != null ? array : (T[]) Array.newInstance(componentType, 0));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,8 +19,12 @@ package org.springframework.data.gemfire.util;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* The CollectionUtils class is a utility class for working with Java Collections Framework and classes.
|
||||
@@ -28,12 +32,28 @@ import java.util.NoSuchElementException;
|
||||
* @author John Blum
|
||||
* @see java.util.Collection
|
||||
* @see java.util.Collections
|
||||
* @see java.util.List
|
||||
* @see java.util.Map
|
||||
* @see java.util.Set
|
||||
* @see org.springframework.util.CollectionUtils
|
||||
* @since 1.7.0
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class CollectionUtils extends org.springframework.util.CollectionUtils {
|
||||
|
||||
/**
|
||||
* Returns an unmodifiable {@link Set} containing the elements from the given object array.
|
||||
*
|
||||
* @param <T> Class type of the elements.
|
||||
* @param elements array of objects to add to the {@link Set}.
|
||||
* @return an unmodifiable {@link Set} containing the elements from the given object array.
|
||||
*/
|
||||
public static <T> Set<T> asSet(T... elements) {
|
||||
Set<T> set = new HashSet<T>(elements.length);
|
||||
Collections.addAll(set, elements);
|
||||
return Collections.unmodifiableSet(set);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapts the given Enumeration as an Iterable object for use within a for each loop.
|
||||
*
|
||||
@@ -69,12 +89,13 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
}
|
||||
|
||||
/**
|
||||
* A null-safe operation returning the original Collection if non-null or an empty Collection
|
||||
* (implemented with List) if null.
|
||||
* Null-safe operation returning the given {@link Collection} if not {@literal null}
|
||||
* or an empty {@link Collection} (implemented with {@link List}) if {@literal null}.
|
||||
*
|
||||
* @param <T> the element class type of the Collection.
|
||||
* @param collection the Collection to evaluate for being null.
|
||||
* @return the given Collection if not null, otherwise return an empty Collection (List).
|
||||
* @param <T> Class type of the {@link Collection} elements.
|
||||
* @param collection {@link Collection} to evaluate.
|
||||
* @return the given {@link Collection} if not null or return an empty {@link Collection}
|
||||
* (implemented with {@link List}).
|
||||
* @see java.util.Collections#emptyList()
|
||||
*/
|
||||
public static <T> Collection<T> nullSafeCollection(Collection<T> collection) {
|
||||
@@ -110,4 +131,44 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Null-safe operation returning the given {@link List} if not {@literal null}
|
||||
* or an empty {@link List} if {@literal null}.
|
||||
*
|
||||
* @param <T> Class type of the {@link List} elements.
|
||||
* @param list {@link List} to evaluate.
|
||||
* @return the given {@link List} if not null or an empty {@link List}.
|
||||
* @see java.util.Collections#emptyList()
|
||||
*/
|
||||
public static <T> List<T> nullSafeList(List<T> list) {
|
||||
return (list != null ? list : Collections.<T>emptyList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Null-safe operation returning the given {@link Map} if not {@literal null}
|
||||
* or an empty {@link Map} if {@literal null}.
|
||||
*
|
||||
* @param <K> Class type of the {@link Map Map's} keys.
|
||||
* @param <V> Class type of the {@link Map Map's} values.
|
||||
* @param map {@link Map} to evaluate.
|
||||
* @return the given {@link Map} if not null or an empty {@link Map}.
|
||||
* @see java.util.Collections#emptyMap()
|
||||
*/
|
||||
public static <K, V> Map<K, V> nullSafeMap(Map<K, V> map) {
|
||||
return (map != null ? map : Collections.<K, V>emptyMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* Null-safe operation returning the given {@link Set} if not {@literal null}
|
||||
* or an empty {@link Set} if {@literal null}.
|
||||
*
|
||||
* @param <T> Class type of the {@link Set} elements.
|
||||
* @param set {@link Set} to evaluate.
|
||||
* @return the given {@link Set} if not null or an empty {@link Set}.
|
||||
* @see java.util.Collections#emptySet()
|
||||
*/
|
||||
public static <T> Set<T> nullSafeSet(Set<T> set) {
|
||||
return (set != null ? set : Collections.<T>emptySet());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user