Skip to content

Commit 0740fb7

Browse files
Merge branch 'housekeeping' into 'dev'
Housekeeping See merge request objectbox/objectbox-java!29
2 parents 5044123 + c62209b commit 0740fb7

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class Query<T> {
5353

5454
native Object nativeFindUnique(long handle, long cursorHandle);
5555

56-
native List nativeFind(long handle, long cursorHandle, long offset, long limit);
56+
native List<T> nativeFind(long handle, long cursorHandle, long offset, long limit) throws Exception;
5757

5858
native long[] nativeFindIds(long handle, long cursorHandle, long offset, long limit);
5959

@@ -90,29 +90,31 @@ native void nativeSetParameter(long handle, int entityId, int propertyId, @Nulla
9090

9191
final Box<T> box;
9292
private final BoxStore store;
93-
private final boolean hasOrder;
9493
private final QueryPublisher<T> publisher;
9594
private final List<EagerRelation> eagerRelations;
9695
private final QueryFilter<T> filter;
9796
private final Comparator<T> comparator;
9897
private final int queryAttempts;
99-
private final int initialRetryBackOffInMs = 10;
98+
private static final int INITIAL_RETRY_BACK_OFF_IN_MS = 10;
10099

101100
long handle;
102101

103-
Query(Box<T> box, long queryHandle, boolean hasOrder, List<EagerRelation> eagerRelations, QueryFilter<T> filter,
102+
Query(Box<T> box, long queryHandle, List<EagerRelation> eagerRelations, QueryFilter<T> filter,
104103
Comparator<T> comparator) {
105104
this.box = box;
106105
store = box.getStore();
107106
queryAttempts = store.internalQueryAttempts();
108107
handle = queryHandle;
109-
this.hasOrder = hasOrder;
110108
publisher = new QueryPublisher<>(this, box);
111109
this.eagerRelations = eagerRelations;
112110
this.filter = filter;
113111
this.comparator = comparator;
114112
}
115113

114+
/**
115+
* Explicitly call {@link #close()} instead.
116+
*/
117+
@SuppressWarnings("deprecation") // finalize()
116118
@Override
117119
protected void finalize() throws Throwable {
118120
close();
@@ -220,8 +222,8 @@ public List<T> find(final long offset, final long limit) {
220222
ensureNoFilterNoComparator();
221223
return callInReadTx(new Callable<List<T>>() {
222224
@Override
223-
public List<T> call() {
224-
List entities = nativeFind(handle, cursorHandle(), offset, limit);
225+
public List<T> call() throws Exception {
226+
List<T> entities = nativeFind(handle, cursorHandle(), offset, limit);
225227
resolveEagerRelations(entities);
226228
return entities;
227229
}
@@ -232,7 +234,7 @@ public List<T> call() {
232234
* Very efficient way to get just the IDs without creating any objects. IDs can later be used to lookup objects
233235
* (lookups by ID are also very efficient in ObjectBox).
234236
* <p>
235-
* Note: a filter set with {@link QueryBuilder#filter} will be silently ignored!
237+
* Note: a filter set with {@link QueryBuilder#filter(QueryFilter)} will be silently ignored!
236238
*/
237239
@Nonnull
238240
public long[] findIds() {
@@ -242,7 +244,7 @@ public long[] findIds() {
242244
/**
243245
* Like {@link #findIds()} but with a offset/limit param, e.g. for pagination.
244246
* <p>
245-
* Note: a filter set with {@link QueryBuilder#filter} will be silently ignored!
247+
* Note: a filter set with {@link QueryBuilder#filter(QueryFilter)} will be silently ignored!
246248
*/
247249
@Nonnull
248250
public long[] findIds(final long offset, final long limit) {
@@ -277,7 +279,7 @@ public PropertyQuery property(Property property) {
277279
}
278280

279281
<R> R callInReadTx(Callable<R> callable) {
280-
return store.callInReadTxWithRetry(callable, queryAttempts, initialRetryBackOffInMs, true);
282+
return store.callInReadTxWithRetry(callable, queryAttempts, INITIAL_RETRY_BACK_OFF_IN_MS, true);
281283
}
282284

283285
/**
@@ -328,37 +330,38 @@ public LazyList<T> findLazyCached() {
328330
return new LazyList<>(box, findIds(), true);
329331
}
330332

331-
void resolveEagerRelations(List entities) {
333+
void resolveEagerRelations(List<T> entities) {
332334
if (eagerRelations != null) {
333335
int entityIndex = 0;
334-
for (Object entity : entities) {
336+
for (T entity : entities) {
335337
resolveEagerRelationForNonNullEagerRelations(entity, entityIndex);
336338
entityIndex++;
337339
}
338340
}
339341
}
340342

341343
/** Note: no null check on eagerRelations! */
342-
void resolveEagerRelationForNonNullEagerRelations(@Nonnull Object entity, int entityIndex) {
344+
void resolveEagerRelationForNonNullEagerRelations(@Nonnull T entity, int entityIndex) {
343345
for (EagerRelation eagerRelation : eagerRelations) {
344346
if (eagerRelation.limit == 0 || entityIndex < eagerRelation.limit) {
345347
resolveEagerRelation(entity, eagerRelation);
346348
}
347349
}
348350
}
349351

350-
void resolveEagerRelation(@Nullable Object entity) {
352+
void resolveEagerRelation(@Nullable T entity) {
351353
if (eagerRelations != null && entity != null) {
352354
for (EagerRelation eagerRelation : eagerRelations) {
353355
resolveEagerRelation(entity, eagerRelation);
354356
}
355357
}
356358
}
357359

358-
void resolveEagerRelation(@Nonnull Object entity, EagerRelation eagerRelation) {
360+
void resolveEagerRelation(@Nonnull T entity, EagerRelation eagerRelation) {
359361
if (eagerRelations != null) {
360362
RelationInfo relationInfo = eagerRelation.relationInfo;
361363
if (relationInfo.toOneGetter != null) {
364+
//noinspection unchecked Can't know target entity type.
362365
ToOne toOne = relationInfo.toOneGetter.getToOne(entity);
363366
if (toOne != null) {
364367
toOne.getTarget();
@@ -367,8 +370,10 @@ void resolveEagerRelation(@Nonnull Object entity, EagerRelation eagerRelation) {
367370
if (relationInfo.toManyGetter == null) {
368371
throw new IllegalStateException("Relation info without relation getter: " + relationInfo);
369372
}
373+
//noinspection unchecked Can't know target entity type.
370374
List toMany = relationInfo.toManyGetter.getToMany(entity);
371375
if (toMany != null) {
376+
//noinspection ResultOfMethodCallIgnored Triggers fetching target entities.
372377
toMany.size();
373378
}
374379
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import io.objectbox.annotation.apihint.Internal;
2929
import io.objectbox.relation.RelationInfo;
3030

31+
import javax.annotation.Nullable;
32+
3133
/**
3234
* With QueryBuilder you define custom queries returning matching entities. Using the methods of this class you can
3335
* select (filter) results for specific data (for example #{@link #equal(Property, String)} and
@@ -91,8 +93,6 @@ enum Operator {
9193

9294
private long handle;
9395

94-
private boolean hasOrder;
95-
9696
private long lastCondition;
9797
private Operator combineNextWith = Operator.NONE;
9898

@@ -190,6 +190,10 @@ private QueryBuilder(long storeHandle, long subQueryBuilderHandle) {
190190
isSubQuery = true;
191191
}
192192

193+
/**
194+
* Explicitly call {@link #close()} instead.
195+
*/
196+
@SuppressWarnings("deprecation") // finalize()
193197
@Override
194198
protected void finalize() throws Throwable {
195199
close();
@@ -215,7 +219,7 @@ public Query<T> build() {
215219
throw new IllegalStateException("Incomplete logic condition. Use or()/and() between two conditions only.");
216220
}
217221
long queryHandle = nativeBuild(handle);
218-
Query<T> query = new Query<>(box, queryHandle, hasOrder, eagerRelations, filter, comparator);
222+
Query<T> query = new Query<>(box, queryHandle, eagerRelations, filter, comparator);
219223
close();
220224
return query;
221225
}
@@ -281,7 +285,6 @@ public QueryBuilder<T> order(Property<T> property, int flags) {
281285
"An operator is pending. Use operators like and() and or() only between two conditions.");
282286
}
283287
nativeOrder(handle, property.getId(), flags);
284-
hasOrder = true;
285288
return this;
286289
}
287290

@@ -368,7 +371,7 @@ public QueryBuilder<T> eager(RelationInfo relationInfo, RelationInfo... more) {
368371
* @param relationInfo The relation as found in the generated meta info class ("EntityName_") of class T.
369372
* @param more Supply further relations to be eagerly loaded.
370373
*/
371-
public QueryBuilder<T> eager(int limit, RelationInfo relationInfo, RelationInfo... more) {
374+
public QueryBuilder<T> eager(int limit, RelationInfo relationInfo, @Nullable RelationInfo... more) {
372375
verifyNotSubQuery();
373376
if (eagerRelations == null) {
374377
eagerRelations = new ArrayList<>();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class QueryPublisher<T> implements DataPublisher<List<T>> {
3535

3636
private final Query<T> query;
3737
private final Box<T> box;
38-
private final Set<DataObserver<List<T>>> observers = new CopyOnWriteArraySet();
38+
private final Set<DataObserver<List<T>>> observers = new CopyOnWriteArraySet<>();
3939

4040
private DataObserver<Class<T>> objectClassObserver;
4141
private DataSubscription objectClassSubscription;

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ public void testTransformer() throws InterruptedException {
9191
query.subscribe().transform(new DataTransformer<List<TestEntity>, Integer>() {
9292

9393
@Override
94-
@SuppressWarnings("NullableProblems")
95-
public Integer transform(List<TestEntity> source) throws Exception {
94+
public Integer transform(List<TestEntity> source) {
9695
int sum = 0;
9796
for (TestEntity entity : source) {
9897
sum += entity.getSimpleInt();
@@ -118,8 +117,8 @@ public void onData(Integer data) {
118117
assertEquals(2003 + 2007 + 2002, (int) receivedSums.get(1));
119118
}
120119

121-
private List<TestEntity> putTestEntitiesScalars() {
122-
return putTestEntities(10, null, 2000);
120+
private void putTestEntitiesScalars() {
121+
putTestEntities(10, null, 2000);
123122
}
124123

125124
@Override

0 commit comments

Comments
 (0)