|
27 | 27 | */ |
28 | 28 | public class CursorResultSet<T> implements Iterable<T> { |
29 | 29 |
|
30 | | - private String database; |
31 | | - private transient InternalCursorDriver cursorDriver; |
32 | | - private transient Class<?>[] clazz; |
33 | | - private transient CursorEntity<T> entity; |
34 | | - private transient int pos; |
35 | | - private int totalCount; |
36 | | - private transient Iterator<T> itr; |
37 | | - |
38 | | - public CursorResultSet(String database, InternalCursorDriver cursorDriver, CursorEntity<T> entity, Class<?> ...clazz) { |
39 | | - this.database = database; |
40 | | - this.cursorDriver = cursorDriver; |
41 | | - this.clazz = clazz; |
42 | | - this.entity = entity; |
43 | | - this.totalCount = entity == null ? 0 : entity.getCount(); |
44 | | - this.pos = 0; |
45 | | - this.itr = new CursorIterator(); |
46 | | - } |
47 | | - |
48 | | - public Iterator<T> iterator() { |
49 | | - return new CursorIterator(); |
50 | | - } |
51 | | - |
52 | | - public boolean hasNext() { |
53 | | - return itr.hasNext(); |
54 | | - } |
55 | | - |
56 | | - public T next() { |
57 | | - return itr.next(); |
58 | | - } |
59 | | - |
60 | | - public void close() throws ArangoException { |
61 | | - long cursorId = entity.getCursorId(); |
62 | | - cursorDriver.finishQuery(database, cursorId); |
63 | | - } |
64 | | - |
65 | | - public int getTotalCount() { |
66 | | - return totalCount; |
67 | | - } |
68 | | - |
69 | | - private void updateEntity() throws ArangoException { |
70 | | - long cursorId = entity.getCursorId(); |
71 | | - this.entity = cursorDriver.continueQuery(database, cursorId, this.clazz); |
72 | | - this.pos = 0; |
73 | | - } |
74 | | - |
75 | | - public class CursorIterator implements Iterator<T> { |
76 | | - |
77 | | - public boolean hasNext() { |
78 | | - if (entity == null) { |
79 | | - return false; |
80 | | - } |
81 | | - if (pos < entity.size()) { |
82 | | - return true; |
83 | | - } |
84 | | - if (entity.hasMore()) { |
85 | | - return true; |
86 | | - } |
87 | | - return false; |
88 | | - } |
89 | | - |
90 | | - public T next() { |
91 | | - if (hasNext()) { |
92 | | - if (pos >= entity.size()) { |
93 | | - try { |
94 | | - updateEntity(); |
95 | | - } catch (ArangoException e) { |
96 | | - throw new IllegalStateException(e); |
97 | | - } |
98 | | - } |
99 | | - return entity.get(pos++); |
100 | | - } |
101 | | - throw new NoSuchElementException(); |
102 | | - } |
103 | | - |
104 | | - public void remove() { |
105 | | - throw new UnsupportedOperationException("remove does not support!!"); |
106 | | - } |
107 | | - |
108 | | - } |
109 | | - |
| 30 | + private String database; |
| 31 | + private InternalCursorDriver cursorDriver; |
| 32 | + private Class<?>[] clazz; |
| 33 | + private CursorEntity<T> entity; |
| 34 | + private int pos; |
| 35 | + private int totalCount; |
| 36 | + private Iterator<T> itr; |
| 37 | + |
| 38 | + public CursorResultSet(String database, InternalCursorDriver cursorDriver, CursorEntity<T> entity, |
| 39 | + Class<?>... clazz) { |
| 40 | + this.database = database; |
| 41 | + this.cursorDriver = cursorDriver; |
| 42 | + this.clazz = clazz; |
| 43 | + this.entity = entity; |
| 44 | + this.totalCount = entity == null ? 0 : entity.getCount(); |
| 45 | + this.pos = 0; |
| 46 | + this.itr = new CursorIterator(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Iterator<T> iterator() { |
| 51 | + return new CursorIterator(); |
| 52 | + } |
| 53 | + |
| 54 | + public boolean hasNext() { |
| 55 | + return itr.hasNext(); |
| 56 | + } |
| 57 | + |
| 58 | + public T next() { |
| 59 | + return itr.next(); |
| 60 | + } |
| 61 | + |
| 62 | + public void close() throws ArangoException { |
| 63 | + long cursorId = entity.getCursorId(); |
| 64 | + cursorDriver.finishQuery(database, cursorId); |
| 65 | + } |
| 66 | + |
| 67 | + public int getTotalCount() { |
| 68 | + return totalCount; |
| 69 | + } |
| 70 | + |
| 71 | + public void updateEntity() throws ArangoException { |
| 72 | + long cursorId = entity.getCursorId(); |
| 73 | + this.entity = cursorDriver.continueQuery(database, cursorId, this.clazz); |
| 74 | + this.pos = 0; |
| 75 | + } |
| 76 | + |
| 77 | + public class CursorIterator implements Iterator<T> { |
| 78 | + |
| 79 | + @Override |
| 80 | + public boolean hasNext() { |
| 81 | + if (entity == null) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + if (pos < entity.size()) { |
| 85 | + return true; |
| 86 | + } |
| 87 | + if (entity.hasMore()) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public T next() { |
| 95 | + if (hasNext()) { |
| 96 | + if (pos >= entity.size()) { |
| 97 | + try { |
| 98 | + updateEntity(); |
| 99 | + } catch (ArangoException e) { |
| 100 | + throw new IllegalStateException(e); |
| 101 | + } |
| 102 | + } |
| 103 | + return entity.get(pos++); |
| 104 | + } |
| 105 | + throw new NoSuchElementException(); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void remove() { |
| 110 | + throw new UnsupportedOperationException("remove does not support!!"); |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
110 | 115 | } |
0 commit comments