|
18 | 18 | import no.digipost.stream.NonEmptyStream;
|
19 | 19 |
|
20 | 20 | import java.util.ArrayList;
|
| 21 | +import java.util.Collection; |
21 | 22 | import java.util.List;
|
22 | 23 | import java.util.Optional;
|
23 | 24 |
|
|
36 | 37 | * relied on.
|
37 | 38 | * <p>
|
38 | 39 | * 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} |
40 | 41 | * constructors, which will copy the given references so that any subsequent changes to the
|
41 | 42 | * to the given source container will not be reflected in the created non-empty list. (As usual,
|
42 | 43 | * 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) {
|
190 | 191 | * or {@link Optional#empty()} if the given list is empty
|
191 | 192 | */
|
192 | 193 | 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)); |
194 | 210 | }
|
195 | 211 |
|
196 | 212 |
|
@@ -228,7 +244,30 @@ static <E> Optional<NonEmptyList<E>> copyOf(E[] array) {
|
228 | 244 | * @see #copyOf(List)
|
229 | 245 | */
|
230 | 246 | 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")); |
232 | 271 | }
|
233 | 272 |
|
234 | 273 |
|
|
0 commit comments