Skip to content

Commit

Permalink
Return an empty stream if passed array is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Feb 22, 2017
1 parent 5d437be commit 178c6cb
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions stream/src/main/java/com/annimon/stream/DoubleStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public static DoubleStream of(PrimitiveIterator.OfDouble iterator) {
*/
public static DoubleStream of(final double... values) {
Objects.requireNonNull(values);
if (values.length == 0) {
return DoubleStream.empty();
}
return new DoubleStream(new DoubleArray(values));
}

Expand Down
3 changes: 3 additions & 0 deletions stream/src/main/java/com/annimon/stream/IntStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public static IntStream of(PrimitiveIterator.OfInt iterator) {
*/
public static IntStream of(final int... values) {
Objects.requireNonNull(values);
if (values.length == 0) {
return IntStream.empty();
}
return new IntStream(new IntArray(values));
}

Expand Down
3 changes: 3 additions & 0 deletions stream/src/main/java/com/annimon/stream/LongStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public static LongStream of(PrimitiveIterator.OfLong iterator) {
*/
public static LongStream of(final long... values) {
Objects.requireNonNull(values);
if (values.length == 0) {
return LongStream.empty();
}
return new LongStream(new LongArray(values));
}

Expand Down
3 changes: 3 additions & 0 deletions stream/src/main/java/com/annimon/stream/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public static <T> Stream<T> of(Iterable<? extends T> iterable) {
*/
public static <T> Stream<T> of(final T... elements) {
Objects.requireNonNull(elements);
if (elements.length == 0) {
return Stream.<T>empty();
}
return new Stream<T>(new ObjArray<T>(elements));
}

Expand Down
7 changes: 7 additions & 0 deletions stream/src/test/java/com/annimon/stream/DoubleStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.NoSuchElementException;
import org.junit.Test;
import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertElements;
import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertIsEmpty;
import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.elements;
import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.isEmpty;
import static com.annimon.stream.test.hamcrest.OptionalDoubleMatcher.hasValue;
Expand Down Expand Up @@ -80,6 +81,12 @@ public void testStreamOfDoublesNull() {
DoubleStream.of((double[]) null);
}

@Test
public void testStreamOfEmptyArray() {
DoubleStream.of(new double[0])
.custom(assertIsEmpty());
}

@Test
public void testStreamOfDouble() {
assertThat(DoubleStream.of(1.234), elements(arrayContaining(1.234)));
Expand Down
9 changes: 8 additions & 1 deletion stream/src/test/java/com/annimon/stream/IntStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hamcrest.Matchers;
import org.junit.Test;
import static com.annimon.stream.test.hamcrest.IntStreamMatcher.assertElements;
import static com.annimon.stream.test.hamcrest.IntStreamMatcher.assertIsEmpty;
import static com.annimon.stream.test.hamcrest.OptionalIntMatcher.hasValue;
import static com.annimon.stream.test.hamcrest.StreamMatcher.elements;
import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -74,14 +75,20 @@ public void testStreamOfInts() {

assertTrue(IntStream.of(data1).count() == 5);
assertTrue(IntStream.of(data2).findFirst().getAsInt() == 42);
assertTrue(!IntStream.of(data3).findFirst().isPresent());
assertFalse(IntStream.of(data3).findFirst().isPresent());
}

@Test(expected = NullPointerException.class)
public void testStreamOfIntsNull() {
IntStream.of((int[]) null);
}

@Test
public void testStreamOfEmptyArray() {
IntStream.of(new int[0])
.custom(assertIsEmpty());
}

@Test
public void testStreamOfInt() {
assertTrue(IntStream.of(42).count() == 1);
Expand Down
7 changes: 7 additions & 0 deletions stream/src/test/java/com/annimon/stream/LongStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.NoSuchElementException;
import org.junit.Test;
import static com.annimon.stream.test.hamcrest.LongStreamMatcher.assertElements;
import static com.annimon.stream.test.hamcrest.LongStreamMatcher.assertIsEmpty;
import static com.annimon.stream.test.hamcrest.LongStreamMatcher.elements;
import static com.annimon.stream.test.hamcrest.LongStreamMatcher.isEmpty;
import static com.annimon.stream.test.hamcrest.OptionalLongMatcher.hasValue;
Expand Down Expand Up @@ -75,6 +76,12 @@ public void testStreamOfLongsNull() {
LongStream.of((long[]) null);
}

@Test
public void testStreamOfEmptyArray() {
LongStream.of(new long[0])
.custom(assertIsEmpty());
}

@Test
public void testStreamOfLong() {
assertThat(LongStream.of(1234), elements(arrayContaining(1234L)));
Expand Down
7 changes: 7 additions & 0 deletions stream/src/test/java/com/annimon/stream/StreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.annimon.stream.function.UnaryOperator;
import com.annimon.stream.test.hamcrest.DoubleStreamMatcher;
import com.annimon.stream.test.hamcrest.OptionalMatcher;
import com.annimon.stream.test.hamcrest.StreamMatcher;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -91,6 +92,12 @@ public void testStreamOfMapNull() {
Stream.of((Map<?, ?>)null);
}

@Test
public void testStreamOfEmptyArray() {
Stream.of(new String[0])
.custom(StreamMatcher.<String>assertIsEmpty());
}

@Test
public void testStreamOfIterator() {
final PrintConsumer<Integer> consumer = new PrintConsumer<Integer>();
Expand Down

0 comments on commit 178c6cb

Please sign in to comment.