1
1
package io .objectbox .query ;
2
2
3
+ import java .util .Collections ;
4
+ import java .util .Comparator ;
3
5
import java .util .Date ;
4
6
import java .util .Iterator ;
5
7
import java .util .List ;
@@ -75,16 +77,19 @@ native static void nativeSetParameters(long handle, int propertyId, String param
75
77
private final QueryPublisher <T > publisher ;
76
78
private final List <EagerRelation > eagerRelations ;
77
79
private final QueryFilter <T > filter ;
80
+ private final Comparator <T > comparator ;
78
81
long handle ;
79
82
80
- Query (Box <T > box , long queryHandle , boolean hasOrder , List <EagerRelation > eagerRelations , QueryFilter <T > filter ) {
83
+ Query (Box <T > box , long queryHandle , boolean hasOrder , List <EagerRelation > eagerRelations , QueryFilter <T > filter ,
84
+ Comparator <T > comparator ) {
81
85
this .box = box ;
82
86
store = box .getStore ();
83
87
handle = queryHandle ;
84
88
this .hasOrder = hasOrder ;
85
89
publisher = new QueryPublisher <>(this , box );
86
90
this .eagerRelations = eagerRelations ;
87
91
this .filter = filter ;
92
+ this .comparator = comparator ;
88
93
}
89
94
90
95
@ Override
@@ -108,7 +113,7 @@ public synchronized void close() {
108
113
*/
109
114
@ Nullable
110
115
public T findFirst () {
111
- ensureNoFilter ();
116
+ ensureNoFilterNoComparator ();
112
117
return store .callInReadTx (new Callable <T >() {
113
118
@ Override
114
119
public T call () {
@@ -120,11 +125,19 @@ public T call() {
120
125
});
121
126
}
122
127
123
- private void ensureNoFilter () {
128
+ private void ensureNoFilterNoComparator () {
124
129
if (filter != null ) {
125
130
throw new UnsupportedOperationException ("Does not yet work with a filter yet. " +
126
131
"At this point, only find() and forEach() are supported with filters." );
127
132
}
133
+ ensureNoComparator ();
134
+ }
135
+
136
+ private void ensureNoComparator () {
137
+ if (comparator != null ) {
138
+ throw new UnsupportedOperationException ("Does not yet work with a sorting comparator yet. " +
139
+ "At this point, only find() is supported with sorting comparators." );
140
+ }
128
141
}
129
142
130
143
/**
@@ -134,7 +147,7 @@ private void ensureNoFilter() {
134
147
*/
135
148
@ Nullable
136
149
public T findUnique () {
137
- ensureNoFilter ();
150
+ ensureNoFilterNoComparator ();
138
151
return store .callInReadTx (new Callable <T >() {
139
152
@ Override
140
153
public T call () {
@@ -166,6 +179,9 @@ public List<T> call() throws Exception {
166
179
}
167
180
}
168
181
resolveEagerRelations (entities );
182
+ if (comparator != null ) {
183
+ Collections .sort (entities , comparator );
184
+ }
169
185
return entities ;
170
186
}
171
187
});
@@ -176,7 +192,7 @@ public List<T> call() throws Exception {
176
192
*/
177
193
@ Nonnull
178
194
public List <T > find (final long offset , final long limit ) {
179
- ensureNoFilter ();
195
+ ensureNoFilterNoComparator ();
180
196
return store .callInReadTx (new Callable <List <T >>() {
181
197
@ Override
182
198
public List <T > call () {
@@ -191,7 +207,7 @@ public List<T> call() {
191
207
/**
192
208
* Very efficient way to get just the IDs without creating any objects. IDs can later be used to lookup objects
193
209
* (lookups by ID are also very efficient in ObjectBox).
194
- *
210
+ * <p>
195
211
* Note: a filter set with {@link QueryBuilder#filter} will be silently ignored!
196
212
*/
197
213
@ Nonnull
@@ -211,7 +227,7 @@ public long[] call(long cursorHandle) {
211
227
* Find all Objects matching the query without actually loading the Objects. See @{@link LazyList} for details.
212
228
*/
213
229
public LazyList <T > findLazy () {
214
- ensureNoFilter ();
230
+ ensureNoFilterNoComparator ();
215
231
return new LazyList <>(box , findIds (), false );
216
232
}
217
233
@@ -225,6 +241,7 @@ public LazyList<T> findLazy() {
225
241
* Note: because the consumer is called within a read transaction it may not write to the database.
226
242
*/
227
243
public void forEach (final QueryConsumer <T > consumer ) {
244
+ ensureNoComparator ();
228
245
box .getStore ().runInReadTx (new Runnable () {
229
246
@ Override
230
247
public void run () {
@@ -258,7 +275,7 @@ public void run() {
258
275
*/
259
276
@ Nonnull
260
277
public LazyList <T > findLazyCached () {
261
- ensureNoFilter ();
278
+ ensureNoFilterNoComparator ();
262
279
return new LazyList <>(box , findIds (), true );
263
280
}
264
281
0 commit comments