From 022bbcbde5e07c41c45420cf7a57cc4a205997be Mon Sep 17 00:00:00 2001 From: Neil McErlean Date: Tue, 8 Dec 2015 11:21:30 +0000 Subject: [PATCH] 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 --- .../util/RMCollectionUtils.java | 18 ++++++++++++++++++ .../util/RMCollectionUtilsUnitTest.java | 10 ++++++++++ 2 files changed, 28 insertions(+) diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtils.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtils.java index add7e6a703..d7549731aa 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtils.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtils.java @@ -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 the element type. + * @return a Set containing all the provided elements (without duplicates). + */ + public static HashSet asSet(T... elements) + { + final HashSet 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. */ diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtilsUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtilsUnitTest.java index 4c8e9240db..4f02e3a24b 100644 --- a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtilsUnitTest.java +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtilsUnitTest.java @@ -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() {{ this.add("hello"); this.add("world"); }}, + asSet("hello", "world")); + assertEquals(new HashSet() {{ this.add(3); this.add(7); this.add(31); this.add(127); }}, + asSet(3, 7, 31, 127)); + } }