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:
John Blum
2016-09-09 13:40:26 -07:00
parent 3f5758d5a0
commit 2132e2d66b
5 changed files with 189 additions and 38 deletions

View File

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

View File

@@ -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));
}
/**

View File

@@ -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());
}
}

View File

@@ -102,27 +102,27 @@ public class ArrayUtilsTest {
public void nullSafeArrayWithNonNullArray() {
String[] stringArray = { "test", "testing", "tested" };
assertThat(ArrayUtils.nullSafeArray(stringArray), is(sameInstance(stringArray)));
Double[] emptyDoubleArray = {};
assertThat(ArrayUtils.nullSafeArray(emptyDoubleArray), is(sameInstance(emptyDoubleArray)));
assertThat(ArrayUtils.nullSafeArray(stringArray, String.class), is(sameInstance(stringArray)));
Integer[] numberArray = { 1, 2, 3 };
assertThat(ArrayUtils.nullSafeArray(numberArray), is(sameInstance(numberArray)));
assertThat(ArrayUtils.nullSafeArray(numberArray, Integer.class), is(sameInstance(numberArray)));
Double[] emptyDoubleArray = {};
assertThat(ArrayUtils.nullSafeArray(emptyDoubleArray, Double.class), is(sameInstance(emptyDoubleArray)));
Character[] characterArray = { 'A', 'B', 'C' };
assertThat(ArrayUtils.nullSafeArray(characterArray), is(sameInstance(characterArray)));
assertThat(ArrayUtils.nullSafeArray(characterArray, Character.class), is(sameInstance(characterArray)));
}
@Test
public void nullSafeArrayWithNullArray() {
Object array = ArrayUtils.nullSafeArray(null);
Object array = ArrayUtils.nullSafeArray(null, String.class);
assertThat(array, is(instanceOf(Object[].class)));
assertThat(((Object[]) array).length, is(equalTo(0)));
assertThat(array, is(instanceOf(String[].class)));
assertThat(((String[]) array).length, is(equalTo(0)));
}
@Test

View File

@@ -16,16 +16,14 @@
package org.springframework.data.gemfire.util;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.junit.Assert.assertNotNull;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -37,18 +35,23 @@ import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import org.junit.Test;
/**
* The CollectionUtilsTest class is a test suite of test cases testing the contract and functionality
* of the CollectionUtils class.
* Test suite of test cases testing the contract and functionality of the {@link CollectionUtils} class.
*
* @author John Blum
* @see java.util.Collection
* @see java.util.Collections
* @see java.util.List
* @see java.util.Enumeration
* @see java.util.Iterator
* @see java.util.Map
* @see java.util.Set
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.util.CollectionUtils
@@ -56,6 +59,46 @@ import org.junit.Test;
*/
public class CollectionUtilsTest {
@Test
public void asSetContainsAllArrayElements() {
Object[] elements = { "a", "b", "c" };
Set<?> set = CollectionUtils.asSet(elements);
assertThat(set, is(notNullValue(Set.class)));
assertThat(set.size(), is(equalTo(elements.length)));
assertThat(set.containsAll(Arrays.asList(elements)), is(true));
}
@Test
public void asSetContainsUniqueArrayElements() {
Object[] elements = { 1, 2, 1 };
Set<?> set = CollectionUtils.asSet(elements);
assertThat(set, is(notNullValue(Set.class)));
assertThat(set.size(), is(equalTo(2)));
assertThat(set.containsAll(Arrays.asList(elements)), is(true));
}
@Test(expected = UnsupportedOperationException.class)
public void asSetReturnsUnmodifiableSet() {
Set<Integer> set = CollectionUtils.asSet(1, 2, 3);
assertThat(set, is(notNullValue(Set.class)));
assertThat(set.size(), is(equalTo(3)));
try {
set.add(4);
set.remove(1);
set.remove(2);
}
catch (UnsupportedOperationException e) {
assertThat(set.size(), is(equalTo(3)));
throw e;
}
}
@Test
@SuppressWarnings("unchecked")
public void iterableEnumeration() {
@@ -108,17 +151,17 @@ public class CollectionUtilsTest {
@Test
public void nullSafeCollectionWithNonNullCollection() {
List<?> mockList = mock(List.class);
Collection<?> mockCollection = mock(Collection.class);
assertSame(mockList, CollectionUtils.nullSafeCollection(mockList));
assertSame(mockCollection, CollectionUtils.nullSafeCollection(mockCollection));
}
@Test
public void nullSafeCollectionWithNullCollection() {
Collection collection = CollectionUtils.nullSafeCollection(null);
assertNotNull(collection);
assertTrue(collection.isEmpty());
assertThat(collection, is(notNullValue(Collection.class)));
assertThat(collection.isEmpty(), is(true));
}
@Test
@@ -133,8 +176,8 @@ public class CollectionUtilsTest {
public void nullSafeIterableWithNullIterable() {
Iterable<Object> iterable = CollectionUtils.nullSafeIterable(null);
assertThat(iterable, is(not(nullValue())));
assertThat(iterable.iterator(), is(not(nullValue())));
assertThat(iterable, is(not(nullValue(Iterable.class))));
assertThat(iterable.iterator(), is(not(nullValue(Iterator.class))));
}
@Test(expected = UnsupportedOperationException.class)
@@ -161,4 +204,49 @@ public class CollectionUtilsTest {
}
}
}
@Test
public void nullSafeListWithNonNullList() {
List<?> mockList = mock(List.class);
assertSame(mockList, CollectionUtils.nullSafeList(mockList));
}
@Test
public void nullSafeListWithNullList() {
List<?> list = CollectionUtils.nullSafeList(null);
assertThat(list, is(notNullValue(List.class)));
assertThat(list.isEmpty(), is(true));
}
@Test
public void nullSafeMapWithNonNullMap() {
Map<?, ?> mockMap = mock(Map.class);
assertSame(mockMap, CollectionUtils.nullSafeMap(mockMap));
}
@Test
public void nullSafeMapWithNullMap() {
Map<?, ?> map = CollectionUtils.nullSafeMap(null);
assertThat(map, is(notNullValue(Map.class)));
assertThat(map.isEmpty(), is(true));
}
@Test
public void nullSafeSetWithNonNullSet() {
Set<?> mockSet = mock(Set.class);
assertSame(mockSet, CollectionUtils.nullSafeSet(mockSet));
}
@Test
public void nullSafeSetWithNullSet() {
Set<?> set = CollectionUtils.nullSafeSet(null);
assertThat(set, is(notNullValue(Set.class)));
assertThat(set.isEmpty(), is(true));
}
}