Skip to content

Commit 5c2a63d

Browse files
committed
find single long and int
1 parent f83c4ed commit 5c2a63d

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public int[] findInts() {
153153
@Override
154154
public int[] call() {
155155
return query.nativeFindInts(query.handle, query.cursorHandle(), property.id, distinct,
156-
enableNull, (int)nullValueLong);
156+
enableNull, (int) nullValueLong);
157157
}
158158
});
159159
}
@@ -251,4 +251,31 @@ public double[] call() {
251251
});
252252
}
253253

254+
private Object findNumber(final boolean unique) {
255+
return query.callInReadTx(new Callable<Object>() {
256+
@Override
257+
public Object call() {
258+
return query.nativeFindNumber(query.handle, query.cursorHandle(), property.id, unique,
259+
enableNull, nullValueLong, nullValueFloat, nullValueDouble);
260+
}
261+
});
262+
}
263+
264+
public Long findFirstLong() {
265+
return (Long) findNumber(false);
266+
}
267+
268+
public Long findUniqueLong() {
269+
return (Long) findNumber(true);
270+
}
271+
272+
273+
public Integer findFirstInt() {
274+
return (Integer) findNumber(false);
275+
}
276+
277+
public Integer findUniqueInt() {
278+
return (Integer) findNumber(true);
279+
}
280+
254281
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ native float[] nativeFindFloats(long handle, long cursorHandle, int propertyId,
8383
native double[] nativeFindDoubles(long handle, long cursorHandle, int propertyId, boolean distinct,
8484
boolean enableNull, double nullValue);
8585

86+
native Object nativeFindNumber(long handle, long cursorHandle, int propertyId, boolean unique, boolean enableNull,
87+
long nullValue, float nullValueFloat, double nullValueDouble);
88+
8689
native long nativeCount(long handle, long cursorHandle);
8790

8891
native long nativeSum(long handle, long cursorHandle, int propertyId);

tests/objectbox-java-test/src/main/java/io/objectbox/query/PropertyQueryTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,42 @@ public void testFindLongs() {
148148
assertEquals(1, query.property(simpleLong).distinct().findLongs().length);
149149
}
150150

151+
@Test
152+
public void testFindLong() {
153+
putTestEntities(5);
154+
Query<TestEntity> query = box.query().greater(simpleLong, 1002).build();
155+
long result = query.property(simpleLong).findFirstLong();
156+
assertEquals(1003, result);
157+
158+
query = box.query().greater(simpleLong, 1004).build();
159+
assertEquals(1005, (long) query.property(simpleLong).distinct().findUniqueLong());
160+
}
161+
162+
@Test()
163+
public void testFindLong_uniqueFails() {
164+
putTestEntity(null, 1);
165+
putTestEntity(null, 1);
166+
box.query().build().property(simpleLong).findUniqueLong();
167+
}
168+
169+
@Test
170+
public void testFindInt() {
171+
putTestEntities(5);
172+
Query<TestEntity> query = box.query().greater(simpleLong, 1002).build();
173+
long result = query.property(simpleInt).findFirstInt();
174+
assertEquals(3, result);
175+
176+
query = box.query().greater(simpleLong, 1004).build();
177+
assertEquals(5, (long) query.property(simpleInt).distinct().findUniqueInt());
178+
}
179+
180+
@Test()
181+
public void testFindInt_uniqueFails() {
182+
putTestEntity(null, 1);
183+
putTestEntity(null, 1);
184+
box.query().build().property(simpleInt).findUniqueInt();
185+
}
186+
151187
@Test
152188
public void testFindInts() {
153189
putTestEntities(5);

0 commit comments

Comments
 (0)