Skip to content

Commit a308640

Browse files
author
Burak Serdar
committed
Add lazy initialization to liststream to prevent concurrent update errors
1 parent 8c30117 commit a308640

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

crud/src/main/java/com/redhat/lightblue/crud/ListDocumentStream.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@
2727
public class ListDocumentStream<T> implements DocumentStream<T> {
2828

2929
private final List<T> documents;
30-
private final Iterator<T> itr;
30+
private Iterator<T> itr;
3131

3232
public ListDocumentStream(List<T> list) {
3333
this.documents=list;
34-
this.itr=documents.iterator();
3534
}
3635

3736
/**
@@ -43,11 +42,19 @@ public List<T> getDocuments() {
4342

4443
@Override
4544
public boolean hasNext() {
45+
// Lazy initialization, don't get the iterator until the last
46+
// moment. This is to protect against concurrent modification
47+
// exceptions if the list is modified between the creation of
48+
// the iterator and the actual iteration
49+
if(itr==null)
50+
itr=documents.iterator();
4651
return itr.hasNext();
4752
}
4853

4954
@Override
5055
public T next() {
56+
if(itr==null)
57+
itr=documents.iterator();
5158
return itr.next();
5259
}
5360

0 commit comments

Comments
 (0)