Skip to content

Commit 32d53d9

Browse files
author
Achim Brandt
committed
more updates for new cursor API
1 parent b117d56 commit 32d53d9

30 files changed

+891
-975
lines changed

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 107 additions & 158 deletions
Large diffs are not rendered by default.

src/main/java/com/arangodb/BaseCursorProxy.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
*/
1111
public class BaseCursorProxy<T, S extends DocumentEntity<T>> implements Iterable<S> {
1212

13-
private BaseCursor<T, S> baseCursor;
13+
private DocumentCursorResult<T, S> baseCursor;
1414

15-
public BaseCursorProxy(BaseCursor<T, S> baseCursor) {
15+
public BaseCursorProxy(DocumentCursorResult<T, S> baseCursor) {
1616
this.baseCursor = baseCursor;
1717
}
1818

@@ -97,35 +97,10 @@ public boolean isNotModified() {
9797
return baseCursor.getEntity().isNotModified();
9898
}
9999

100-
/**
101-
* If the request is unauthorized this returns true
102-
*
103-
* @return boolean
104-
*/
105-
public boolean isUnauthorized() {
106-
return baseCursor.getEntity().isUnauthorized();
107-
}
108-
109-
public boolean isError() {
110-
return baseCursor.getEntity().isError();
111-
}
112-
113100
public int getCode() {
114101
return baseCursor.getEntity().getCode();
115102
}
116103

117-
public int getErrorNumber() {
118-
return baseCursor.getEntity().getErrorNumber();
119-
}
120-
121-
public String getErrorMessage() {
122-
return baseCursor.getEntity().getErrorMessage();
123-
}
124-
125-
public long getEtag() {
126-
return baseCursor.getEntity().getEtag();
127-
}
128-
129104
public int getStatusCode() {
130105
return baseCursor.getEntity().getStatusCode();
131106
}

src/main/java/com/arangodb/BaseCursorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public class BaseCursorTest<T> extends BaseCursorProxy<T, DocumentEntity<T>> {
99

10-
public BaseCursorTest(BaseCursor<T, DocumentEntity<T>> baseCursor) {
10+
public BaseCursorTest(DocumentCursorResult<T, DocumentEntity<T>> baseCursor) {
1111
super(baseCursor);
1212
}
1313
}

src/main/java/com/arangodb/BaseCursor.java renamed to src/main/java/com/arangodb/CursorResult.java

Lines changed: 28 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -21,79 +21,52 @@
2121
import java.util.List;
2222
import java.util.NoSuchElementException;
2323

24-
import com.arangodb.entity.BaseCursorEntity;
25-
import com.arangodb.entity.DocumentEntity;
24+
import com.arangodb.entity.CursorEntity;
2625

2726
/**
2827
* @author tamtam180 - kirscheless at gmail.com
29-
* @author a-brandt
3028
*
3129
*/
32-
public class BaseCursor<T, S extends DocumentEntity<T>> implements Iterable<S> {
30+
public class CursorResult<T> implements Iterable<T> {
3331

3432
private String database;
35-
private transient InternalCursorDocumentDriver cursorDriver;
36-
private transient Class<S> classDocumentEntity;
37-
private transient Class<T> clazz;
38-
private transient BaseCursorEntity<T, S> entity;
33+
private transient InternalCursorDriver cursorDriver;
34+
private transient Class<?>[] clazz;
35+
private transient CursorEntity<T> entity;
3936
private transient int pos;
4037
private int count;
38+
private CursorIterator iter;
4139

42-
public BaseCursor(String database, InternalCursorDocumentDriver cursorDriver, BaseCursorEntity<T, S> entity,
43-
Class<S> classDocumentEntity, Class<T> clazz) {
40+
public CursorResult(String database, InternalCursorDriver cursorDriver, CursorEntity<T> entity, Class<?>... clazz) {
4441
this.database = database;
4542
this.cursorDriver = cursorDriver;
46-
this.classDocumentEntity = classDocumentEntity;
4743
this.clazz = clazz;
4844
this.entity = entity;
4945
this.count = entity == null ? 0 : entity.getCount();
5046
this.pos = 0;
47+
this.iter = null;
5148
}
5249

5350
@Override
54-
public Iterator<S> iterator() {
55-
return new DocumentEntityIterator();
56-
}
57-
58-
/**
59-
* Returns an iterator over a entity set of elements of type T.
60-
*
61-
* @return an Iterator.
62-
*/
63-
public Iterator<T> entityIterator() {
64-
return new EntityIterator();
65-
}
66-
67-
/**
68-
* Returns the DocumentEntity objects as a list
69-
*
70-
* @return list of DocumentEntity objects
71-
*/
72-
public List<S> asList() {
73-
List<S> result = new ArrayList<S>();
74-
Iterator<S> iterator = iterator();
75-
76-
while (iterator.hasNext()) {
77-
result.add(iterator.next());
51+
public Iterator<T> iterator() {
52+
if (iter == null) {
53+
iter = new CursorIterator();
7854
}
7955

80-
return result;
56+
return iter;
8157
}
8258

8359
/**
84-
* Returns the entities of DocumentEntity objects as a list
60+
* Returns the objects as a list
8561
*
8662
* @return list of DocumentEntity objects
8763
*/
88-
public List<T> asEntityList() {
64+
public List<T> asList() {
8965
List<T> result = new ArrayList<T>();
90-
Iterator<S> iterator = iterator();
66+
Iterator<T> iterator = iterator();
9167

9268
while (iterator.hasNext()) {
93-
T e = iterator.next().getEntity();
94-
if (e != null) {
95-
result.add(e);
96-
}
69+
result.add(iterator.next());
9770
}
9871

9972
return result;
@@ -119,7 +92,7 @@ public int getCount() {
11992
}
12093

12194
/**
122-
* et total number of results for queries with LIMIT clause
95+
* Get total number of results for queries with LIMIT clause
12396
*
12497
* @return total number of results
12598
*/
@@ -136,7 +109,7 @@ public int getFullCount() {
136109
*
137110
* @return the single result or null
138111
*/
139-
public S getUniqueResult() {
112+
public T getUniqueResult() {
140113
return entity.getUniqueResult();
141114
}
142115

@@ -147,55 +120,23 @@ public S getUniqueResult() {
147120
*/
148121
private void updateEntity() throws ArangoException {
149122
long cursorId = entity.getCursorId();
150-
this.entity = cursorDriver.continueBaseCursorEntityQuery(database, cursorId, classDocumentEntity, clazz);
123+
this.entity = cursorDriver.continueQuery(database, cursorId, this.clazz);
151124
this.pos = 0;
152125
}
153126

154127
/**
155-
* internal DocumentEntity iterator
128+
* Returns the CursorEntity object
129+
*
130+
* @return CursorEntity
156131
*/
157-
public class DocumentEntityIterator implements Iterator<S> {
158-
159-
@Override
160-
public boolean hasNext() {
161-
if (entity == null) {
162-
return false;
163-
}
164-
if (pos < entity.size()) {
165-
return true;
166-
}
167-
if (entity.hasMore()) {
168-
return true;
169-
}
170-
return false;
171-
}
172-
173-
@Override
174-
public S next() {
175-
if (hasNext()) {
176-
if (pos >= entity.size()) {
177-
try {
178-
updateEntity();
179-
} catch (ArangoException e) {
180-
throw new IllegalStateException(e);
181-
}
182-
}
183-
return entity.get(pos++);
184-
}
185-
throw new NoSuchElementException();
186-
}
187-
188-
@Override
189-
public void remove() {
190-
throw new UnsupportedOperationException("remove is not supported");
191-
}
192-
132+
public CursorEntity<T> getEntity() {
133+
return entity;
193134
}
194135

195136
/**
196-
* internal DocumentEntity iterator
137+
* internal iterator
197138
*/
198-
public class EntityIterator implements Iterator<T> {
139+
public class CursorIterator implements Iterator<T> {
199140

200141
@Override
201142
public boolean hasNext() {
@@ -221,25 +162,16 @@ public T next() {
221162
throw new IllegalStateException(e);
222163
}
223164
}
224-
return entity.get(pos++).getEntity();
165+
return entity.get(pos++);
225166
}
226167
throw new NoSuchElementException();
227168
}
228169

229170
@Override
230171
public void remove() {
231-
throw new UnsupportedOperationException("remove is not supported");
172+
throw new UnsupportedOperationException("remove is not supported!");
232173
}
233174

234175
}
235176

236-
/**
237-
* Returns the BaseCursorEntity object
238-
*
239-
* @return BaseCursorEntity
240-
*/
241-
public BaseCursorEntity<T, S> getEntity() {
242-
return entity;
243-
}
244-
245177
}

src/main/java/com/arangodb/DocumentCursor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public class DocumentCursor<T> extends BaseCursorProxy<T, DocumentEntity<T>> {
99

10-
public DocumentCursor(BaseCursor<T, DocumentEntity<T>> baseCursor) {
10+
public DocumentCursor(DocumentCursorResult<T, DocumentEntity<T>> baseCursor) {
1111
super(baseCursor);
1212
}
1313
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (C) 2012 tamtam180
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.arangodb;
18+
19+
import java.util.ArrayList;
20+
import java.util.Iterator;
21+
import java.util.List;
22+
23+
import com.arangodb.entity.CursorEntity;
24+
import com.arangodb.entity.DocumentEntity;
25+
26+
/**
27+
* @author tamtam180 - kirscheless at gmail.com
28+
* @author a-brandt
29+
*
30+
*/
31+
public class DocumentCursorResult<T, S extends DocumentEntity<T>> extends CursorResult<S> {
32+
33+
public DocumentCursorResult(String database, InternalCursorDriver cursorDriver, CursorEntity<S> entity, Class<?>... clazz) {
34+
super(database, cursorDriver, entity, clazz);
35+
}
36+
37+
/**
38+
* Returns an iterator over a entity set of elements of type T.
39+
*
40+
* @return an Iterator.
41+
*/
42+
public Iterator<T> entityIterator() {
43+
return new EntityIterator();
44+
}
45+
46+
/**
47+
* Returns the entities of DocumentEntity objects as a list
48+
*
49+
* @return list of DocumentEntity objects
50+
*/
51+
public List<T> asEntityList() {
52+
List<T> result = new ArrayList<T>();
53+
Iterator<S> iterator = iterator();
54+
55+
while (iterator.hasNext()) {
56+
T e = iterator.next().getEntity();
57+
if (e != null) {
58+
result.add(e);
59+
}
60+
}
61+
62+
return result;
63+
}
64+
65+
/**
66+
* internal entity iterator
67+
*/
68+
public class EntityIterator implements Iterator<T> {
69+
70+
private Iterator<S> iterator;
71+
72+
public EntityIterator() {
73+
iterator = iterator();
74+
}
75+
76+
@Override
77+
public boolean hasNext() {
78+
return iterator.hasNext();
79+
}
80+
81+
@Override
82+
public T next() {
83+
return iterator.next().getEntity();
84+
}
85+
86+
@Override
87+
public void remove() {
88+
iterator.remove();
89+
}
90+
91+
}
92+
}

src/main/java/com/arangodb/EdgeCursor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public class EdgeCursor<T> extends BaseCursorProxy<T, EdgeEntity<T>> {
99

10-
public EdgeCursor(BaseCursor<T, EdgeEntity<T>> baseCursor) {
10+
public EdgeCursor(DocumentCursorResult<T, EdgeEntity<T>> baseCursor) {
1111
super(baseCursor);
1212
}
1313
}

0 commit comments

Comments
 (0)