Skip to content

Commit c27c2d5

Browse files
committed
moved aggregate functions from Query to PropertyQuery
1 parent 8c3c50e commit c27c2d5

File tree

2 files changed

+108
-61
lines changed

2 files changed

+108
-61
lines changed

objectbox-java/src/main/java/io/objectbox/query/PropertyQuery.java

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
@SuppressWarnings("WeakerAccess") // WeakerAccess: allow inner class access without accessor
3030
public class PropertyQuery {
3131
final Query query;
32+
final long queryHandle;
3233
final Property property;
34+
final int propertyId;
35+
3336
boolean distinct;
3437
boolean noCaseIfDistinct = true;
3538
boolean enableNull;
@@ -42,7 +45,9 @@ public class PropertyQuery {
4245

4346
PropertyQuery(Query query, Property property) {
4447
this.query = query;
48+
queryHandle = query.handle;
4549
this.property = property;
50+
propertyId = property.id;
4651
}
4752

4853
/** Clears all values (e.g. distinct and null value). */
@@ -140,7 +145,7 @@ public String[] findStrings() {
140145
public String[] call() {
141146
boolean distinctNoCase = distinct && noCaseIfDistinct;
142147
long cursorHandle = query.cursorHandle();
143-
return query.nativeFindStrings(query.handle, cursorHandle, property.id, distinct, distinctNoCase,
148+
return query.nativeFindStrings(queryHandle, cursorHandle, propertyId, distinct, distinctNoCase,
144149
enableNull, nullValueString);
145150
}
146151
});
@@ -161,7 +166,7 @@ public long[] findLongs() {
161166
return (long[]) query.callInReadTx(new Callable<long[]>() {
162167
@Override
163168
public long[] call() {
164-
return query.nativeFindLongs(query.handle, query.cursorHandle(), property.id, distinct,
169+
return query.nativeFindLongs(queryHandle, query.cursorHandle(), propertyId, distinct,
165170
enableNull, nullValueLong);
166171
}
167172
});
@@ -180,7 +185,7 @@ public int[] findInts() {
180185
return (int[]) query.callInReadTx(new Callable<int[]>() {
181186
@Override
182187
public int[] call() {
183-
return query.nativeFindInts(query.handle, query.cursorHandle(), property.id, distinct,
188+
return query.nativeFindInts(queryHandle, query.cursorHandle(), propertyId, distinct,
184189
enableNull, (int) nullValueLong);
185190
}
186191
});
@@ -199,7 +204,7 @@ public short[] findShorts() {
199204
return (short[]) query.callInReadTx(new Callable<short[]>() {
200205
@Override
201206
public short[] call() {
202-
return query.nativeFindShorts(query.handle, query.cursorHandle(), property.id, distinct,
207+
return query.nativeFindShorts(queryHandle, query.cursorHandle(), propertyId, distinct,
203208
enableNull, (short) nullValueLong);
204209
}
205210
});
@@ -218,7 +223,7 @@ public char[] findChars() {
218223
return (char[]) query.callInReadTx(new Callable<char[]>() {
219224
@Override
220225
public char[] call() {
221-
return query.nativeFindChars(query.handle, query.cursorHandle(), property.id, distinct,
226+
return query.nativeFindChars(queryHandle, query.cursorHandle(), propertyId, distinct,
222227
enableNull, (char) nullValueLong);
223228
}
224229
});
@@ -235,7 +240,7 @@ public byte[] findBytes() {
235240
return (byte[]) query.callInReadTx(new Callable<byte[]>() {
236241
@Override
237242
public byte[] call() {
238-
return query.nativeFindBytes(query.handle, query.cursorHandle(), property.id, distinct,
243+
return query.nativeFindBytes(queryHandle, query.cursorHandle(), propertyId, distinct,
239244
enableNull, (byte) nullValueLong);
240245
}
241246
});
@@ -254,7 +259,7 @@ public float[] findFloats() {
254259
return (float[]) query.callInReadTx(new Callable<float[]>() {
255260
@Override
256261
public float[] call() {
257-
return query.nativeFindFloats(query.handle, query.cursorHandle(), property.id, distinct,
262+
return query.nativeFindFloats(queryHandle, query.cursorHandle(), propertyId, distinct,
258263
enableNull, nullValueFloat);
259264
}
260265
});
@@ -273,7 +278,7 @@ public double[] findDoubles() {
273278
return (double[]) query.callInReadTx(new Callable<double[]>() {
274279
@Override
275280
public double[] call() {
276-
return query.nativeFindDoubles(query.handle, query.cursorHandle(), property.id, distinct,
281+
return query.nativeFindDoubles(queryHandle, query.cursorHandle(), propertyId, distinct,
277282
enableNull, nullValueDouble);
278283
}
279284
});
@@ -284,7 +289,7 @@ public String findString() {
284289
@Override
285290
public String call() {
286291
boolean distinctCase = distinct && !noCaseIfDistinct;
287-
return query.nativeFindString(query.handle, query.cursorHandle(), property.id, unique, distinct,
292+
return query.nativeFindString(queryHandle, query.cursorHandle(), propertyId, unique, distinct,
288293
distinctCase, enableNull, nullValueString);
289294
}
290295
});
@@ -294,7 +299,7 @@ private Object findNumber() {
294299
return query.callInReadTx(new Callable<Object>() {
295300
@Override
296301
public Object call() {
297-
return query.nativeFindNumber(query.handle, query.cursorHandle(), property.id, unique, distinct,
302+
return query.nativeFindNumber(queryHandle, query.cursorHandle(), propertyId, unique, distinct,
298303
enableNull, nullValueLong, nullValueFloat, nullValueDouble);
299304
}
300305
});
@@ -332,4 +337,75 @@ public Double findDouble() {
332337
return (Double) findNumber();
333338
}
334339

340+
341+
/** Sums up all values for the given property over all Objects matching the query. */
342+
public long sum() {
343+
return (Long) query.callInReadTx(new Callable<Long>() {
344+
@Override
345+
public Long call() {
346+
return query.nativeSum(queryHandle, query.cursorHandle(), propertyId);
347+
}
348+
});
349+
}
350+
351+
/** Sums up all values for the given property over all Objects matching the query. */
352+
public double sumDouble() {
353+
return (Double) query.callInReadTx(new Callable<Double>() {
354+
@Override
355+
public Double call() {
356+
return query.nativeSumDouble(queryHandle, query.cursorHandle(), propertyId);
357+
}
358+
});
359+
}
360+
361+
/** Finds the maximum value for the given property over all Objects matching the query. */
362+
public long max() {
363+
return (Long) query.callInReadTx(new Callable<Long>() {
364+
@Override
365+
public Long call() {
366+
return query.nativeMax(queryHandle, query.cursorHandle(), propertyId);
367+
}
368+
});
369+
}
370+
371+
/** Finds the maximum value for the given property over all Objects matching the query. */
372+
public double maxDouble() {
373+
return (Double) query.callInReadTx(new Callable<Double>() {
374+
@Override
375+
public Double call() {
376+
return query.nativeMaxDouble(queryHandle, query.cursorHandle(), propertyId);
377+
}
378+
});
379+
}
380+
381+
/** Finds the minimum value for the given property over all Objects matching the query. */
382+
public long min() {
383+
return (Long) query.callInReadTx(new Callable<Long>() {
384+
@Override
385+
public Long call() {
386+
return query.nativeMin(queryHandle, query.cursorHandle(), propertyId);
387+
}
388+
});
389+
}
390+
391+
/** Finds the minimum value for the given property over all Objects matching the query. */
392+
public double minDouble() {
393+
return (Double) query.callInReadTx(new Callable<Double>() {
394+
@Override
395+
public Double call() {
396+
return query.nativeMinDouble(queryHandle, query.cursorHandle(), propertyId);
397+
}
398+
});
399+
}
400+
401+
/** Calculates the average of all values for the given property over all Objects matching the query. */
402+
public double avg() {
403+
return (Double) query.callInReadTx(new Callable<Double>() {
404+
@Override
405+
public Double call() {
406+
return query.nativeAvg(queryHandle, query.cursorHandle(), propertyId);
407+
}
408+
});
409+
}
410+
335411
}

objectbox-java/src/main/java/io/objectbox/query/Query.java

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public LazyList<T> findLazy() {
290290

291291
/**
292292
* Creates a {@link PropertyQuery} for the given property.
293-
*
293+
* <p>
294294
* A {@link PropertyQuery} uses the same conditions as this Query object,
295295
* but returns only the value(s) of a single property (not an entity objects).
296296
*
@@ -409,77 +409,48 @@ public Long call(long cursorHandle) {
409409
});
410410
}
411411

412-
/** Sums up all values for the given property over all Objects matching the query. */
412+
/** @deprecated Use {@link #property(Property)} to get a {@link PropertyQuery} for aggregate functions. */
413+
@Deprecated
413414
public long sum(final Property property) {
414-
return box.internalCallWithReaderHandle(new CallWithHandle<Long>() {
415-
@Override
416-
public Long call(long cursorHandle) {
417-
return nativeSum(handle, cursorHandle, property.getId());
418-
}
419-
});
415+
return property(property).sum();
420416
}
421417

422-
/** Sums up all values for the given property over all Objects matching the query. */
418+
/** @deprecated Use {@link #property(Property)} to get a {@link PropertyQuery} for aggregate functions. */
419+
@Deprecated
423420
public double sumDouble(final Property property) {
424-
return box.internalCallWithReaderHandle(new CallWithHandle<Double>() {
425-
@Override
426-
public Double call(long cursorHandle) {
427-
return nativeSumDouble(handle, cursorHandle, property.getId());
428-
}
429-
});
421+
return property(property).sumDouble();
430422
}
431423

432-
/** Finds the maximum value for the given property over all Objects matching the query. */
424+
/** @deprecated Use {@link #property(Property)} to get a {@link PropertyQuery} for aggregate functions. */
425+
@Deprecated
433426
public long max(final Property property) {
434-
return box.internalCallWithReaderHandle(new CallWithHandle<Long>() {
435-
@Override
436-
public Long call(long cursorHandle) {
437-
return nativeMax(handle, cursorHandle, property.getId());
438-
}
439-
});
427+
return property(property).max();
440428
}
441429

442-
/** Finds the maximum value for the given property over all Objects matching the query. */
430+
/** @deprecated Use {@link #property(Property)} to get a {@link PropertyQuery} for aggregate functions. */
431+
@Deprecated
443432
public double maxDouble(final Property property) {
444-
return box.internalCallWithReaderHandle(new CallWithHandle<Double>() {
445-
@Override
446-
public Double call(long cursorHandle) {
447-
return nativeMaxDouble(handle, cursorHandle, property.getId());
448-
}
449-
});
433+
return property(property).maxDouble();
450434
}
451435

452-
/** Finds the minimum value for the given property over all Objects matching the query. */
436+
/** @deprecated Use {@link #property(Property)} to get a {@link PropertyQuery} for aggregate functions. */
437+
@Deprecated
453438
public long min(final Property property) {
454-
return box.internalCallWithReaderHandle(new CallWithHandle<Long>() {
455-
@Override
456-
public Long call(long cursorHandle) {
457-
return nativeMin(handle, cursorHandle, property.getId());
458-
}
459-
});
439+
return property(property).min();
460440
}
461441

462-
/** Finds the minimum value for the given property over all Objects matching the query. */
442+
/** @deprecated Use {@link #property(Property)} to get a {@link PropertyQuery} for aggregate functions. */
443+
@Deprecated
463444
public double minDouble(final Property property) {
464-
return box.internalCallWithReaderHandle(new CallWithHandle<Double>() {
465-
@Override
466-
public Double call(long cursorHandle) {
467-
return nativeMinDouble(handle, cursorHandle, property.getId());
468-
}
469-
});
445+
return property(property).minDouble();
470446
}
471447

472-
/** Calculates the average of all values for the given property over all Objects matching the query. */
448+
/** @deprecated Use {@link #property(Property)} to get a {@link PropertyQuery} for aggregate functions. */
449+
@Deprecated
473450
public double avg(final Property property) {
474-
return box.internalCallWithReaderHandle(new CallWithHandle<Double>() {
475-
@Override
476-
public Double call(long cursorHandle) {
477-
return nativeAvg(handle, cursorHandle, property.getId());
478-
}
479-
});
451+
return property(property).avg();
480452
}
481453

482-
483454
/**
484455
* Sets a parameter previously given to the {@link QueryBuilder} to a new value.
485456
*/

0 commit comments

Comments
 (0)