diff --git a/src/main/java/org/springframework/data/gemfire/support/ConnectionEndpointList.java b/src/main/java/org/springframework/data/gemfire/support/ConnectionEndpointList.java index 90545b3d..803fb2c6 100644 --- a/src/main/java/org/springframework/data/gemfire/support/ConnectionEndpointList.java +++ b/src/main/java/org/springframework/data/gemfire/support/ConnectionEndpointList.java @@ -99,7 +99,7 @@ public class ConnectionEndpointList extends AbstractList { public static ConnectionEndpointList parse(int defaultPort, String... hostsPorts) { List connectionEndpoints = new ArrayList(hostsPorts.length); - for (String hostPort : ArrayUtils.nullSafeArray(hostsPorts)) { + for (String hostPort : ArrayUtils.nullSafeArray(hostsPorts, String.class)) { connectionEndpoints.add(ConnectionEndpoint.parse(hostPort, defaultPort)); } diff --git a/src/main/java/org/springframework/data/gemfire/util/ArrayUtils.java b/src/main/java/org/springframework/data/gemfire/util/ArrayUtils.java index 93ac489a..9a03c1a2 100644 --- a/src/main/java/org/springframework/data/gemfire/util/ArrayUtils.java +++ b/src/main/java/org/springframework/data/gemfire/util/ArrayUtils.java @@ -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 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 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[] nullSafeArray(T[] array) { - return (array != null ? array : (T[]) new Object[0]); + public static T[] nullSafeArray(T[] array, Class componentType) { + return (array != null ? array : (T[]) Array.newInstance(componentType, 0)); } /** diff --git a/src/main/java/org/springframework/data/gemfire/util/CollectionUtils.java b/src/main/java/org/springframework/data/gemfire/util/CollectionUtils.java index c5ee55ef..2108d1cb 100644 --- a/src/main/java/org/springframework/data/gemfire/util/CollectionUtils.java +++ b/src/main/java/org/springframework/data/gemfire/util/CollectionUtils.java @@ -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 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 Set asSet(T... elements) { + Set set = new HashSet(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 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 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 Collection nullSafeCollection(Collection 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 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 List nullSafeList(List list) { + return (list != null ? list : Collections.emptyList()); + } + + /** + * Null-safe operation returning the given {@link Map} if not {@literal null} + * or an empty {@link Map} if {@literal null}. + * + * @param Class type of the {@link Map Map's} keys. + * @param 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 Map nullSafeMap(Map map) { + return (map != null ? map : Collections.emptyMap()); + } + + /** + * Null-safe operation returning the given {@link Set} if not {@literal null} + * or an empty {@link Set} if {@literal null}. + * + * @param 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 Set nullSafeSet(Set set) { + return (set != null ? set : Collections.emptySet()); + } } diff --git a/src/test/java/org/springframework/data/gemfire/util/ArrayUtilsTest.java b/src/test/java/org/springframework/data/gemfire/util/ArrayUtilsTest.java index 3e43802b..7a3cd465 100644 --- a/src/test/java/org/springframework/data/gemfire/util/ArrayUtilsTest.java +++ b/src/test/java/org/springframework/data/gemfire/util/ArrayUtilsTest.java @@ -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 diff --git a/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsTest.java b/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsTest.java index b3fa8a2a..88f196bf 100644 --- a/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsTest.java +++ b/src/test/java/org/springframework/data/gemfire/util/CollectionUtilsTest.java @@ -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 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 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)); + } }