Skip to content

Commit c89829a

Browse files
committed
PropertyQuery: single nullValue method
1 parent c27c2d5 commit c89829a

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,30 +101,28 @@ public PropertyQuery unique() {
101101
return this;
102102
}
103103

104-
public PropertyQuery nullValue(long nullValue) {
105-
enableNull = true;
106-
this.nullValueLong = nullValue;
107-
return this;
108-
}
109-
110-
public PropertyQuery nullValue(float nullValue) {
111-
enableNull = true;
112-
this.nullValueFloat = nullValue;
113-
return this;
114-
}
115-
116-
public PropertyQuery nullValue(double nullValue) {
117-
enableNull = true;
118-
this.nullValueDouble = nullValue;
119-
return this;
120-
}
121-
122-
public PropertyQuery nullValue(String nullValue) {
104+
/**
105+
* By default, null values are not returned by find methods (primitive arrays cannot contains nulls).
106+
* However, using this function, you can define an alternative value that will be returned for null values.
107+
* E.g. -1 for ins/longs or "NULL" for strings.
108+
*/
109+
public PropertyQuery nullValue(Object nullValue) {
123110
if (nullValue == null) {
124-
throw new IllegalArgumentException("Null strings are not allowed (yet)");
111+
throw new IllegalArgumentException("Null values are not allowed");
125112
}
113+
boolean isString = nullValue instanceof String;
114+
boolean isNumber = nullValue instanceof Number;
115+
if (!isString && !isNumber) {
116+
throw new IllegalArgumentException("Unsupported value class: " + nullValue.getClass());
117+
}
118+
126119
enableNull = true;
127-
this.nullValueString = nullValue;
120+
nullValueString = isString ? (String) nullValue : null;
121+
boolean isFloat = nullValue instanceof Float;
122+
nullValueFloat = isFloat ? (Float) nullValue : 0;
123+
boolean isDouble = nullValue instanceof Double;
124+
nullValueDouble = isDouble ? (Double) nullValue : 0;
125+
nullValueLong = isNumber && !isFloat && !isDouble ? ((Number) nullValue).longValue() : 0;
128126
return this;
129127
}
130128

0 commit comments

Comments
 (0)