Add Streams.of(Iterator)

This commit is contained in:
Gary Gregory 2022-08-12 14:36:02 -04:00
parent ec93f3b7f5
commit 717f163a6d
3 changed files with 30 additions and 35 deletions

View File

@ -29,11 +29,11 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.apache.commons.lang3.function.Suppliers; import org.apache.commons.lang3.function.Suppliers;
import org.apache.commons.lang3.function.ToBooleanBiFunction; import org.apache.commons.lang3.function.ToBooleanBiFunction;
import org.apache.commons.lang3.stream.LangCollectors; import org.apache.commons.lang3.stream.LangCollectors;
import org.apache.commons.lang3.stream.Streams;
/** /**
* <p>Operations on {@link java.lang.String} that are * <p>Operations on {@link java.lang.String} that are
@ -124,8 +124,6 @@ import org.apache.commons.lang3.stream.LangCollectors;
//@Immutable //@Immutable
public class StringUtils { public class StringUtils {
private static final int STRING_BUILDER_SIZE = 256;
// Performance testing notes (JDK 1.4, Jul03, scolebourne) // Performance testing notes (JDK 1.4, Jul03, scolebourne)
// Whitespace: // Whitespace:
// Character.isWhitespace() is faster than WHITESPACE.indexOf() // Character.isWhitespace() is faster than WHITESPACE.indexOf()
@ -4386,7 +4384,6 @@ public class StringUtils {
* @since 2.0 * @since 2.0
*/ */
public static String join(final Iterator<?> iterator, final char separator) { public static String join(final Iterator<?> iterator, final char separator) {
// handle null, zero and one elements before building a buffer // handle null, zero and one elements before building a buffer
if (iterator == null) { if (iterator == null) {
return null; return null;
@ -4394,22 +4391,7 @@ public class StringUtils {
if (!iterator.hasNext()) { if (!iterator.hasNext()) {
return EMPTY; return EMPTY;
} }
return Streams.of(iterator).collect(LangCollectors.joining(toStringOrEmpty(String.valueOf(separator)), EMPTY, EMPTY, StringUtils::toStringOrEmpty));
// two or more elements
final StringBuilder buf = new StringBuilder(STRING_BUILDER_SIZE); // Java default is 16, probably too small
while (iterator.hasNext()) {
final Object obj = iterator.next();
if (obj != null) {
buf.append(obj);
}
if (iterator.hasNext()) {
buf.append(separator);
}
}
return buf.toString();
} }
/** /**
@ -4433,20 +4415,7 @@ public class StringUtils {
if (!iterator.hasNext()) { if (!iterator.hasNext()) {
return EMPTY; return EMPTY;
} }
return Streams.of(iterator).collect(LangCollectors.joining(toStringOrEmpty(separator), EMPTY, EMPTY, StringUtils::toStringOrEmpty));
// two or more elements
final StringBuilder buf = new StringBuilder(STRING_BUILDER_SIZE); // Java default is 16, probably too small
while (iterator.hasNext()) {
final Object obj = iterator.next();
if (obj != null) {
buf.append(obj);
}
if (separator != null && iterator.hasNext()) {
buf.append(separator);
}
}
return buf.toString();
} }
/** /**
@ -4727,7 +4696,7 @@ public class StringUtils {
* {@code endIndex > array.length()} * {@code endIndex > array.length()}
*/ */
public static String join(final Object[] array, final String delimiter, final int startIndex, final int endIndex) { public static String join(final Object[] array, final String delimiter, final int startIndex, final int endIndex) {
return array != null ? Stream.of(array).skip(startIndex).limit(Math.max(0, endIndex - startIndex)) return array != null ? Streams.of(array).skip(startIndex).limit(Math.max(0, endIndex - startIndex))
.collect(LangCollectors.joining(delimiter, EMPTY, EMPTY, StringUtils::toStringOrEmpty)) : null; .collect(LangCollectors.joining(delimiter, EMPTY, EMPTY, StringUtils::toStringOrEmpty)) : null;
} }

View File

@ -20,10 +20,12 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.Spliterator; import java.util.Spliterator;
import java.util.Spliterators;
import java.util.Spliterators.AbstractSpliterator; import java.util.Spliterators.AbstractSpliterator;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.BinaryOperator; import java.util.function.BinaryOperator;
@ -653,6 +655,18 @@ public class Streams {
return iterable == null ? Stream.empty() : StreamSupport.stream(iterable.spliterator(), false); return iterable == null ? Stream.empty() : StreamSupport.stream(iterable.spliterator(), false);
} }
/**
* Creates a stream on the given Iterator.
*
* @param <E> the type of elements in the Iterator.
* @param iterator the Iterator to stream or null.
* @return a new Stream or {@link Stream#empty()} if the Iterator is null.
* @since 3.13.0
*/
public static <E> Stream<E> of(final Iterator<E> iterator) {
return iterator == null ? Stream.empty() : StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false);
}
/** /**
* Null-safe version of {@link Stream#of(Object[])}. * Null-safe version of {@link Stream#of(Object[])}.
* *

View File

@ -31,6 +31,7 @@ import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -199,6 +200,17 @@ public class StreamsTest extends AbstractLangTest {
assertEquals(0, Streams.of(input).collect(Collectors.toList()).size()); assertEquals(0, Streams.of(input).collect(Collectors.toList()).size());
} }
@Test
public void testOfIteratorNotNull() {
assertEquals(2, Streams.of(Arrays.asList("A", "B").iterator()).collect(Collectors.toList()).size());
}
@Test
public void testOfIteratorNull() {
final Iterator<String> input = null;
assertEquals(0, Streams.of(input).collect(Collectors.toList()).size());
}
@Test @Test
public void testSimpleStreamFilter() { public void testSimpleStreamFilter() {
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6"); final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");