From 2759b31d47207569082bd5d0b1bb68f49b49efe4 Mon Sep 17 00:00:00 2001 From: Rune Flobakk Date: Tue, 20 Feb 2024 16:14:31 +0100 Subject: [PATCH 1/4] Allow copy-constructors applied to Collections Instead of requiring a List, which is unnecessary strict. This aligns with the JDK copy constructors for lists: public ArrayList(Collection c) https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#ArrayList-java.util.Collection- List.copyOf https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/List.html#copyOf(java.util.Collection) --- .../no/digipost/collection/NonEmptyList.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/java/no/digipost/collection/NonEmptyList.java b/src/main/java/no/digipost/collection/NonEmptyList.java index db919c8..81e7fed 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.) @@ -181,16 +182,16 @@ static NonEmptyList ofUnsafe(E[] nonEmptyArray) { /** * Try to construct a non-empty list from copying the elements - * of a given list, which may be empty. + * of a given collection, which may be empty. * - * @param the type of elements in the list. - * @param list the list, 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 list is empty + * or {@link Optional#empty()} if the given collection is empty */ - static Optional> copyOf(List list) { - return firstOf(list).map(first -> NonEmptyList.of(first, new ArrayList<>(list.subList(1, list.size())))); + static Optional> copyOf(Collection collection) { + return of(new ArrayList<>(collection)); } @@ -211,24 +212,24 @@ static Optional> copyOf(E[] array) { /** * Unsafe construction of non-empty list from copying the - * elements of a list assumed to be non-empty. + * elements of a collection assumed to be non-empty. *

- * This method should only be used when the given list is guarantied + * 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(List)} if you need + * 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 nonEmptyList the list, which is assumed not to be empty + * @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(List) + * @see #copyOf(Collection) */ - static NonEmptyList copyOfUnsafe(List nonEmptyList) { - return copyOf(nonEmptyList).orElseThrow(() -> new IllegalArgumentException("empty list")); + static NonEmptyList copyOfUnsafe(Collection nonEmptyCollection) { + return copyOf(nonEmptyCollection).orElseThrow(() -> new IllegalArgumentException("empty list")); } From e573b9fd3e4eed80a146de8b14eef7109d044a5c Mon Sep 17 00:00:00 2001 From: Rune Flobakk Date: Tue, 20 Feb 2024 16:32:17 +0100 Subject: [PATCH 2/4] Upgrade Maven plugins --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 6e36ad3..de1f3fc 100644 --- a/pom.xml +++ b/pom.xml @@ -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 From 61a715f873f7f89105706dec8d7d5952ebd34091 Mon Sep 17 00:00:00 2001 From: Rune Flobakk Date: Tue, 20 Feb 2024 16:55:13 +0100 Subject: [PATCH 3/4] Keeping methods for backwards compatibility Less specific types in method parameters is not binary backwards compatible :( The methods can and should be deleted for a new major-release. --- .../no/digipost/collection/NonEmptyList.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/main/java/no/digipost/collection/NonEmptyList.java b/src/main/java/no/digipost/collection/NonEmptyList.java index 81e7fed..ccba221 100644 --- a/src/main/java/no/digipost/collection/NonEmptyList.java +++ b/src/main/java/no/digipost/collection/NonEmptyList.java @@ -180,6 +180,21 @@ static NonEmptyList ofUnsafe(E[] nonEmptyArray) { } + /** + * Try to construct a non-empty list from copying the elements + * of a given list, which may be empty. + * + * @param the type of elements in the list. + * @param list the list, which may be empty + * + * @return the resulting non-empty list, + * or {@link Optional#empty()} if the given list is empty + */ + static Optional> copyOf(List list) { + return copyOf((Collection) list); + } + + /** * Try to construct a non-empty list from copying the elements * of a given collection, which may be empty. @@ -210,6 +225,29 @@ static Optional> copyOf(E[] array) { } + /** + * Unsafe construction of non-empty list from copying the + * elements of a list assumed to be non-empty. + *

+ * This method should only be used when the given list 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(List)} if you need + * more flexibility in handling of a possible empty list. + * + * @param the type of elements in the list. + * @param nonEmptyList the list, which is assumed not to be empty + * + * @return the resulting non-empty list + * + * @throws IllegalArgumentException if the given list is empty + * + * @see #copyOf(List) + */ + static NonEmptyList copyOfUnsafe(List nonEmptyList) { + return copyOfUnsafe((Collection) nonEmptyList); + } + + /** * Unsafe construction of non-empty list from copying the * elements of a collection assumed to be non-empty. From e951504d85374e5f42e84e8f4f3c71d6ab3b05df Mon Sep 17 00:00:00 2001 From: Rune Flobakk Date: Tue, 20 Feb 2024 16:59:17 +0100 Subject: [PATCH 4/4] Upgrade test-dependencies --- pom.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index de1f3fc..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