1
|
package eu.dnetlib.enabling.resultset.client;
|
2
|
|
3
|
import java.util.Iterator;
|
4
|
import java.util.LinkedList;
|
5
|
import java.util.List;
|
6
|
import java.util.NoSuchElementException;
|
7
|
import java.util.Queue;
|
8
|
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
|
12
|
import eu.dnetlib.enabling.resultset.client.utils.ResultSetRuntimeException;
|
13
|
import eu.dnetlib.enabling.resultset.client.utils.ResultSetTimeoutException;
|
14
|
import eu.dnetlib.enabling.resultset.rmi.ResultSetException;
|
15
|
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
|
16
|
|
17
|
/**
|
18
|
*
|
19
|
* @author claudio
|
20
|
*
|
21
|
*/
|
22
|
public class ResultSetClientIterator implements Iterator<String> {
|
23
|
|
24
|
/**
|
25
|
* logger
|
26
|
*/
|
27
|
private static final Log log = LogFactory.getLog(ResultSetClientIterator.class);
|
28
|
|
29
|
/**
|
30
|
* the page source
|
31
|
*/
|
32
|
private ResultSetPageProvider pageProvider;
|
33
|
|
34
|
/**
|
35
|
* buffer used to provide a single element for the next() method
|
36
|
*/
|
37
|
private Queue<String> buffer;
|
38
|
|
39
|
|
40
|
public ResultSetClientIterator(ResultSetService resultSet, String rsId)
|
41
|
throws ResultSetRuntimeException {
|
42
|
|
43
|
pageProvider = new ResultSetPageProvider(resultSet, rsId);
|
44
|
buffer = new LinkedList<String>();
|
45
|
}
|
46
|
|
47
|
public ResultSetClientIterator(ResultSetService resultSet, String rsId, int pageSize) {
|
48
|
this(resultSet, rsId);
|
49
|
pageProvider.setPageSize(pageSize);
|
50
|
}
|
51
|
|
52
|
public ResultSetClientIterator(ResultSetService resultSet, String rsId, int pageSize, long timeout) {
|
53
|
this(resultSet, rsId, pageSize);
|
54
|
pageProvider.setMaxWaitTime(timeout);
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Tries to refill the buffer with a nextPage()
|
59
|
* @return true if the buffer was filled successfully, false otherwise
|
60
|
* @throws ResultSetTimeoutException in case of timeout
|
61
|
* @throws ResultSetException
|
62
|
* @throws ResultSetException
|
63
|
*/
|
64
|
private boolean refillBuffer() throws ResultSetTimeoutException, ResultSetRuntimeException {
|
65
|
List<String> page = pageProvider.nextPage();
|
66
|
if (page != null && !page.isEmpty()) {
|
67
|
buffer.addAll(page);
|
68
|
return true;
|
69
|
}
|
70
|
return false;
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
public boolean hasNext() {
|
75
|
if (!buffer.isEmpty())
|
76
|
return true;
|
77
|
return refillBuffer();
|
78
|
}
|
79
|
|
80
|
@Override
|
81
|
public String next() {
|
82
|
if (!hasNext()) {
|
83
|
log.info("NoSuchElementException");
|
84
|
throw new NoSuchElementException();
|
85
|
}
|
86
|
return buffer.poll();
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
public void remove() {
|
91
|
throw new UnsupportedOperationException();
|
92
|
}
|
93
|
|
94
|
}
|