Skip to content

Commit

Permalink
Merge pull request #36 from digipost/nonemptylist-copyof-loosen-typing
Browse files Browse the repository at this point in the history
NonEmptyList copy-constructors accept Collection
  • Loading branch information
runeflobakk authored Feb 20, 2024
2 parents d6c3f99 + e951504 commit 65db4df
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.1</version>
<version>5.10.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -63,7 +63,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-bom</artifactId>
<version>2.0.10</version>
<version>2.0.12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -85,7 +85,7 @@
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>3.15.5</version>
<version>3.15.6</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -113,7 +113,7 @@
<dependency>
<groupId>no.digipost</groupId>
<artifactId>jul-to-slf4j-junit-extension</artifactId>
<version>1.0</version>
<version>1.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -194,7 +194,7 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.3</version>
<version>3.2.5</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
Expand Down Expand Up @@ -244,7 +244,7 @@
<plugin>
<groupId>com.github.siom79.japicmp</groupId>
<artifactId>japicmp-maven-plugin</artifactId>
<version>0.18.3</version>
<version>0.18.5</version>
<configuration>
<parameter>
<includes>
Expand Down
45 changes: 42 additions & 3 deletions src/main/java/no/digipost/collection/NonEmptyList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,7 +37,7 @@
* relied on.
* <p>
* 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.)
Expand Down Expand Up @@ -190,7 +191,22 @@ static <E> NonEmptyList<E> ofUnsafe(E[] nonEmptyArray) {
* or {@link Optional#empty()} if the given list is empty
*/
static <E> Optional<NonEmptyList<E>> copyOf(List<E> list) {
return firstOf(list).map(first -> NonEmptyList.of(first, new ArrayList<>(list.subList(1, list.size()))));
return copyOf((Collection<E>) list);
}


/**
* Try to construct a non-empty list from copying the elements
* of a given collection, which may be empty.
*
* @param <E> 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 <E> Optional<NonEmptyList<E>> copyOf(Collection<E> collection) {
return of(new ArrayList<>(collection));
}


Expand Down Expand Up @@ -228,7 +244,30 @@ static <E> Optional<NonEmptyList<E>> copyOf(E[] array) {
* @see #copyOf(List)
*/
static <E> NonEmptyList<E> copyOfUnsafe(List<E> nonEmptyList) {
return copyOf(nonEmptyList).orElseThrow(() -> new IllegalArgumentException("empty list"));
return copyOfUnsafe((Collection<E>) nonEmptyList);
}


/**
* <strong>Unsafe</strong> construction of non-empty list from copying the
* elements of a collection assumed to be non-empty.
* <p>
* This method should only be used when the given collection is <em>guarantied</em>
* 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 <E> 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 <E> NonEmptyList<E> copyOfUnsafe(Collection<E> nonEmptyCollection) {
return copyOf(nonEmptyCollection).orElseThrow(() -> new IllegalArgumentException("empty list"));
}


Expand Down

0 comments on commit 65db4df

Please sign in to comment.