Additional helper method in CollectionUtils to ease dealing with Serializable collections.

This commit is contained in:
nmcerlean
2016-01-12 10:36:05 +00:00
parent 29e496b5ac
commit 578ff64b23
2 changed files with 31 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ package org.alfresco.module.org_alfresco_module_rm.util;
import static org.springframework.util.ObjectUtils.nullSafeEquals;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@@ -98,6 +99,25 @@ public final class RMCollectionUtils
}
}
/**
* Returns a Serializable List containing all of the provided elements.
*
* @param elements the elements to put in a list.
* @param <T> the element type.
* @return a Serializable List containing all the provided elements.
*/
@SafeVarargs
public static <T extends Serializable, LIST extends Serializable & List<T>>
LIST asSerializableList(T... elements)
{
final LIST l = (LIST)new ArrayList<>(elements.length);
for (T element : elements)
{
l.add(element);
}
return l;
}
/**
* Returns a Set containing all of the provided elements. Duplicate elements will be removed as per the
* {@code Set} contract.

View File

@@ -23,6 +23,7 @@ import static com.google.common.collect.Sets.newHashSet;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
import static org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils.asSerializableList;
import static org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils.asSet;
import static org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils.diffKey;
import static org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils.head;
@@ -30,6 +31,7 @@ import static org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils.
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -101,4 +103,13 @@ public class RMCollectionUtilsUnitTest
assertEquals(newHashSet("hello", "world"), asSet("hello", "world"));
assertEquals(newHashSet(3, 7, 31, 127), asSet(3, 7, 31, 127));
}
@Test public void elementsAsSerializableList()
{
// If these lines compile, then we're good
Serializable s = asSerializableList("one", "two", "three");
List<String> l = asSerializableList("one", "two", "three");
assertEquals(s, l);
}
}