diff --git a/pom.xml b/pom.xml index 6e36ad3..a297970 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,7 @@ org.junit junit-bom - 5.10.1 + 5.10.2 pom import @@ -63,7 +63,7 @@ org.slf4j slf4j-bom - 2.0.10 + 2.0.12 pom import @@ -85,7 +85,7 @@ nl.jqno.equalsverifier equalsverifier - 3.15.5 + 3.15.6 test @@ -113,7 +113,7 @@ no.digipost jul-to-slf4j-junit-extension - 1.0 + 1.0.1 test @@ -194,7 +194,7 @@ maven-surefire-plugin - 3.2.3 + 3.2.5 maven-deploy-plugin @@ -244,7 +244,7 @@ com.github.siom79.japicmp japicmp-maven-plugin - 0.18.3 + 0.18.5 diff --git a/src/main/java/no/digipost/collection/NonEmptyList.java b/src/main/java/no/digipost/collection/NonEmptyList.java index db919c8..ccba221 100644 --- a/src/main/java/no/digipost/collection/NonEmptyList.java +++ b/src/main/java/no/digipost/collection/NonEmptyList.java @@ -18,6 +18,7 @@ import no.digipost.stream.NonEmptyStream; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Optional; @@ -36,7 +37,7 @@ * relied on. *

* If you need to construct non-empty lists based on another container you - * need to further mutate, consider using one of the {@link #copyOf(List) copyOf} + * need to further mutate, consider using one of the {@link #copyOf(Collection) copyOf} * constructors, which will copy the given references so that any subsequent changes to the * to the given source container will not be reflected in the created non-empty list. (As usual, * this is a shallow copy, meaning that the instances themselves can of course be mutated by anything.) @@ -190,7 +191,22 @@ static NonEmptyList ofUnsafe(E[] nonEmptyArray) { * or {@link Optional#empty()} if the given list is empty */ static Optional> copyOf(List list) { - return firstOf(list).map(first -> NonEmptyList.of(first, new ArrayList<>(list.subList(1, list.size())))); + return copyOf((Collection) list); + } + + + /** + * Try to construct a non-empty list from copying the elements + * of a given collection, which may be empty. + * + * @param the type of elements in the collection. + * @param collection the collection, which may be empty + * + * @return the resulting non-empty list, + * or {@link Optional#empty()} if the given collection is empty + */ + static Optional> copyOf(Collection collection) { + return of(new ArrayList<>(collection)); } @@ -228,7 +244,30 @@ static Optional> copyOf(E[] array) { * @see #copyOf(List) */ static NonEmptyList copyOfUnsafe(List nonEmptyList) { - return copyOf(nonEmptyList).orElseThrow(() -> new IllegalArgumentException("empty list")); + return copyOfUnsafe((Collection) nonEmptyList); + } + + + /** + * Unsafe construction of non-empty list from copying the + * elements of a collection assumed to be non-empty. + *

+ * This method should only be used when the given collection is guarantied + * to be empty, and thus offers a fail-fast way to introduce the non-empty + * quality on a type level. Use {@link #copyOf(Collection)} if you need + * more flexibility in handling of a possible empty list. + * + * @param the type of elements in the list. + * @param nonEmptyCollection the collection, which is assumed not to be empty + * + * @return the resulting non-empty list + * + * @throws IllegalArgumentException if the given list is empty + * + * @see #copyOf(Collection) + */ + static NonEmptyList copyOfUnsafe(Collection nonEmptyCollection) { + return copyOf(nonEmptyCollection).orElseThrow(() -> new IllegalArgumentException("empty list")); }