Skip to content

Commit 31ed540

Browse files
committed
Help people with floating point equality
1 parent 57de49f commit 31ed540

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
/**
1616
* With QueryBuilder you define custom queries returning matching entities. Using the methods of this class you can
1717
* select (filter) results for specific data (for example #{@link #equal(Property, String)} and
18-
* {@link #isNull(Property)}) and select an sort order for the resulting list (see {@link #order(Property)} and its overloads).
18+
* {@link #isNull(Property)}) and select an sort order for the resulting list (see {@link #order(Property)} and its
19+
* overloads).
1920
* <p>
20-
* Use {@link #build()} to conclude your query definitions and to get a {@link Query} object, which is used to actually get results.
21+
* Use {@link #build()} to conclude your query definitions and to get a {@link Query} object, which is used to actually
22+
* get results.
2123
* <p>
2224
* Note: Currently you can only query for complete entities. Returning individual property values or aggregates are
2325
* currently not available. Keep in mind that ObjectBox is very fast and the overhead to create an entity is very low.
@@ -443,6 +445,17 @@ public QueryBuilder<T> equal(Property property, String value) {
443445
return this;
444446
}
445447

448+
// Help people with floating point equality...
449+
/**
450+
* Floating point equality is non-trivial; this is just a convenience for
451+
* {@link #between(Property, double, double)} with parameters(property, value - tolerance, value + tolerance).
452+
* When using {@link Query#setParameters(Property, double, double)},
453+
* consider that the params are the lower and upper bounds.
454+
*/
455+
public QueryBuilder<T> equal(Property property, double value, double tolerance) {
456+
return between(property, value - tolerance, value + tolerance);
457+
}
458+
446459
public QueryBuilder<T> notEqual(Property property, String value) {
447460
checkCombineCondition(nativeNotEqual(handle, property.getId(), value, false));
448461
return this;

0 commit comments

Comments
 (0)