Utility method asSet - also testing Bamboo build.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@119764 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Neil McErlean
2015-12-08 11:21:30 +00:00
parent 7db3beea1e
commit 022bbcbde5
2 changed files with 28 additions and 0 deletions

View File

@@ -97,6 +97,24 @@ public final class RMCollectionUtils
}
}
/**
* Returns a Set containing all of the provided elements. Duplicate elements will be removed as per the
* {@code Set} contract.
*
* @param elements the elements to put in a Set.
* @param <T> the element type.
* @return a Set containing all the provided elements (without duplicates).
*/
public static <T> HashSet<T> asSet(T... elements)
{
final HashSet<T> set = new HashSet<>(elements.length);
for (T element : elements)
{
set.add(element);
}
return set;
}
/**
* This enum represents a change in an entry between 2 collections.
*/

View File

@@ -22,6 +22,7 @@ package org.alfresco.module.org_alfresco_module_rm.util;
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.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;
import static org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils.tail;
@@ -34,6 +35,7 @@ import org.junit.Test;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -94,4 +96,12 @@ public class RMCollectionUtilsUnitTest
assertEquals("a", head(asList("a")));
assertNull(head(emptyList()));
}
@Test public void elementsAsSet()
{
assertEquals(new HashSet<String>() {{ this.add("hello"); this.add("world"); }},
asSet("hello", "world"));
assertEquals(new HashSet<Integer>() {{ this.add(3); this.add(7); this.add(31); this.add(127); }},
asSet(3, 7, 31, 127));
}
}