mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Common utility classes developed as part of refactor for RM-2549.
asSet method that works like java.util.Arrays.asList. Also variants of java.util.Arrays.asList that take Supplier<T> rather than T. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/BRANCHES/classified_renditions@111287 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
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 <T> the type of elements in the list.
|
||||||
|
* @return the list with each element retrieved from a {@code Supplier}.
|
||||||
|
*/
|
||||||
|
public static <T> List<T> asListFrom(Supplier<T>... suppliers)
|
||||||
|
{
|
||||||
|
if (suppliers == null || suppliers.length == 0)
|
||||||
|
{
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return Stream.of(suppliers)
|
||||||
|
.map(s -> s.get())
|
||||||
|
.collect(toList());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Set<T> asSetFrom(Supplier<T>... suppliers)
|
||||||
|
{
|
||||||
|
List<T> l = asListFrom(suppliers);
|
||||||
|
Set<T> result = new HashSet<>();
|
||||||
|
result.addAll(l);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This utility method converts a vararg of objects into a Set<T>.
|
||||||
|
*
|
||||||
|
* @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 <T> Set<T> asSet(T... objects)
|
||||||
|
{
|
||||||
|
Set<T> result = new HashSet<>();
|
||||||
|
for (T obj : objects)
|
||||||
|
{
|
||||||
|
result.add(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
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<String> 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.<String>asListFrom());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void asSetShouldProduceSet()
|
||||||
|
{
|
||||||
|
Set<String> expectedSet = new HashSet<>();
|
||||||
|
expectedSet.add("hello");
|
||||||
|
expectedSet.add("world");
|
||||||
|
|
||||||
|
assertEquals(expectedSet, asSet("hello", "hello", "world"));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user