BAEL-1473 (#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
This commit is contained in:
parent
6923d3accd
commit
25f449ad38
@ -1,45 +1,19 @@
|
|||||||
package com.baeldung.spliteratorAPI;
|
package com.baeldung.spliteratorAPI;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Spliterator;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.IntStream;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import java.util.stream.StreamSupport;
|
|
||||||
|
|
||||||
public class Executor {
|
public class Executor {
|
||||||
public void executeCustomSpliterator() {
|
|
||||||
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),
|
|
||||||
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),
|
|
||||||
new Author("Alice", 1), new Author("Mike", 0), new Author("Michał", 0), new Author("Loredana", 1)), 0);
|
|
||||||
Stream<Author> stream = IntStream.range(0, article.getListOfAuthors()
|
|
||||||
.size())
|
|
||||||
.mapToObj(article.getListOfAuthors()::get);
|
|
||||||
System.out.println("count= " + countAutors(stream.parallel()));
|
|
||||||
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(article.getListOfAuthors());
|
|
||||||
Stream<Author> stream2 = StreamSupport.stream(spliterator, true);
|
|
||||||
System.out.println("count= " + countAutors(stream2.parallel()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void executeSpliterator() {
|
public static int countAutors(Stream<Author> stream) {
|
||||||
Spliterator<Article> split1 = generateElements().spliterator();
|
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true),
|
||||||
Spliterator<Article> split2 = split1.trySplit();
|
RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
|
||||||
ExecutorService service = Executors.newCachedThreadPool();
|
return wordCounter.getCounter();
|
||||||
service.execute(new Task(split1));
|
}
|
||||||
service.execute(new Task(split2));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int countAutors(Stream<Author> stream) {
|
public static List<Article> generateElements() {
|
||||||
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true), RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
|
return Stream.generate(() -> new Article("Java")).limit(35000).collect(Collectors.toList());
|
||||||
return wordCounter.getCounter();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private List<Article> generateElements() {
|
}
|
||||||
return Stream.generate(() -> new Article("Java"))
|
|
||||||
.limit(35000)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +1,9 @@
|
|||||||
package com.baeldung.spliteratorAPI;
|
package com.baeldung.spliteratorAPI;
|
||||||
|
|
||||||
import java.util.Spliterator;
|
import java.util.Spliterator;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
public class Task implements Runnable {
|
public class Task implements Callable<String> {
|
||||||
private Spliterator<Article> spliterator;
|
private Spliterator<Article> spliterator;
|
||||||
private final static String SUFFIX = "- published by Baeldung";
|
private final static String SUFFIX = "- published by Baeldung";
|
||||||
|
|
||||||
@ -11,7 +12,7 @@ public class Task implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public String call() {
|
||||||
int current = 0;
|
int current = 0;
|
||||||
while (spliterator.tryAdvance(article -> {
|
while (spliterator.tryAdvance(article -> {
|
||||||
article.setName(article.getName()
|
article.setName(article.getName()
|
||||||
@ -20,7 +21,7 @@ public class Task implements Runnable {
|
|||||||
current++;
|
current++;
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
System.out.println(Thread.currentThread()
|
return Thread.currentThread()
|
||||||
.getName() + ":" + current);
|
.getName() + ":" + current;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.spliteratorAPI;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Spliterator;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ExecutorTest {
|
||||||
|
Article article;
|
||||||
|
Stream<Author> stream;
|
||||||
|
Spliterator<Author> spliterator;
|
||||||
|
Spliterator<Article> split1;
|
||||||
|
Spliterator<Article> split2;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
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), 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), new Author("Alice", 1),
|
||||||
|
new Author("Mike", 0), new Author("Michał", 0), new Author("Loredana", 1)), 0);
|
||||||
|
stream = article.getListOfAuthors().stream();
|
||||||
|
split1 = Executor.generateElements().spliterator();
|
||||||
|
split2 = split1.trySplit();
|
||||||
|
spliterator = new RelatedAuthorSpliterator(article.getListOfAuthors());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAstreamOfAuthors_whenProcessedInParallelWithCustomSpliterator_coubtProducessRightOutput() {
|
||||||
|
Stream<Author> stream2 = StreamSupport.stream(spliterator, true);
|
||||||
|
assertThat(Executor.countAutors(stream2.parallel())).isEqualTo(9);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSpliterator_whenAppliedToAListOfArticle_thenSplittedInHalf() {
|
||||||
|
assertThat(new Task(split1).call()).containsSequence(Executor.generateElements().size() / 2 + "");
|
||||||
|
assertThat(new Task(split2).call()).containsSequence(Executor.generateElements().size() / 2 + "");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user