Skip to content

Commit 8a5d4ee

Browse files
CollectionUtils: Add iterableOf()
1 parent 32a494d commit 8a5d4ee

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

tcases-lib/src/main/java/org/cornutum/tcases/util/CollectionUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77

88
package org.cornutum.tcases.util;
99

10+
import java.util.Arrays;
1011
import java.util.Collection;
12+
import java.util.Collections;
1113
import java.util.Iterator;
14+
import java.util.List;
1215
import java.util.Map;
16+
import java.util.Optional;
1317
import java.util.stream.Stream;
1418
import java.util.stream.StreamSupport;
1519

@@ -35,6 +39,14 @@ public static <T> Stream<T> toStream( Iterator<T> iterator)
3539
return StreamSupport.stream( iterable.spliterator(), false);
3640
}
3741

42+
/**
43+
* If the given value is present, returns it as a singleton list. Otherwise, returns an empty list.
44+
*/
45+
public static <T> List<T> iterableOf( Optional<T> value)
46+
{
47+
return value.map( Arrays::asList).orElse( Collections.emptyList());
48+
}
49+
3850
/**
3951
* Returns a stream containing the members of the given collection.
4052
*/

0 commit comments

Comments
 (0)