diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/FPUtils.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/FPUtils.java new file mode 100644 index 0000000000..d0e49771f9 --- /dev/null +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/FPUtils.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2005-2015 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.module.org_alfresco_module_rm.test.util; + +import static java.util.stream.Collectors.toList; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Supplier; +import java.util.stream.Stream; + +/** + * Utility class to help with Java 8 FP stuff. + * + * @author Neil Mc Erlean + * @since 3.0.a + */ +public class FPUtils +{ + /** + * This method is intended to work exactly like {@code java.util.Arrays.asList()} but it takes + * a vararg of {@code Supplier}s instead of actual objects. + * + * @param suppliers a vararg of {@link Supplier}s giving a sequence of values for the list. + * @param the type of elements in the list. + * @return the list with each element retrieved from a {@code Supplier}. + */ + public static List asListFrom(Supplier... suppliers) + { + if (suppliers == null || suppliers.length == 0) + { + return Collections.emptyList(); + } + else + { + return Stream.of(suppliers) + .map(s -> s.get()) + .collect(toList()); + } + } + + public static Set asSetFrom(Supplier... suppliers) + { + List l = asListFrom(suppliers); + Set result = new HashSet<>(); + result.addAll(l); + return result; + } + + /** + * This utility method converts a vararg of objects into a Set. + * + * @param objects the objects to be added to the set + * @return a Set of objects (any equal objects will of course not be duplicated) + */ + public static Set asSet(T... objects) + { + Set result = new HashSet<>(); + for (T obj : objects) + { + result.add(obj); + } + + return result; + } +} \ No newline at end of file diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/FPUtilsUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/FPUtilsUnitTest.java new file mode 100644 index 0000000000..206687295a --- /dev/null +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/FPUtilsUnitTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2005-2015 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.module.org_alfresco_module_rm.test.util; + +import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asListFrom; +import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asSet; +import static org.junit.Assert.assertEquals; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.Test; + +/** + * Unit tests for {@link FPUtils}. + * + * @author Neil Mc Erlean + * @since 3.0.a + */ +public class FPUtilsUnitTest +{ + @Test public void asListShouldProduceList() + { + List l = asListFrom(() -> "hello", + () -> "world", + () -> { + String s1 = "abc"; + String s2 = "xyz"; + return s1 + s2; + }); + assertEquals(asList("hello", "world", "abcxyz"), l); + } + + @Test public void asListShouldWorkForEmptyVarArgs() + { + assertEquals(emptyList(), FPUtils.asListFrom()); + } + + @Test public void asSetShouldProduceSet() + { + Set expectedSet = new HashSet<>(); + expectedSet.add("hello"); + expectedSet.add("world"); + + assertEquals(expectedSet, asSet("hello", "hello", "world")); + } +} \ No newline at end of file