diff --git a/src/main/java/org/hisp/dhis/model/AttributeValue.java b/src/main/java/org/hisp/dhis/model/AttributeValue.java index e2a212f0..ca2ca4d4 100644 --- a/src/main/java/org/hisp/dhis/model/AttributeValue.java +++ b/src/main/java/org/hisp/dhis/model/AttributeValue.java @@ -50,6 +50,15 @@ public AttributeValue(Attribute attribute, String value) { this.value = value; } + /** + * Indicates whether the attribute is not null. + * + * @return true if the attribute is not null. + */ + public boolean hasAttribute() { + return attribute != null; + } + /** * Indicates whether the value is not null or empty. * diff --git a/src/main/java/org/hisp/dhis/util/CollectionUtils.java b/src/main/java/org/hisp/dhis/util/CollectionUtils.java index 4035a52e..876a92ca 100644 --- a/src/main/java/org/hisp/dhis/util/CollectionUtils.java +++ b/src/main/java/org/hisp/dhis/util/CollectionUtils.java @@ -109,24 +109,26 @@ public static List mutableList(T... items) { } /** - * Returns a sublist of the given list of the given length, starting from the beginning at index - * 0. Does not throw any exceptions. Returns an empty list of the given items is null, or of the - * given length is less than or equal to zero. + * Returns a sublist of the given list of the given length starting from the beginning (i.e. index + * 0). Does not throw exceptions. Returns an empty list if the given items is null, or if the + * given size is less than or equal to zero. Returns a copy of the given list if the list size is + * less than or equal to the given size. * * @param type. * @param items the items. + * @param size the size of the sublist. * @return a sublist. */ - public static List sublist(List items, int length) { - if (empty(items) || length <= 0) { + public static List sublist(List items, int size) { + if (empty(items) || size <= 0) { return List.of(); } - if (items.size() <= length) { + if (items.size() <= size) { return new ArrayList<>(items); } - return new ArrayList<>(items.subList(0, length)); + return new ArrayList<>(items.subList(0, size)); } /** @@ -416,6 +418,30 @@ public static Optional first(Collection collection) { return empty(collection) ? Optional.empty() : Optional.ofNullable(collection.iterator().next()); } + /** + * Returns an optional last item in the given list. Returns an empty optional if the given list is + * null or empty, or if the last item is null. + * + * @param type. + * @param list the list. + * @return an optional last item in the given list. + */ + public static Optional last(List list) { + return empty(list) ? Optional.empty() : Optional.ofNullable(list.get(list.size() - 1)); + } + + /** + * Indicates if the given index is the last index in the given list. Returns false if the list is + * null or empty, or if the index is negative or out of bounds. + * + * @param list the list. + * @param index the index. + * @return true if the index is the last index in the list. + */ + public static boolean isLast(List list, int index) { + return list != null && !list.isEmpty() && index >= 0 && index == list.size() - 1; + } + /** * Converts the given collection to a comma separated string value of the joined string * representation of each item. Returns null if the given collection is null or empty. diff --git a/src/test/java/org/hisp/dhis/util/CollectionUtilsTest.java b/src/test/java/org/hisp/dhis/util/CollectionUtilsTest.java index 058830b7..4289bd5b 100644 --- a/src/test/java/org/hisp/dhis/util/CollectionUtilsTest.java +++ b/src/test/java/org/hisp/dhis/util/CollectionUtilsTest.java @@ -40,6 +40,8 @@ import static org.hisp.dhis.util.CollectionUtils.firstNonEmptyList; import static org.hisp.dhis.util.CollectionUtils.get; import static org.hisp.dhis.util.CollectionUtils.index; +import static org.hisp.dhis.util.CollectionUtils.isLast; +import static org.hisp.dhis.util.CollectionUtils.last; import static org.hisp.dhis.util.CollectionUtils.list; import static org.hisp.dhis.util.CollectionUtils.mapJoin; import static org.hisp.dhis.util.CollectionUtils.mapToCommaSeparated; @@ -400,6 +402,29 @@ void testFirst() { assertTrue(first(list(null, "b", "c")).isEmpty()); } + @Test + void testLast() { + assertEquals("c", last(list("a", "b", "c")).get()); + assertFalse(last(list("a", "b", "c")).isEmpty()); + assertTrue(last(null).isEmpty()); + assertTrue(last(list()).isEmpty()); + assertTrue(last(list("a", "b", null)).isEmpty()); + } + + @Test + void testIsLast() { + List list = list("a", "b", "c"); + + assertFalse(isLast(list, 0)); + assertFalse(isLast(list, 1)); + assertTrue(isLast(list, 2)); + + assertFalse(isLast(list, -1)); + assertFalse(isLast(list, 3)); + assertFalse(isLast(null, 0)); + assertFalse(isLast(list(), 0)); + } + @Test void testToCommaSeparatedNullEmpty() { assertNull(toCommaSeparated(null));