Rename new method

This commit is contained in:
Gary Gregory 2022-07-16 11:14:44 -04:00
parent e0818e3383
commit 152b1777fd
2 changed files with 33 additions and 33 deletions

View File

@ -530,7 +530,7 @@ public class Streams {
* @since 3.13.0
*/
public static <T> FailableStream<T> failableStream(final Collection<T> stream) {
return failableStream(toStream(stream));
return failableStream(of(stream));
}
/**
@ -578,7 +578,7 @@ public class Streams {
}
private static <E> Stream<E> filter(final Collection<E> collection, final Predicate<? super E> predicate) {
return toStream(collection).filter(predicate);
return of(collection).filter(predicate);
}
/**
@ -597,7 +597,7 @@ public class Streams {
* @since 3.13.0
*/
public static <E> Stream<E> instancesOf(final Class<? super E> clazz, final Collection<? super E> collection) {
return instancesOf(clazz, toStream(collection));
return instancesOf(clazz, of(collection));
}
@SuppressWarnings("unchecked") // After the isInstance check, we still need to type-cast.
@ -617,6 +617,18 @@ public class Streams {
return filter(collection, Objects::nonNull);
}
/**
* Delegates to {@link Collection#stream()} or returns {@link Stream#empty()} if the collection is null.
*
* @param <E> the type of elements in the collection.
* @param collection the collection to stream or null.
* @return {@link Collection#stream()} or {@link Stream#empty()} if the collection is null.
* @since 3.13.0
*/
public static <E> Stream<E> of(final Collection<E> collection) {
return collection == null ? Stream.empty() : collection.stream();
}
/**
* Streams the elements of the given enumeration in order.
*
@ -742,16 +754,4 @@ public class Streams {
public static <T extends Object> Collector<T, ?, T[]> toArray(final Class<T> pElementType) {
return new ArrayCollector<>(pElementType);
}
/**
* Delegates to {@link Collection#stream()} or returns {@link Stream#empty()} if the collection is null.
*
* @param <E> the type of elements in the collection.
* @param collection the collection to stream or null.
* @return {@link Collection#stream()} or {@link Stream#empty()} if the collection is null.
* @since 3.13.0
*/
public static <E> Stream<E> toStream(final Collection<E> collection) {
return collection == null ? Stream.empty() : collection.stream();
}
}

View File

@ -133,13 +133,6 @@ public class StreamsTest extends AbstractLangTest {
}));
}
@Test
public void testNullSafeStreamNotNull() {
assertEquals(2, Streams.nonNull(Arrays.asList("A", "B")).collect(Collectors.toList()).size());
assertEquals(2, Streams.nonNull(Arrays.asList(null, "A", null, "B", null)).collect(Collectors.toList()).size());
assertEquals(0, Streams.nonNull(Arrays.asList(null, null)).collect(Collectors.toList()).size());
}
@Test
public void testInstanceOfStream() {
assertEquals(2, Streams.instancesOf(String.class, Arrays.asList("A", "B")).collect(Collectors.toList()).size());
@ -150,6 +143,13 @@ public class StreamsTest extends AbstractLangTest {
assertEquals(2, Streams.instancesOf(String.class, objects).collect(Collectors.toList()).size());
}
@Test
public void testNullSafeStreamNotNull() {
assertEquals(2, Streams.nonNull(Arrays.asList("A", "B")).collect(Collectors.toList()).size());
assertEquals(2, Streams.nonNull(Arrays.asList(null, "A", null, "B", null)).collect(Collectors.toList()).size());
assertEquals(0, Streams.nonNull(Arrays.asList(null, null)).collect(Collectors.toList()).size());
}
@Test
public void testNullSafeStreamNull() {
final List<String> input = null;
@ -163,6 +163,17 @@ public class StreamsTest extends AbstractLangTest {
assertEquals(2, Streams.of("foo", "bar").count());
}
@Test
public void testOfCollectionNotNull() {
assertEquals(2, Streams.of(Arrays.asList("A", "B")).collect(Collectors.toList()).size());
}
@Test
public void testOfCollectionNull() {
final List<String> input = null;
assertEquals(0, Streams.of(input).collect(Collectors.toList()).size());
}
@Test
public void testOfEnumeration() {
final Hashtable<String, Integer> table = new Hashtable<>();
@ -235,15 +246,4 @@ public class StreamsTest extends AbstractLangTest {
assertEquals("1", array[2]);
}
@Test
public void testToStreamNotNull() {
assertEquals(2, Streams.toStream(Arrays.asList("A", "B")).collect(Collectors.toList()).size());
}
@Test
public void testToStreamNull() {
final List<String> input = null;
assertEquals(0, Streams.toStream(input).collect(Collectors.toList()).size());
}
}