Skip to content

Commit 25f449a

Browse files
gshell-techmaibin
authored andcommitted
BAEL-1473 (eugenp#3477)
* BAEL-1473 Intoduction to Spliterator in Java * BAEL-1473 - Replace .out with logger.info * removed log * BAEL-1473 - added test-cases * modify test-cases
1 parent 6923d3a commit 25f449a

3 files changed

Lines changed: 58 additions & 39 deletions

File tree

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,19 @@
11
package com.baeldung.spliteratorAPI;
22

3-
import java.util.Arrays;
43
import java.util.List;
5-
import java.util.Spliterator;
6-
import java.util.concurrent.ExecutorService;
7-
import java.util.concurrent.Executors;
84
import java.util.stream.Collectors;
9-
import java.util.stream.IntStream;
105
import java.util.stream.Stream;
11-
import java.util.stream.StreamSupport;
126

137
public class Executor {
14-
public void executeCustomSpliterator() {
15-
Article article = new Article(Arrays.asList(new Author("Ahmad", 0), new Author("Eugen", 0), new Author("Alice", 1), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1),
16-
new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0),
17-
new Author("Alice", 1), new Author("Mike", 0), new Author("Michał", 0), new Author("Loredana", 1)), 0);
18-
Stream<Author> stream = IntStream.range(0, article.getListOfAuthors()
19-
.size())
20-
.mapToObj(article.getListOfAuthors()::get);
21-
System.out.println("count= " + countAutors(stream.parallel()));
22-
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(article.getListOfAuthors());
23-
Stream<Author> stream2 = StreamSupport.stream(spliterator, true);
24-
System.out.println("count= " + countAutors(stream2.parallel()));
25-
}
268

27-
public void executeSpliterator() {
28-
Spliterator<Article> split1 = generateElements().spliterator();
29-
Spliterator<Article> split2 = split1.trySplit();
30-
ExecutorService service = Executors.newCachedThreadPool();
31-
service.execute(new Task(split1));
32-
service.execute(new Task(split2));
33-
}
9+
public static int countAutors(Stream<Author> stream) {
10+
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true),
11+
RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
12+
return wordCounter.getCounter();
13+
}
3414

35-
private static int countAutors(Stream<Author> stream) {
36-
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true), RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
37-
return wordCounter.getCounter();
38-
}
15+
public static List<Article> generateElements() {
16+
return Stream.generate(() -> new Article("Java")).limit(35000).collect(Collectors.toList());
17+
}
3918

40-
private List<Article> generateElements() {
41-
return Stream.generate(() -> new Article("Java"))
42-
.limit(35000)
43-
.collect(Collectors.toList());
44-
}
45-
}
19+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.baeldung.spliteratorAPI;
22

33
import java.util.Spliterator;
4+
import java.util.concurrent.Callable;
45

5-
public class Task implements Runnable {
6+
public class Task implements Callable<String> {
67
private Spliterator<Article> spliterator;
78
private final static String SUFFIX = "- published by Baeldung";
89

@@ -11,7 +12,7 @@ public Task(Spliterator<Article> spliterator) {
1112
}
1213

1314
@Override
14-
public void run() {
15+
public String call() {
1516
int current = 0;
1617
while (spliterator.tryAdvance(article -> {
1718
article.setName(article.getName()
@@ -20,7 +21,7 @@ public void run() {
2021
current++;
2122
}
2223
;
23-
System.out.println(Thread.currentThread()
24-
.getName() + ":" + current);
24+
return Thread.currentThread()
25+
.getName() + ":" + current;
2526
}
2627
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.spliteratorAPI;
2+
3+
import java.util.Arrays;
4+
import java.util.Spliterator;
5+
import java.util.stream.Stream;
6+
import java.util.stream.StreamSupport;
7+
8+
import static org.assertj.core.api.Assertions.*;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
public class ExecutorTest {
13+
Article article;
14+
Stream<Author> stream;
15+
Spliterator<Author> spliterator;
16+
Spliterator<Article> split1;
17+
Spliterator<Article> split2;
18+
19+
@Before
20+
public void init() {
21+
article = new Article(Arrays.asList(new Author("Ahmad", 0), new Author("Eugen", 0), new Author("Alice", 1),
22+
new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0),
23+
new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0),
24+
new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1),
25+
new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1),
26+
new Author("Mike", 0), new Author("Michał", 0), new Author("Loredana", 1)), 0);
27+
stream = article.getListOfAuthors().stream();
28+
split1 = Executor.generateElements().spliterator();
29+
split2 = split1.trySplit();
30+
spliterator = new RelatedAuthorSpliterator(article.getListOfAuthors());
31+
}
32+
33+
@Test
34+
public void givenAstreamOfAuthors_whenProcessedInParallelWithCustomSpliterator_coubtProducessRightOutput() {
35+
Stream<Author> stream2 = StreamSupport.stream(spliterator, true);
36+
assertThat(Executor.countAutors(stream2.parallel())).isEqualTo(9);
37+
}
38+
39+
@Test
40+
public void givenSpliterator_whenAppliedToAListOfArticle_thenSplittedInHalf() {
41+
assertThat(new Task(split1).call()).containsSequence(Executor.generateElements().size() / 2 + "");
42+
assertThat(new Task(split2).call()).containsSequence(Executor.generateElements().size() / 2 + "");
43+
}
44+
}

0 commit comments

Comments
 (0)