Skip to content

Commit 615fdc8

Browse files
Add type parameters for all Property<T> usage.
- Note: Query.setParameter also works with link queries, so allow any Property type.
1 parent caf778f commit 615fdc8

File tree

7 files changed

+52
-52
lines changed

7 files changed

+52
-52
lines changed

objectbox-java/src/main/java/io/objectbox/Box.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ public Class<T> getEntityClass() {
604604
}
605605

606606
@Internal
607-
public List<T> internalGetBacklinkEntities(int entityId, Property relationIdProperty, long key) {
607+
public List<T> internalGetBacklinkEntities(int entityId, Property<?> relationIdProperty, long key) {
608608
Cursor<T> reader = getReader();
609609
try {
610610
return reader.getBacklinkEntities(entityId, relationIdProperty, key);

objectbox-java/src/main/java/io/objectbox/BoxStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public static boolean isObjectBrowserAvailable() {
220220
entityTypeIdByClass.put(entityInfo.getEntityClass(), entityId);
221221
classByEntityTypeId.put(entityId, entityInfo.getEntityClass());
222222
propertiesByClass.put(entityInfo.getEntityClass(), entityInfo);
223-
for (Property property : entityInfo.getAllProperties()) {
223+
for (Property<?> property : entityInfo.getAllProperties()) {
224224
if (property.customType != null) {
225225
if (property.converterClass == null) {
226226
throw new RuntimeException("No converter class for custom type of " + property);

objectbox-java/src/main/java/io/objectbox/Cursor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ protected static native long collect004000(long cursor, long keyIfComplete, int
108108

109109
native int nativePropertyId(long cursor, String propertyValue);
110110

111-
native List nativeGetBacklinkEntities(long cursor, int entityId, int propertyId, long key);
111+
native List<T> nativeGetBacklinkEntities(long cursor, int entityId, int propertyId, long key);
112112

113113
native long[] nativeGetBacklinkIds(long cursor, int entityId, int propertyId, long key);
114114

115-
native List nativeGetRelationEntities(long cursor, int sourceEntityId, int relationId, long key, boolean backlink);
115+
native List<T> nativeGetRelationEntities(long cursor, int sourceEntityId, int relationId, long key, boolean backlink);
116116

117117
native long[] nativeGetRelationIds(long cursor, int sourceEntityId, int relationId, long key, boolean backlink);
118118

@@ -144,8 +144,8 @@ protected Cursor(Transaction tx, long cursor, EntityInfo<T> entityInfo, BoxStore
144144
this.entityInfo = entityInfo;
145145
this.boxStoreForEntities = boxStore;
146146

147-
Property[] allProperties = entityInfo.getAllProperties();
148-
for (Property property : allProperties) {
147+
Property<T>[] allProperties = entityInfo.getAllProperties();
148+
for (Property<T> property : allProperties) {
149149
if (!property.isIdVerified()) {
150150
int id = getPropertyId(property.dbName);
151151
property.verifyId(id);
@@ -281,7 +281,7 @@ long internalHandle() {
281281
}
282282

283283
@Internal
284-
List<T> getBacklinkEntities(int entityId, Property relationIdProperty, long key) {
284+
List<T> getBacklinkEntities(int entityId, Property<?> relationIdProperty, long key) {
285285
try {
286286
return nativeGetBacklinkEntities(cursor, entityId, relationIdProperty.getId(), key);
287287
} catch (IllegalArgumentException e) {
@@ -291,7 +291,7 @@ List<T> getBacklinkEntities(int entityId, Property relationIdProperty, long key)
291291
}
292292

293293
@Internal
294-
long[] getBacklinkIds(int entityId, Property relationIdProperty, long key) {
294+
long[] getBacklinkIds(int entityId, Property<?> relationIdProperty, long key) {
295295
try {
296296
return nativeGetBacklinkIds(cursor, entityId, relationIdProperty.getId(), key);
297297
} catch (IllegalArgumentException e) {

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
* (subject to change in a future version).
2626
*/
2727
@SuppressWarnings("WeakerAccess") // WeakerAccess: allow inner class access without accessor
28-
public class PropertyQuery {
29-
final Query query;
28+
public class PropertyQuery<T> {
29+
final Query<T> query;
3030
final long queryHandle;
31-
final Property property;
31+
final Property<T> property;
3232
final int propertyId;
3333

3434
boolean distinct;
@@ -41,7 +41,7 @@ public class PropertyQuery {
4141
String nullValueString;
4242
long nullValueLong;
4343

44-
PropertyQuery(Query query, Property property) {
44+
PropertyQuery(Query<T> query, Property<T> property) {
4545
this.query = query;
4646
queryHandle = query.handle;
4747
this.property = property;
@@ -97,7 +97,7 @@ native String nativeFindString(long handle, long cursorHandle, int propertyId, b
9797
native long nativeCount(long handle, long cursorHandle, int propertyId, boolean distinct);
9898

9999
/** Clears all values (e.g. distinct and null value). */
100-
public PropertyQuery reset() {
100+
public PropertyQuery<T> reset() {
101101
distinct = false;
102102
noCaseIfDistinct = true;
103103
unique = false;
@@ -115,7 +115,7 @@ public PropertyQuery reset() {
115115
* Note: strings default to case-insensitive comparision;
116116
* to change that call {@link #distinct(QueryBuilder.StringOrder)}.
117117
*/
118-
public PropertyQuery distinct() {
118+
public PropertyQuery<T> distinct() {
119119
distinct = true;
120120
return this;
121121
}
@@ -124,7 +124,7 @@ public PropertyQuery distinct() {
124124
* For string properties you can specify {@link io.objectbox.query.QueryBuilder.StringOrder#CASE_SENSITIVE} if you
125125
* want to have case sensitive distinct values (e.g. returning "foo","Foo","FOO" instead of "foo").
126126
*/
127-
public PropertyQuery distinct(QueryBuilder.StringOrder stringOrder) {
127+
public PropertyQuery<T> distinct(QueryBuilder.StringOrder stringOrder) {
128128
if (property.type != String.class) {
129129
throw new RuntimeException("Reserved for string properties, but got " + property);
130130
}
@@ -142,7 +142,7 @@ public PropertyQuery distinct(QueryBuilder.StringOrder stringOrder) {
142142
* <p>
143143
* Will be ignored for find methods returning multiple values, e.g. {@link #findInts()}.
144144
*/
145-
public PropertyQuery unique() {
145+
public PropertyQuery<T> unique() {
146146
unique = true;
147147
return this;
148148
}
@@ -152,7 +152,7 @@ public PropertyQuery unique() {
152152
* However, using this function, you can define an alternative value that will be returned for null values.
153153
* E.g. -1 for ins/longs or "NULL" for strings.
154154
*/
155-
public PropertyQuery nullValue(Object nullValue) {
155+
public PropertyQuery<T> nullValue(Object nullValue) {
156156
//noinspection ConstantConditions Annotation can not enforce non-null.
157157
if (nullValue == null) {
158158
throw new IllegalArgumentException("Null values are not allowed");
@@ -185,7 +185,7 @@ public PropertyQuery nullValue(Object nullValue) {
185185
* @return Found strings
186186
*/
187187
public String[] findStrings() {
188-
return (String[]) query.callInReadTx(() -> {
188+
return query.callInReadTx(() -> {
189189
boolean distinctNoCase = distinct && noCaseIfDistinct;
190190
long cursorHandle = query.cursorHandle();
191191
return nativeFindStrings(queryHandle, cursorHandle, propertyId, distinct, distinctNoCase,
@@ -205,7 +205,7 @@ public String[] findStrings() {
205205
* @return Found longs
206206
*/
207207
public long[] findLongs() {
208-
return (long[]) query.callInReadTx(() ->
208+
return query.callInReadTx(() ->
209209
nativeFindLongs(queryHandle, query.cursorHandle(), propertyId, distinct, enableNull, nullValueLong)
210210
);
211211
}
@@ -220,7 +220,7 @@ public long[] findLongs() {
220220
* See also: {@link #distinct()}
221221
*/
222222
public int[] findInts() {
223-
return (int[]) query.callInReadTx(() ->
223+
return query.callInReadTx(() ->
224224
nativeFindInts(queryHandle, query.cursorHandle(), propertyId, distinct, enableNull, (int) nullValueLong)
225225
);
226226
}
@@ -235,7 +235,7 @@ public int[] findInts() {
235235
* See also: {@link #distinct()}
236236
*/
237237
public short[] findShorts() {
238-
return (short[]) query.callInReadTx(() ->
238+
return query.callInReadTx(() ->
239239
nativeFindShorts(queryHandle, query.cursorHandle(), propertyId, distinct, enableNull, (short) nullValueLong)
240240
);
241241
}
@@ -250,7 +250,7 @@ public short[] findShorts() {
250250
* See also: {@link #distinct()}
251251
*/
252252
public char[] findChars() {
253-
return (char[]) query.callInReadTx(() ->
253+
return query.callInReadTx(() ->
254254
nativeFindChars(queryHandle, query.cursorHandle(), propertyId, distinct, enableNull, (char) nullValueLong)
255255
);
256256
}
@@ -263,7 +263,7 @@ public char[] findChars() {
263263
* Note: results are not guaranteed to be in any particular order.
264264
*/
265265
public byte[] findBytes() {
266-
return (byte[]) query.callInReadTx(() ->
266+
return query.callInReadTx(() ->
267267
nativeFindBytes(queryHandle, query.cursorHandle(), propertyId, distinct, enableNull, (byte) nullValueLong)
268268
);
269269
}
@@ -278,7 +278,7 @@ public byte[] findBytes() {
278278
* See also: {@link #distinct()}
279279
*/
280280
public float[] findFloats() {
281-
return (float[]) query.callInReadTx(() ->
281+
return query.callInReadTx(() ->
282282
nativeFindFloats(queryHandle, query.cursorHandle(), propertyId, distinct, enableNull, nullValueFloat)
283283
);
284284
}
@@ -293,13 +293,13 @@ public float[] findFloats() {
293293
* See also: {@link #distinct()}
294294
*/
295295
public double[] findDoubles() {
296-
return (double[]) query.callInReadTx(() ->
296+
return query.callInReadTx(() ->
297297
nativeFindDoubles(queryHandle, query.cursorHandle(), propertyId, distinct, enableNull, nullValueDouble)
298298
);
299299
}
300300

301301
public String findString() {
302-
return (String) query.callInReadTx(() -> {
302+
return query.callInReadTx(() -> {
303303
boolean distinctCase = distinct && !noCaseIfDistinct;
304304
return nativeFindString(queryHandle, query.cursorHandle(), propertyId, unique, distinct,
305305
distinctCase, enableNull, nullValueString);
@@ -357,7 +357,7 @@ public Double findDouble() {
357357
* This is different from Java arithmetic where it would "wrap around" (e.g. max. value + 1 = min. value).
358358
*/
359359
public long sum() {
360-
return (Long) query.callInReadTx(
360+
return query.callInReadTx(
361361
() -> nativeSum(queryHandle, query.cursorHandle(), propertyId)
362362
);
363363
}
@@ -370,7 +370,7 @@ public long sum() {
370370
* @return 0 in case no elements matched the query
371371
*/
372372
public double sumDouble() {
373-
return (Double) query.callInReadTx(
373+
return query.callInReadTx(
374374
() -> nativeSumDouble(queryHandle, query.cursorHandle(), propertyId)
375375
);
376376
}
@@ -381,7 +381,7 @@ public double sumDouble() {
381381
* @return Long.MIN_VALUE in case no elements matched the query
382382
*/
383383
public long max() {
384-
return (Long) query.callInReadTx(
384+
return query.callInReadTx(
385385
() -> nativeMax(queryHandle, query.cursorHandle(), propertyId)
386386
);
387387
}
@@ -392,7 +392,7 @@ public long max() {
392392
* @return NaN in case no elements matched the query
393393
*/
394394
public double maxDouble() {
395-
return (Double) query.callInReadTx(
395+
return query.callInReadTx(
396396
() -> nativeMaxDouble(queryHandle, query.cursorHandle(), propertyId)
397397
);
398398
}
@@ -403,7 +403,7 @@ public double maxDouble() {
403403
* @return Long.MAX_VALUE in case no elements matched the query
404404
*/
405405
public long min() {
406-
return (Long) query.callInReadTx(
406+
return query.callInReadTx(
407407
() -> nativeMin(queryHandle, query.cursorHandle(), propertyId)
408408
);
409409
}
@@ -414,7 +414,7 @@ public long min() {
414414
* @return NaN in case no elements matched the query
415415
*/
416416
public double minDouble() {
417-
return (Double) query.callInReadTx(
417+
return query.callInReadTx(
418418
() -> nativeMinDouble(queryHandle, query.cursorHandle(), propertyId)
419419
);
420420
}
@@ -427,7 +427,7 @@ public double minDouble() {
427427
* @return NaN in case no elements matched the query
428428
*/
429429
public double avg() {
430-
return (Double) query.callInReadTx(
430+
return query.callInReadTx(
431431
() -> nativeAvg(queryHandle, query.cursorHandle(), propertyId)
432432
);
433433
}
@@ -440,7 +440,7 @@ public double avg() {
440440
* @return 0 in case no elements matched the query
441441
*/
442442
public long avgLong() {
443-
return (Long) query.callInReadTx(
443+
return query.callInReadTx(
444444
() -> nativeAvgLong(queryHandle, query.cursorHandle(), propertyId)
445445
);
446446
}
@@ -451,7 +451,7 @@ public long avgLong() {
451451
* See also: {@link #distinct()}
452452
*/
453453
public long count() {
454-
return (Long) query.callInReadTx(
454+
return query.callInReadTx(
455455
() -> nativeCount(queryHandle, query.cursorHandle(), propertyId, distinct)
456456
);
457457
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ public LazyList<T> findLazy() {
267267
*
268268
* @param property the property for which to return values
269269
*/
270-
public PropertyQuery property(Property property) {
271-
return new PropertyQuery(this, property);
270+
public PropertyQuery<T> property(Property<T> property) {
271+
return new PropertyQuery<>(this, property);
272272
}
273273

274274
<R> R callInReadTx(Callable<R> callable) {
@@ -380,7 +380,7 @@ public long count() {
380380
/**
381381
* Sets a parameter previously given to the {@link QueryBuilder} to a new value.
382382
*/
383-
public Query<T> setParameter(Property property, String value) {
383+
public Query<T> setParameter(Property<?> property, String value) {
384384
nativeSetParameter(handle, property.getEntityId(), property.getId(), null, value);
385385
return this;
386386
}
@@ -398,7 +398,7 @@ public Query<T> setParameter(String alias, String value) {
398398
/**
399399
* Sets a parameter previously given to the {@link QueryBuilder} to a new value.
400400
*/
401-
public Query<T> setParameter(Property property, long value) {
401+
public Query<T> setParameter(Property<?> property, long value) {
402402
nativeSetParameter(handle, property.getEntityId(), property.getId(), null, value);
403403
return this;
404404
}
@@ -416,7 +416,7 @@ public Query<T> setParameter(String alias, long value) {
416416
/**
417417
* Sets a parameter previously given to the {@link QueryBuilder} to a new value.
418418
*/
419-
public Query<T> setParameter(Property property, double value) {
419+
public Query<T> setParameter(Property<?> property, double value) {
420420
nativeSetParameter(handle, property.getEntityId(), property.getId(), null, value);
421421
return this;
422422
}
@@ -436,7 +436,7 @@ public Query<T> setParameter(String alias, double value) {
436436
*
437437
* @throws NullPointerException if given date is null
438438
*/
439-
public Query<T> setParameter(Property property, Date value) {
439+
public Query<T> setParameter(Property<?> property, Date value) {
440440
return setParameter(property, value.getTime());
441441
}
442442

@@ -453,7 +453,7 @@ public Query<T> setParameter(String alias, Date value) {
453453
/**
454454
* Sets a parameter previously given to the {@link QueryBuilder} to a new value.
455455
*/
456-
public Query<T> setParameter(Property property, boolean value) {
456+
public Query<T> setParameter(Property<?> property, boolean value) {
457457
return setParameter(property, value ? 1 : 0);
458458
}
459459

@@ -469,7 +469,7 @@ public Query<T> setParameter(String alias, boolean value) {
469469
/**
470470
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
471471
*/
472-
public Query<T> setParameters(Property property, long value1, long value2) {
472+
public Query<T> setParameters(Property<?> property, long value1, long value2) {
473473
nativeSetParameters(handle, property.getEntityId(), property.getId(), null, value1, value2);
474474
return this;
475475
}
@@ -487,7 +487,7 @@ public Query<T> setParameters(String alias, long value1, long value2) {
487487
/**
488488
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
489489
*/
490-
public Query<T> setParameters(Property property, int[] values) {
490+
public Query<T> setParameters(Property<?> property, int[] values) {
491491
nativeSetParameters(handle, property.getEntityId(), property.getId(), null, values);
492492
return this;
493493
}
@@ -505,7 +505,7 @@ public Query<T> setParameters(String alias, int[] values) {
505505
/**
506506
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
507507
*/
508-
public Query<T> setParameters(Property property, long[] values) {
508+
public Query<T> setParameters(Property<?> property, long[] values) {
509509
nativeSetParameters(handle, property.getEntityId(), property.getId(), null, values);
510510
return this;
511511
}
@@ -523,7 +523,7 @@ public Query<T> setParameters(String alias, long[] values) {
523523
/**
524524
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
525525
*/
526-
public Query<T> setParameters(Property property, double value1, double value2) {
526+
public Query<T> setParameters(Property<?> property, double value1, double value2) {
527527
nativeSetParameters(handle, property.getEntityId(), property.getId(), null, value1, value2);
528528
return this;
529529
}
@@ -541,7 +541,7 @@ public Query<T> setParameters(String alias, double value1, double value2) {
541541
/**
542542
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
543543
*/
544-
public Query<T> setParameters(Property property, String[] values) {
544+
public Query<T> setParameters(Property<?> property, String[] values) {
545545
nativeSetParameters(handle, property.getEntityId(), property.getId(), null, values);
546546
return this;
547547
}
@@ -559,7 +559,7 @@ public Query<T> setParameters(String alias, String[] values) {
559559
/**
560560
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
561561
*/
562-
public Query<T> setParameter(Property property, byte[] value) {
562+
public Query<T> setParameter(Property<?> property, byte[] value) {
563563
nativeSetParameter(handle, property.getEntityId(), property.getId(), null, value);
564564
return this;
565565
}

0 commit comments

Comments
 (0)