Skip to content

Commit 0585764

Browse files
Ahmad Alsaniemaibin
Ahmad Alsanie
authored andcommitted
BAEL-1473 replaced int with AtomicInteger for safe usage in multi-threaded env (eugenp#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
1 parent 689083a commit 0585764

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

core-java-8/src/main/java/com/baeldung/spliteratorAPI/RelatedAuthorSpliterator.java

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,48 @@
22

33
import java.util.List;
44
import java.util.Spliterator;
5+
import java.util.concurrent.atomic.AtomicInteger;
56
import java.util.function.Consumer;
67

78
public class RelatedAuthorSpliterator implements Spliterator<Author> {
8-
private final List<Author> list;
9-
private int current = 0;
10-
11-
public RelatedAuthorSpliterator(List<Author> list) {
12-
this.list = list;
13-
}
14-
15-
@Override
16-
public boolean tryAdvance(Consumer<? super Author> action) {
17-
action.accept(list.get(current++));
18-
return current < list.size();
19-
}
20-
21-
@Override
22-
public Spliterator<Author> trySplit() {
23-
int currentSize = list.size() - current;
24-
if (currentSize < 10) {
25-
return null;
26-
}
27-
for (int splitPos = currentSize / 2 + current; splitPos < list.size(); splitPos++) {
28-
if (list.get(splitPos)
29-
.getRelatedArticleId() == 0) {
30-
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(list.subList(current, splitPos));
31-
current = splitPos;
32-
return spliterator;
33-
}
34-
}
35-
return null;
36-
}
37-
38-
@Override
39-
public long estimateSize() {
40-
return list.size() - current;
41-
}
42-
43-
@Override
44-
public int characteristics() {
45-
return SIZED + CONCURRENT;
46-
}
9+
private final List<Author> list;
10+
AtomicInteger current = new AtomicInteger();
11+
12+
public RelatedAuthorSpliterator(List<Author> list) {
13+
this.list = list;
14+
}
15+
16+
@Override
17+
public boolean tryAdvance(Consumer<? super Author> action) {
18+
19+
action.accept(list.get(current.getAndIncrement()));
20+
return current.get() < list.size();
21+
}
22+
23+
@Override
24+
public Spliterator<Author> trySplit() {
25+
int currentSize = list.size() - current.get();
26+
if (currentSize < 10) {
27+
return null;
28+
}
29+
for (int splitPos = currentSize / 2 + current.intValue(); splitPos < list.size(); splitPos++) {
30+
if (list.get(splitPos).getRelatedArticleId() == 0) {
31+
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(list.subList(current.get(), splitPos));
32+
current.set(splitPos);
33+
return spliterator;
34+
}
35+
}
36+
return null;
37+
}
38+
39+
@Override
40+
public long estimateSize() {
41+
return list.size() - current.get();
42+
}
43+
44+
@Override
45+
public int characteristics() {
46+
return CONCURRENT;
47+
}
48+
4749
}

0 commit comments

Comments
 (0)