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 a1a85912cd..f1be9d3ced 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 @@ -63,6 +63,40 @@ public final class RMCollectionUtils return duplicateElems; } + /** Returns the head (element at index 0) of the provided List. + * + * @param l the list whose head is sought. + * @param the type of the List. + * @return the head element or {@code null} for the empty list. + * @throws NullPointerException if l is {@code null} + */ + public static T head(List l) + { + return l.isEmpty() ? null : l.get(0); + } + + /** + * Returns the tail of the provided List i.e. the sublist which contains + * all elements of the given list except the {@link #head(List) head}. + * + * @param l the list whose tail is sought. + * @param the type of the List. + * @return the tail sublist, which will be an empty list if the provided list had only a single element. + * @throws NullPointerException if l is {@code null} + * @throws UnsupportedOperationException if the provided list was empty. + */ + public static List tail(List l) + { + if (l.isEmpty()) + { + throw new UnsupportedOperationException("Cannot get tail of empty list."); + } + else + { + return l.subList(1, l.size()); + } + } + /** * 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 6f7d3f8c87..76bcb48cbd 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 @@ -20,8 +20,13 @@ 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.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; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils.Difference; @@ -75,4 +80,18 @@ public class RMCollectionUtilsUnitTest assertEquals(Difference.UNCHANGED, diffKey(mapA, mapB, -1)); assertEquals(Difference.CHANGED, diffKey(mapA, mapB, 3)); } + + @Test public void tailsOfLists() + { + assertEquals(asList(2), tail(asList(1, 2))); + assertEquals(emptyList(), tail(asList(1))); + expectedException(UnsupportedOperationException.class, () -> tail(emptyList())); + } + + @Test public void headsOfLists() + { + assertEquals("a", head(asList("a", "b"))); + assertEquals("a", head(asList("a"))); + assertNull(head(emptyList())); + } }