Skip to content

Commit 7b60478

Browse files
committed
QueryBuilder: always verify handle before using it
1 parent 3788244 commit 7b60478

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ public synchronized void close() {
157157
* Builds the query and closes this QueryBuilder.
158158
*/
159159
public Query<T> build() {
160-
if (handle == 0) {
161-
throw new IllegalStateException("This QueryBuilder has already been closed. Please use a new instance.");
162-
}
160+
verifyHandle();
163161
if (combineNextWith != Operator.NONE) {
164162
throw new IllegalStateException("Incomplete logic condition. Use or()/and() between two conditions only.");
165163
}
@@ -169,6 +167,12 @@ public Query<T> build() {
169167
return query;
170168
}
171169

170+
private void verifyHandle() {
171+
if (handle == 0) {
172+
throw new IllegalStateException("This QueryBuilder has already been closed. Please use a new instance.");
173+
}
174+
}
175+
172176
/**
173177
* Specifies given property to be used for sorting.
174178
* Shorthand for {@link #order(Property, int)} with flags equal to 0.
@@ -211,6 +215,7 @@ public QueryBuilder<T> orderDesc(Property property) {
211215
* @see #orderDesc(Property)
212216
*/
213217
public QueryBuilder<T> order(Property property, int flags) {
218+
verifyHandle();
214219
if (combineNextWith != Operator.NONE) {
215220
throw new IllegalStateException(
216221
"An operator is pending. Use operators like and() and or() only between two conditions.");
@@ -346,106 +351,126 @@ private void checkCombineCondition(long currentCondition) {
346351
}
347352

348353
public QueryBuilder<T> isNull(Property property) {
354+
verifyHandle();
349355
checkCombineCondition(nativeNull(handle, property.getId()));
350356
return this;
351357
}
352358

353359
public QueryBuilder<T> notNull(Property property) {
360+
verifyHandle();
354361
checkCombineCondition(nativeNotNull(handle, property.getId()));
355362
return this;
356363
}
357364

358365
public QueryBuilder<T> equal(Property property, long value) {
366+
verifyHandle();
359367
checkCombineCondition(nativeEqual(handle, property.getId(), value));
360368
return this;
361369
}
362370

363371
public QueryBuilder<T> equal(Property property, boolean value) {
372+
verifyHandle();
364373
checkCombineCondition(nativeEqual(handle, property.getId(), value ? 1 : 0));
365374
return this;
366375
}
367376

368377
/** @throws NullPointerException if given value is null. Use {@link #isNull(Property)} instead. */
369378
public QueryBuilder<T> equal(Property property, Date value) {
379+
verifyHandle();
370380
checkCombineCondition(nativeEqual(handle, property.getId(), value.getTime()));
371381
return this;
372382
}
373383

374384
public QueryBuilder<T> notEqual(Property property, long value) {
385+
verifyHandle();
375386
checkCombineCondition(nativeNotEqual(handle, property.getId(), value));
376387
return this;
377388
}
378389

379390
public QueryBuilder<T> notEqual(Property property, boolean value) {
391+
verifyHandle();
380392
checkCombineCondition(nativeNotEqual(handle, property.getId(), value ? 1 : 0));
381393
return this;
382394
}
383395

384396
/** @throws NullPointerException if given value is null. Use {@link #isNull(Property)} instead. */
385397
public QueryBuilder<T> notEqual(Property property, Date value) {
398+
verifyHandle();
386399
checkCombineCondition(nativeNotEqual(handle, property.getId(), value.getTime()));
387400
return this;
388401
}
389402

390403
public QueryBuilder<T> less(Property property, long value) {
404+
verifyHandle();
391405
checkCombineCondition(nativeLess(handle, property.getId(), value));
392406
return this;
393407
}
394408

395409
public QueryBuilder<T> greater(Property property, long value) {
410+
verifyHandle();
396411
checkCombineCondition(nativeGreater(handle, property.getId(), value));
397412
return this;
398413
}
399414

400415
public QueryBuilder<T> less(Property property, Date value) {
416+
verifyHandle();
401417
checkCombineCondition(nativeLess(handle, property.getId(), value.getTime()));
402418
return this;
403419
}
404420

405421
/** @throws NullPointerException if given value is null. Use {@link #isNull(Property)} instead. */
406422
public QueryBuilder<T> greater(Property property, Date value) {
423+
verifyHandle();
407424
checkCombineCondition(nativeGreater(handle, property.getId(), value.getTime()));
408425
return this;
409426
}
410427

411428
public QueryBuilder<T> between(Property property, long value1, long value2) {
429+
verifyHandle();
412430
checkCombineCondition(nativeBetween(handle, property.getId(), value1, value2));
413431
return this;
414432
}
415433

416434
/** @throws NullPointerException if one of the given values is null. */
417435
public QueryBuilder<T> between(Property property, Date value1, Date value2) {
436+
verifyHandle();
418437
checkCombineCondition(nativeBetween(handle, property.getId(), value1.getTime(), value2.getTime()));
419438
return this;
420439
}
421440

422441
// FIXME DbException: invalid unordered_map<K, T> key
423442
public QueryBuilder<T> in(Property property, long[] values) {
443+
verifyHandle();
424444
checkCombineCondition(nativeIn(handle, property.getId(), values, false));
425445
return this;
426446
}
427447

428448
public QueryBuilder<T> in(Property property, int[] values) {
449+
verifyHandle();
429450
checkCombineCondition(nativeIn(handle, property.getId(), values, false));
430451
return this;
431452
}
432453

433454
public QueryBuilder<T> notIn(Property property, long[] values) {
455+
verifyHandle();
434456
checkCombineCondition(nativeIn(handle, property.getId(), values, true));
435457
return this;
436458
}
437459

438460
public QueryBuilder<T> notIn(Property property, int[] values) {
461+
verifyHandle();
439462
checkCombineCondition(nativeIn(handle, property.getId(), values, true));
440463
return this;
441464
}
442465

443466
public QueryBuilder<T> equal(Property property, String value) {
467+
verifyHandle();
444468
checkCombineCondition(nativeEqual(handle, property.getId(), value, false));
445469
return this;
446470
}
447471

448472
// Help people with floating point equality...
473+
449474
/**
450475
* Floating point equality is non-trivial; this is just a convenience for
451476
* {@link #between(Property, double, double)} with parameters(property, value - tolerance, value + tolerance).
@@ -457,61 +482,73 @@ public QueryBuilder<T> equal(Property property, double value, double tolerance)
457482
}
458483

459484
public QueryBuilder<T> notEqual(Property property, String value) {
485+
verifyHandle();
460486
checkCombineCondition(nativeNotEqual(handle, property.getId(), value, false));
461487
return this;
462488
}
463489

464490
public QueryBuilder<T> contains(Property property, String value) {
491+
verifyHandle();
465492
checkCombineCondition(nativeContains(handle, property.getId(), value, false));
466493
return this;
467494
}
468495

469496
public QueryBuilder<T> startsWith(Property property, String value) {
497+
verifyHandle();
470498
checkCombineCondition(nativeStartsWith(handle, property.getId(), value, false));
471499
return this;
472500
}
473501

474502
public QueryBuilder<T> endsWith(Property property, String value) {
503+
verifyHandle();
475504
checkCombineCondition(nativeEndsWith(handle, property.getId(), value, false));
476505
return this;
477506
}
478507

479508
public QueryBuilder<T> equal(Property property, String value, StringOrder order) {
509+
verifyHandle();
480510
checkCombineCondition(nativeEqual(handle, property.getId(), value, order == StringOrder.CASE_SENSITIVE));
481511
return this;
482512
}
483513

484514
public QueryBuilder<T> notEqual(Property property, String value, StringOrder order) {
515+
verifyHandle();
485516
checkCombineCondition(nativeNotEqual(handle, property.getId(), value, order == StringOrder.CASE_SENSITIVE));
486517
return this;
487518
}
488519

489520
public QueryBuilder<T> contains(Property property, String value, StringOrder order) {
521+
verifyHandle();
490522
checkCombineCondition(nativeContains(handle, property.getId(), value, order == StringOrder.CASE_SENSITIVE));
491523
return this;
492524
}
493525

494526
public QueryBuilder<T> startsWith(Property property, String value, StringOrder order) {
527+
verifyHandle();
495528
checkCombineCondition(nativeStartsWith(handle, property.getId(), value, order == StringOrder.CASE_SENSITIVE));
496529
return this;
497530
}
498531

499532
public QueryBuilder<T> endsWith(Property property, String value, StringOrder order) {
533+
verifyHandle();
500534
checkCombineCondition(nativeEndsWith(handle, property.getId(), value, order == StringOrder.CASE_SENSITIVE));
501535
return this;
502536
}
503537

504538
public QueryBuilder<T> less(Property property, double value) {
539+
verifyHandle();
505540
checkCombineCondition(nativeLess(handle, property.getId(), value));
506541
return this;
507542
}
508543

509544
public QueryBuilder<T> greater(Property property, double value) {
545+
verifyHandle();
510546
checkCombineCondition(nativeGreater(handle, property.getId(), value));
511547
return this;
512548
}
513549

514550
public QueryBuilder<T> between(Property property, double value1, double value2) {
551+
verifyHandle();
515552
checkCombineCondition(nativeBetween(handle, property.getId(), value1, value2));
516553
return this;
517554
}

0 commit comments

Comments
 (0)