* 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:
Ahmad Alsanie 2018-01-21 19:44:22 +02:00 committed by maibin
parent 6923d3accd
commit 25f449ad38
3 changed files with 58 additions and 39 deletions

View File

@ -1,45 +1,19 @@
package com.baeldung.spliteratorAPI;
import java.util.Arrays;
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.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
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() {
Spliterator<Article> split1 = generateElements().spliterator();
Spliterator<Article> split2 = split1.trySplit();
ExecutorService service = Executors.newCachedThreadPool();
service.execute(new Task(split1));
service.execute(new Task(split2));
}
public static int countAutors(Stream<Author> stream) {
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true),
RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
return wordCounter.getCounter();
}
private static int countAutors(Stream<Author> stream) {
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true), RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
return wordCounter.getCounter();
}
public static List<Article> generateElements() {
return Stream.generate(() -> new Article("Java")).limit(35000).collect(Collectors.toList());
}
private List<Article> generateElements() {
return Stream.generate(() -> new Article("Java"))
.limit(35000)
.collect(Collectors.toList());
}
}
}

View File

@ -1,8 +1,9 @@
package com.baeldung.spliteratorAPI;
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 final static String SUFFIX = "- published by Baeldung";
@ -11,7 +12,7 @@ public class Task implements Runnable {
}
@Override
public void run() {
public String call() {
int current = 0;
while (spliterator.tryAdvance(article -> {
article.setName(article.getName()
@ -20,7 +21,7 @@ public class Task implements Runnable {
current++;
}
;
System.out.println(Thread.currentThread()
.getName() + ":" + current);
return Thread.currentThread()
.getName() + ":" + current;
}
}

View File

@ -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 + "");
}
}