Skip to content

Commit a0839bc

Browse files
Query: throw if filter is used with count().
1 parent 71a6866 commit a0839bc

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,21 @@ public T call() {
157157
}
158158

159159
private void ensureNoFilterNoComparator() {
160+
ensureNoFilter();
161+
ensureNoComparator();
162+
}
163+
164+
private void ensureNoFilter() {
160165
if (filter != null) {
161-
throw new UnsupportedOperationException("Does not yet work with a filter yet. " +
162-
"At this point, only find() and forEach() are supported with filters.");
166+
throw new UnsupportedOperationException("Does not work with a filter. " +
167+
"Only find() and forEach() support filters.");
163168
}
164-
ensureNoComparator();
165169
}
166170

167171
private void ensureNoComparator() {
168172
if (comparator != null) {
169-
throw new UnsupportedOperationException("Does not yet work with a sorting comparator yet. " +
170-
"At this point, only find() is supported with sorting comparators.");
173+
throw new UnsupportedOperationException("Does not work with a sorting comparator. " +
174+
"Only find() supports sorting with a comparator.");
171175
}
172176
}
173177

@@ -385,6 +389,7 @@ void resolveEagerRelation(@Nonnull T entity, EagerRelation eagerRelation) {
385389

386390
/** Returns the count of Objects matching the query. */
387391
public long count() {
392+
ensureNoFilter();
388393
return box.internalCallWithReaderHandle(new CallWithHandle<Long>() {
389394
@Override
390395
public Long call(long cursorHandle) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public void comparator_find() {
6767
assertEquals("apple", entities.get(4).getSimpleString());
6868
}
6969

70+
@Test(expected = UnsupportedOperationException.class)
71+
public void filter_count_unsupported() {
72+
box.query()
73+
.filter(createTestFilter())
74+
.build()
75+
.count();
76+
}
77+
7078
@Test(expected = UnsupportedOperationException.class)
7179
public void filter_findFirst_unsupported() {
7280
box.query()

0 commit comments

Comments
 (0)