Skip to content

Commit deb27c1

Browse files
Tonnixmaibin
authored andcommitted
Added PR files for BAEL-2031 (eugenp#4844)
* Added source files for BAEL-2031 * Added test files for BAEL-2031
1 parent 51d79fd commit deb27c1

6 files changed

Lines changed: 199 additions & 0 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.nullsafecollectionstreams;
2+
3+
import java.util.Collection;
4+
import java.util.stream.Stream;
5+
import static org.apache.commons.collections4.CollectionUtils.emptyIfNull;
6+
7+
public class NullSafeCollectionStreamsUsingCommonsEmptyIfNull {
8+
9+
/**
10+
* This method shows how to make a null safe stream from a collection through the use of
11+
* emptyIfNull() method from Apache Commons CollectionUtils library
12+
*
13+
* @param collection The collection that is to be converted into a stream
14+
* @return The stream that has been created from the collection or an empty stream if the collection is null
15+
*/
16+
public Stream<String> collectionAsStream(Collection<String> collection) {
17+
return emptyIfNull(collection).stream();
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.nullsafecollectionstreams;
2+
3+
import java.util.Collection;
4+
import java.util.Optional;
5+
import java.util.stream.Stream;
6+
7+
public class NullSafeCollectionStreamsUsingJava8OptionalContainer {
8+
9+
/**
10+
* This method shows how to make a null safe stream from a collection through the use of
11+
* Java SE 8’s Optional Container
12+
*
13+
* @param collection The collection that is to be converted into a stream
14+
* @return The stream that has been created from the collection or an empty stream if the collection is null
15+
*/
16+
public Stream<String> collectionAsStream(Collection<String> collection) {
17+
return Optional.ofNullable(collection)
18+
.map(Collection::stream)
19+
.orElseGet(Stream::empty);
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.nullsafecollectionstreams;
2+
3+
import java.util.Collection;
4+
import java.util.stream.Stream;
5+
6+
public class NullSafeCollectionStreamsUsingNullDereferenceCheck {
7+
8+
/**
9+
* This method shows how to make a null safe stream from a collection through the use of a check
10+
* to prevent null dereferences
11+
*
12+
* @param collection The collection that is to be converted into a stream
13+
* @return The stream that has been created from the collection or an empty stream if the collection is null
14+
*/
15+
public Stream<String> collectionAsStream(Collection<String> collection) {
16+
return collection == null ? Stream.empty() : collection.stream();
17+
}
18+
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.nullsafecollectionstreams;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
import java.util.stream.Stream;
7+
import org.junit.Test;
8+
import static org.junit.Assert.*;
9+
10+
11+
public class NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest {
12+
13+
private final NullSafeCollectionStreamsUsingCommonsEmptyIfNull instance =
14+
new NullSafeCollectionStreamsUsingCommonsEmptyIfNull();
15+
16+
@Test
17+
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
18+
Collection<String> collection = null;
19+
Stream<String> expResult = Stream.empty();
20+
Stream<String> result = instance.collectionAsStream(collection);
21+
assertStreamEquals(expResult, result);
22+
23+
}
24+
25+
@Test
26+
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
27+
28+
Collection<String> collection = Arrays.asList("a", "b", "c");
29+
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
30+
Stream<String> result = instance.collectionAsStream(collection);
31+
assertStreamEquals(expResult, result);
32+
}
33+
34+
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
35+
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
36+
while (iter1.hasNext() && iter2.hasNext())
37+
assertEquals(iter1.next(), iter2.next());
38+
assert !iter1.hasNext() && !iter2.hasNext();
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.baeldung.nullsafecollectionstreams;
7+
8+
import java.util.Arrays;
9+
import java.util.Collection;
10+
import java.util.Iterator;
11+
import java.util.stream.Stream;
12+
import org.junit.After;
13+
import org.junit.AfterClass;
14+
import org.junit.Before;
15+
import org.junit.BeforeClass;
16+
import org.junit.Test;
17+
import static org.junit.Assert.*;
18+
19+
/**
20+
*
21+
* @author Kwaje Anthony <kwajeanthony@gmail.com>
22+
*/
23+
public class NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest {
24+
25+
private final NullSafeCollectionStreamsUsingJava8OptionalContainer instance =
26+
new NullSafeCollectionStreamsUsingJava8OptionalContainer();
27+
28+
@Test
29+
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
30+
Collection<String> collection = null;
31+
Stream<String> expResult = Stream.empty();
32+
Stream<String> result = instance.collectionAsStream(collection);
33+
assertStreamEquals(expResult, result);
34+
35+
}
36+
37+
@Test
38+
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
39+
40+
Collection<String> collection = Arrays.asList("a", "b", "c");
41+
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
42+
Stream<String> result = instance.collectionAsStream(collection);
43+
assertStreamEquals(expResult, result);
44+
}
45+
46+
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
47+
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
48+
while (iter1.hasNext() && iter2.hasNext())
49+
assertEquals(iter1.next(), iter2.next());
50+
assert !iter1.hasNext() && !iter2.hasNext();
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
package com.baeldung.nullsafecollectionstreams;
3+
4+
import java.util.Arrays;
5+
import java.util.Collection;
6+
import java.util.Iterator;
7+
import java.util.stream.Stream;
8+
import org.junit.Test;
9+
import static org.junit.Assert.*;
10+
11+
/**
12+
*
13+
* @author Kwaje Anthony <kwajeanthony@gmail.com>
14+
*/
15+
public class NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest {
16+
17+
private final NullSafeCollectionStreamsUsingNullDereferenceCheck instance =
18+
new NullSafeCollectionStreamsUsingNullDereferenceCheck();
19+
20+
@Test
21+
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
22+
Collection<String> collection = null;
23+
Stream<String> expResult = Stream.empty();
24+
Stream<String> result = instance.collectionAsStream(collection);
25+
assertStreamEquals(expResult, result);
26+
27+
}
28+
29+
@Test
30+
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
31+
32+
Collection<String> collection = Arrays.asList("a", "b", "c");
33+
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
34+
Stream<String> result = instance.collectionAsStream(collection);
35+
assertStreamEquals(expResult, result);
36+
}
37+
38+
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
39+
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
40+
while (iter1.hasNext() && iter2.hasNext())
41+
assertEquals(iter1.next(), iter2.next());
42+
assert !iter1.hasNext() && !iter2.hasNext();
43+
}
44+
45+
}

0 commit comments

Comments
 (0)