Skip to content

Commit 801464c

Browse files
committed
PropertyQuery support for null values
1 parent 56d2081 commit 801464c

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

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

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.util.concurrent.Callable;
2121

22-
import io.objectbox.InternalAccess;
2322
import io.objectbox.Property;
2423

2524
/**
@@ -33,6 +32,12 @@ public class PropertyQuery {
3332
final Property property;
3433
boolean distinct;
3534
boolean noCaseIfDistinct = true;
35+
boolean enableNull;
36+
37+
double nullValueDouble;
38+
float nullValueFloat;
39+
String nullValueString;
40+
long nullValueLong;
3641

3742
PropertyQuery(Query query, Property property) {
3843
this.query = query;
@@ -63,6 +68,33 @@ public PropertyQuery distinct(QueryBuilder.StringOrder stringOrder) {
6368
return this;
6469
}
6570

71+
public PropertyQuery nullValue(long nullValue) {
72+
enableNull = true;
73+
this.nullValueLong = nullValue;
74+
return this;
75+
}
76+
77+
public PropertyQuery nullValue(float nullValue) {
78+
enableNull = true;
79+
this.nullValueFloat = nullValue;
80+
return this;
81+
}
82+
83+
public PropertyQuery nullValue(double nullValue) {
84+
enableNull = true;
85+
this.nullValueDouble = nullValue;
86+
return this;
87+
}
88+
89+
public PropertyQuery nullValue(String nullValue) {
90+
if (nullValue == null) {
91+
throw new IllegalArgumentException("Null strings are not allowed (yet)");
92+
}
93+
enableNull = true;
94+
this.nullValueString = nullValue;
95+
return this;
96+
}
97+
6698
/**
6799
* Find the values for the given string property for objects matching the query.
68100
* <p>
@@ -80,7 +112,8 @@ public String[] findStrings() {
80112
public String[] call() {
81113
boolean distinctNoCase = distinct && noCaseIfDistinct;
82114
long cursorHandle = query.cursorHandle();
83-
return query.nativeFindStrings(query.handle, cursorHandle, property.id, distinct,distinctNoCase);
115+
return query.nativeFindStrings(query.handle, cursorHandle, property.id, distinct, distinctNoCase,
116+
enableNull, nullValueString);
84117
}
85118
});
86119
}
@@ -100,7 +133,8 @@ public long[] findLongs() {
100133
return (long[]) query.callInReadTx(new Callable<long[]>() {
101134
@Override
102135
public long[] call() {
103-
return query.nativeFindLongs(query.handle, query.cursorHandle(), property.id, distinct);
136+
return query.nativeFindLongs(query.handle, query.cursorHandle(), property.id, distinct,
137+
enableNull, nullValueLong);
104138
}
105139
});
106140
}
@@ -118,7 +152,8 @@ public int[] findInts() {
118152
return (int[]) query.callInReadTx(new Callable<int[]>() {
119153
@Override
120154
public int[] call() {
121-
return query.nativeFindInts(query.handle, query.cursorHandle(), property.id, distinct);
155+
return query.nativeFindInts(query.handle, query.cursorHandle(), property.id, distinct,
156+
enableNull, (int)nullValueLong);
122157
}
123158
});
124159
}
@@ -136,7 +171,8 @@ public short[] findShorts() {
136171
return (short[]) query.callInReadTx(new Callable<short[]>() {
137172
@Override
138173
public short[] call() {
139-
return query.nativeFindShorts(query.handle, query.cursorHandle(), property.id, distinct);
174+
return query.nativeFindShorts(query.handle, query.cursorHandle(), property.id, distinct,
175+
enableNull, (short) nullValueLong);
140176
}
141177
});
142178
}
@@ -154,7 +190,8 @@ public char[] findChars() {
154190
return (char[]) query.callInReadTx(new Callable<char[]>() {
155191
@Override
156192
public char[] call() {
157-
return query.nativeFindChars(query.handle, query.cursorHandle(), property.id, distinct);
193+
return query.nativeFindChars(query.handle, query.cursorHandle(), property.id, distinct,
194+
enableNull, (char) nullValueLong);
158195
}
159196
});
160197
}
@@ -170,7 +207,8 @@ public byte[] findBytes() {
170207
return (byte[]) query.callInReadTx(new Callable<byte[]>() {
171208
@Override
172209
public byte[] call() {
173-
return query.nativeFindBytes(query.handle, query.cursorHandle(), property.id, distinct);
210+
return query.nativeFindBytes(query.handle, query.cursorHandle(), property.id, distinct,
211+
enableNull, (byte) nullValueLong);
174212
}
175213
});
176214
}
@@ -188,7 +226,8 @@ public float[] findFloats() {
188226
return (float[]) query.callInReadTx(new Callable<float[]>() {
189227
@Override
190228
public float[] call() {
191-
return query.nativeFindFloats(query.handle, query.cursorHandle(), property.id, distinct);
229+
return query.nativeFindFloats(query.handle, query.cursorHandle(), property.id, distinct,
230+
enableNull, nullValueFloat);
192231
}
193232
});
194233
}
@@ -206,7 +245,8 @@ public double[] findDoubles() {
206245
return (double[]) query.callInReadTx(new Callable<double[]>() {
207246
@Override
208247
public double[] call() {
209-
return query.nativeFindDoubles(query.handle, query.cursorHandle(), property.id, distinct);
248+
return query.nativeFindDoubles(query.handle, query.cursorHandle(), property.id, distinct,
249+
enableNull, nullValueDouble);
210250
}
211251
});
212252
}

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,28 @@ public class Query<T> {
6060
native long[] nativeFindKeysUnordered(long handle, long cursorHandle);
6161

6262
native String[] nativeFindStrings(long handle, long cursorHandle, int propertyId, boolean distinct,
63-
boolean distinctNoCase);
63+
boolean distinctNoCase, boolean enableNull, String nullValue);
6464

65-
native long[] nativeFindLongs(long handle, long cursorHandle, int propertyId, boolean distinct);
65+
native long[] nativeFindLongs(long handle, long cursorHandle, int propertyId, boolean distinct, boolean enableNull,
66+
long nullValue);
6667

67-
native int[] nativeFindInts(long handle, long cursorHandle, int propertyId, boolean distinct);
68+
native int[] nativeFindInts(long handle, long cursorHandle, int propertyId, boolean distinct, boolean enableNull,
69+
int nullValue);
6870

69-
native short[] nativeFindShorts(long handle, long cursorHandle, int propertyId, boolean distinct);
71+
native short[] nativeFindShorts(long handle, long cursorHandle, int propertyId, boolean distinct,
72+
boolean enableNull, short nullValue);
7073

71-
native char[] nativeFindChars(long handle, long cursorHandle, int propertyId, boolean distinct);
74+
native char[] nativeFindChars(long handle, long cursorHandle, int propertyId, boolean distinct, boolean enableNull,
75+
char nullValue);
7276

73-
native byte[] nativeFindBytes(long handle, long cursorHandle, int propertyId, boolean distinct);
77+
native byte[] nativeFindBytes(long handle, long cursorHandle, int propertyId, boolean distinct, boolean enableNull,
78+
byte nullValue);
7479

75-
native float[] nativeFindFloats(long handle, long cursorHandle, int propertyId, boolean distinct);
80+
native float[] nativeFindFloats(long handle, long cursorHandle, int propertyId, boolean distinct,
81+
boolean enableNull, float nullValue);
7682

77-
native double[] nativeFindDoubles(long handle, long cursorHandle, int propertyId, boolean distinct);
83+
native double[] nativeFindDoubles(long handle, long cursorHandle, int propertyId, boolean distinct,
84+
boolean enableNull, double nullValue);
7885

7986
native long nativeCount(long handle, long cursorHandle);
8087

0 commit comments

Comments
 (0)