JAVA-8924 Cleanup for spliterator article (#14221)
This commit is contained in:
parent
ab884e145f
commit
b3a5af07c0
@ -1,7 +1,5 @@
|
|||||||
package com.baeldung.spliterator;
|
package com.baeldung.spliterator;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class Executor {
|
public class Executor {
|
||||||
@ -11,9 +9,4 @@ public class Executor {
|
|||||||
RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
|
RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
|
||||||
return wordCounter.getCounter();
|
return wordCounter.getCounter();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Article> generateElements() {
|
|
||||||
return Stream.generate(() -> new Article("Java")).limit(35000).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -1,27 +0,0 @@
|
|||||||
package com.baeldung.spliterator;
|
|
||||||
|
|
||||||
import java.util.Spliterator;
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
|
|
||||||
public class Task implements Callable<String> {
|
|
||||||
private Spliterator<Article> spliterator;
|
|
||||||
private final static String SUFFIX = "- published by Baeldung";
|
|
||||||
|
|
||||||
public Task(Spliterator<Article> spliterator) {
|
|
||||||
this.spliterator = spliterator;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String call() {
|
|
||||||
int current = 0;
|
|
||||||
while (spliterator.tryAdvance(article -> {
|
|
||||||
article.setName(article.getName()
|
|
||||||
.concat(SUFFIX));
|
|
||||||
})) {
|
|
||||||
current++;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
return Thread.currentThread()
|
|
||||||
.getName() + ":" + current;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,7 @@
|
|||||||
package com.baeldung.spliterator;
|
package com.baeldung.spliterator;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -8,33 +10,23 @@ import java.util.stream.Collectors;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import java.util.stream.StreamSupport;
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.spliterator.Article;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import com.baeldung.spliterator.Author;
|
|
||||||
import com.baeldung.spliterator.Executor;
|
|
||||||
import com.baeldung.spliterator.RelatedAuthorSpliterator;
|
|
||||||
import com.baeldung.spliterator.Task;
|
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class ExecutorUnitTest {
|
public class ExecutorUnitTest {
|
||||||
Article article;
|
Article article;
|
||||||
Stream<Author> stream;
|
Stream<Author> stream;
|
||||||
Spliterator<Author> spliterator;
|
Spliterator<Author> spliterator;
|
||||||
Spliterator<Article> split1;
|
|
||||||
Spliterator<Article> split2;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
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),
|
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("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);
|
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());
|
spliterator = new RelatedAuthorSpliterator(article.getListOfAuthors());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,36 +37,39 @@ public class ExecutorUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSpliterator_whenAppliedToAListOfArticle_thenSplittedInHalf() {
|
public void givenAStreamOfArticles_whenProcessedSequentiallyWithSpliterator_ProducessRightOutput() {
|
||||||
assertThat(new Task(split1).call()).containsSequence(Executor.generateElements()
|
|
||||||
.size() / 2 + "");
|
|
||||||
assertThat(new Task(split2).call()).containsSequence(Executor.generateElements()
|
|
||||||
.size() / 2 + "");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenAstreamOfArticles_whenProcessedInSequentiallyWithSpliterator_ProducessRightOutput() {
|
|
||||||
List<Article> articles = Stream.generate(() -> new Article("Java"))
|
List<Article> articles = Stream.generate(() -> new Article("Java"))
|
||||||
.limit(35000)
|
.limit(35000)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
Spliterator<Article> spliterator = articles.spliterator();
|
Spliterator<Article> spliterator = articles.spliterator();
|
||||||
while (spliterator.tryAdvance(article -> article.setName(article.getName()
|
while (spliterator.tryAdvance(article -> article.setName(article.getName()
|
||||||
.concat("- published by Baeldung"))));
|
.concat("- published by Baeldung"))))
|
||||||
|
;
|
||||||
|
|
||||||
articles.forEach(article -> assertThat(article.getName()).isEqualTo("Java- published by Baeldung"));
|
articles.forEach(article -> assertThat(article.getName()).isEqualTo("Java- published by Baeldung"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSpliterator_whenAppliedToListOfArticles_thenSplitIntoEqualHalf() {
|
public void givenAStreamOfArticle_whenProcessedUsingTrySplit_thenSplitIntoEqualHalf() {
|
||||||
|
List<Article> articles = Stream.generate(() -> new Article("Java"))
|
||||||
|
.limit(35000)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
Spliterator<Article> split1 = articles.spliterator();
|
||||||
|
Spliterator<Article> split2 = split1.trySplit();
|
||||||
|
|
||||||
|
log.info("Size: " + split1.estimateSize());
|
||||||
|
log.info("Characteristics: " + split1.characteristics());
|
||||||
|
|
||||||
List<Article> articlesListOne = new ArrayList<>();
|
List<Article> articlesListOne = new ArrayList<>();
|
||||||
List<Article> articlesListTwo = new ArrayList<>();
|
List<Article> articlesListTwo = new ArrayList<>();
|
||||||
|
|
||||||
split1.forEachRemaining(articlesListOne::add);
|
split1.forEachRemaining(articlesListOne::add);
|
||||||
split2.forEachRemaining(articlesListTwo::add);
|
split2.forEachRemaining(articlesListTwo::add);
|
||||||
|
|
||||||
System.out.println(articlesListOne.size());
|
assertThat(articlesListOne.size()).isEqualTo(17500);
|
||||||
System.out.println(articlesListTwo.size());
|
assertThat(articlesListTwo.size()).isEqualTo(17500);
|
||||||
|
|
||||||
assertThat(articlesListOne).doesNotContainAnyElementsOf(articlesListTwo);
|
assertThat(articlesListOne).doesNotContainAnyElementsOf(articlesListTwo);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user