BAEL-1473 replaced int with AtomicInteger for safe usage in multi-threaded env (#3497)
* BAEL-1473 Intoduction to Spliterator in Java * BAEL-1473 - Replace .out with logger.info * removed log * BAEL-1473 - added test-cases * modify test-cases * AtomicInteger instead of int * SIZED removed
This commit is contained in:
parent
689083a090
commit
0585764866
|
@ -2,11 +2,12 @@ package com.baeldung.spliteratorAPI;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Spliterator;
|
import java.util.Spliterator;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class RelatedAuthorSpliterator implements Spliterator<Author> {
|
public class RelatedAuthorSpliterator implements Spliterator<Author> {
|
||||||
private final List<Author> list;
|
private final List<Author> list;
|
||||||
private int current = 0;
|
AtomicInteger current = new AtomicInteger();
|
||||||
|
|
||||||
public RelatedAuthorSpliterator(List<Author> list) {
|
public RelatedAuthorSpliterator(List<Author> list) {
|
||||||
this.list = list;
|
this.list = list;
|
||||||
|
@ -14,21 +15,21 @@ public class RelatedAuthorSpliterator implements Spliterator<Author> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean tryAdvance(Consumer<? super Author> action) {
|
public boolean tryAdvance(Consumer<? super Author> action) {
|
||||||
action.accept(list.get(current++));
|
|
||||||
return current < list.size();
|
action.accept(list.get(current.getAndIncrement()));
|
||||||
|
return current.get() < list.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Spliterator<Author> trySplit() {
|
public Spliterator<Author> trySplit() {
|
||||||
int currentSize = list.size() - current;
|
int currentSize = list.size() - current.get();
|
||||||
if (currentSize < 10) {
|
if (currentSize < 10) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
for (int splitPos = currentSize / 2 + current; splitPos < list.size(); splitPos++) {
|
for (int splitPos = currentSize / 2 + current.intValue(); splitPos < list.size(); splitPos++) {
|
||||||
if (list.get(splitPos)
|
if (list.get(splitPos).getRelatedArticleId() == 0) {
|
||||||
.getRelatedArticleId() == 0) {
|
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(list.subList(current.get(), splitPos));
|
||||||
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(list.subList(current, splitPos));
|
current.set(splitPos);
|
||||||
current = splitPos;
|
|
||||||
return spliterator;
|
return spliterator;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,11 +38,12 @@ public class RelatedAuthorSpliterator implements Spliterator<Author> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long estimateSize() {
|
public long estimateSize() {
|
||||||
return list.size() - current;
|
return list.size() - current.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int characteristics() {
|
public int characteristics() {
|
||||||
return SIZED + CONCURRENT;
|
return CONCURRENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue