Skip to content

Commit 65db4df

Browse files
authored
Merge pull request #36 from digipost/nonemptylist-copyof-loosen-typing
NonEmptyList copy-constructors accept Collection
2 parents d6c3f99 + e951504 commit 65db4df

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

pom.xml

+6-6
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<dependency>
5050
<groupId>org.junit</groupId>
5151
<artifactId>junit-bom</artifactId>
52-
<version>5.10.1</version>
52+
<version>5.10.2</version>
5353
<type>pom</type>
5454
<scope>import</scope>
5555
</dependency>
@@ -63,7 +63,7 @@
6363
<dependency>
6464
<groupId>org.slf4j</groupId>
6565
<artifactId>slf4j-bom</artifactId>
66-
<version>2.0.10</version>
66+
<version>2.0.12</version>
6767
<type>pom</type>
6868
<scope>import</scope>
6969
</dependency>
@@ -85,7 +85,7 @@
8585
<dependency>
8686
<groupId>nl.jqno.equalsverifier</groupId>
8787
<artifactId>equalsverifier</artifactId>
88-
<version>3.15.5</version>
88+
<version>3.15.6</version>
8989
<scope>test</scope>
9090
</dependency>
9191
<dependency>
@@ -113,7 +113,7 @@
113113
<dependency>
114114
<groupId>no.digipost</groupId>
115115
<artifactId>jul-to-slf4j-junit-extension</artifactId>
116-
<version>1.0</version>
116+
<version>1.0.1</version>
117117
<scope>test</scope>
118118
</dependency>
119119
<dependency>
@@ -194,7 +194,7 @@
194194
</plugin>
195195
<plugin>
196196
<artifactId>maven-surefire-plugin</artifactId>
197-
<version>3.2.3</version>
197+
<version>3.2.5</version>
198198
</plugin>
199199
<plugin>
200200
<artifactId>maven-deploy-plugin</artifactId>
@@ -244,7 +244,7 @@
244244
<plugin>
245245
<groupId>com.github.siom79.japicmp</groupId>
246246
<artifactId>japicmp-maven-plugin</artifactId>
247-
<version>0.18.3</version>
247+
<version>0.18.5</version>
248248
<configuration>
249249
<parameter>
250250
<includes>

src/main/java/no/digipost/collection/NonEmptyList.java

+42-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import no.digipost.stream.NonEmptyStream;
1919

2020
import java.util.ArrayList;
21+
import java.util.Collection;
2122
import java.util.List;
2223
import java.util.Optional;
2324

@@ -36,7 +37,7 @@
3637
* relied on.
3738
* <p>
3839
* If you need to construct non-empty lists based on another container you
39-
* need to further mutate, consider using one of the {@link #copyOf(List) copyOf}
40+
* need to further mutate, consider using one of the {@link #copyOf(Collection) copyOf}
4041
* constructors, which will copy the given references so that any subsequent changes to the
4142
* to the given source container will not be reflected in the created non-empty list. (As usual,
4243
* this is a shallow copy, meaning that the instances themselves can of course be mutated by anything.)
@@ -190,7 +191,22 @@ static <E> NonEmptyList<E> ofUnsafe(E[] nonEmptyArray) {
190191
* or {@link Optional#empty()} if the given list is empty
191192
*/
192193
static <E> Optional<NonEmptyList<E>> copyOf(List<E> list) {
193-
return firstOf(list).map(first -> NonEmptyList.of(first, new ArrayList<>(list.subList(1, list.size()))));
194+
return copyOf((Collection<E>) list);
195+
}
196+
197+
198+
/**
199+
* Try to construct a non-empty list from copying the elements
200+
* of a given collection, which may be empty.
201+
*
202+
* @param <E> the type of elements in the collection.
203+
* @param collection the collection, which may be empty
204+
*
205+
* @return the resulting non-empty list,
206+
* or {@link Optional#empty()} if the given collection is empty
207+
*/
208+
static <E> Optional<NonEmptyList<E>> copyOf(Collection<E> collection) {
209+
return of(new ArrayList<>(collection));
194210
}
195211

196212

@@ -228,7 +244,30 @@ static <E> Optional<NonEmptyList<E>> copyOf(E[] array) {
228244
* @see #copyOf(List)
229245
*/
230246
static <E> NonEmptyList<E> copyOfUnsafe(List<E> nonEmptyList) {
231-
return copyOf(nonEmptyList).orElseThrow(() -> new IllegalArgumentException("empty list"));
247+
return copyOfUnsafe((Collection<E>) nonEmptyList);
248+
}
249+
250+
251+
/**
252+
* <strong>Unsafe</strong> construction of non-empty list from copying the
253+
* elements of a collection assumed to be non-empty.
254+
* <p>
255+
* This method should only be used when the given collection is <em>guarantied</em>
256+
* to be empty, and thus offers a fail-fast way to introduce the non-empty
257+
* quality on a type level. Use {@link #copyOf(Collection)} if you need
258+
* more flexibility in handling of a possible empty list.
259+
*
260+
* @param <E> the type of elements in the list.
261+
* @param nonEmptyCollection the collection, which is assumed not to be empty
262+
*
263+
* @return the resulting non-empty list
264+
*
265+
* @throws IllegalArgumentException if the given list is empty
266+
*
267+
* @see #copyOf(Collection)
268+
*/
269+
static <E> NonEmptyList<E> copyOfUnsafe(Collection<E> nonEmptyCollection) {
270+
return copyOf(nonEmptyCollection).orElseThrow(() -> new IllegalArgumentException("empty list"));
232271
}
233272

234273

0 commit comments

Comments
 (0)