Skip to content

Commit c112ca6

Browse files
committed
add PropertyQuery.count()
1 parent 7d600be commit c112ca6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ native String nativeFindString(long handle, long cursorHandle, int propertyId, b
9494

9595
native double nativeAvg(long handle, long cursorHandle, int propertyId);
9696

97+
native long nativeCount(long handle, long cursorHandle, int propertyId, boolean distinct);
98+
9799
/** Clears all values (e.g. distinct and null value). */
98100
public PropertyQuery reset() {
99101
distinct = false;
@@ -450,4 +452,13 @@ public Double call() {
450452
});
451453
}
452454

453-
}
455+
public long count() {
456+
return (Long) query.callInReadTx(new Callable<Long>() {
457+
@Override
458+
public Long call() {
459+
return nativeCount(queryHandle, query.cursorHandle(), propertyId, distinct);
460+
}
461+
});
462+
}
463+
464+
}

tests/objectbox-java-test/src/test/java/io/objectbox/query/PropertyQueryTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,19 @@ public void testFindShorts_wrongPropertyType() {
427427
box.query().build().property(simpleInt).findShorts();
428428
}
429429

430+
@Test
431+
public void testCount() {
432+
putTestEntity(null, 1000);
433+
putTestEntity("BAR", 100);
434+
putTestEntitiesStrings();
435+
putTestEntity("banana", 101);
436+
Query<TestEntity> query = box.query().build();
437+
PropertyQuery stringQuery = query.property(simpleString);
438+
assertEquals(8, query.count());
439+
assertEquals(7, stringQuery.count());
440+
assertEquals(6, stringQuery.distinct().count());
441+
}
442+
430443
private List<TestEntity> putTestEntitiesScalars() {
431444
return putTestEntities(10, null, 2000);
432445
}

0 commit comments

Comments
 (0)