Skip to content

Commit 75aebf3

Browse files
committed
PropertyQuery: added findShorts, findChars, findFloats, findDoubles
1 parent 92a1c2c commit 75aebf3

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,44 @@ public int[] call() {
125125
});
126126
}
127127

128+
/**
129+
* Find the values for the given int property for objects matching the query.
130+
* <p>
131+
* Note: null values are excluded from results.
132+
* <p>
133+
* Note: results are not guaranteed to be in any particular order.
134+
* <p>
135+
* See also: {@link #distinct}
136+
*/
137+
public short[] findShorts() {
138+
return (short[]) query.callInReadTx(new Callable<short[]>() {
139+
@Override
140+
public short[] call() {
141+
long cursorHandle = InternalAccess.getActiveTxCursorHandle(query.box);
142+
return query.nativeFindShorts(query.handle, cursorHandle, property.id, distinct);
143+
}
144+
});
145+
}
146+
147+
/**
148+
* Find the values for the given int property for objects matching the query.
149+
* <p>
150+
* Note: null values are excluded from results.
151+
* <p>
152+
* Note: results are not guaranteed to be in any particular order.
153+
* <p>
154+
* See also: {@link #distinct}
155+
*/
156+
public char[] findChars() {
157+
return (char[]) query.callInReadTx(new Callable<char[]>() {
158+
@Override
159+
public char[] call() {
160+
long cursorHandle = InternalAccess.getActiveTxCursorHandle(query.box);
161+
return query.nativeFindChars(query.handle, cursorHandle, property.id, distinct);
162+
}
163+
});
164+
}
165+
128166
/**
129167
* Find the values for the given byte property for objects matching the query.
130168
* <p>
@@ -142,4 +180,42 @@ public byte[] call() {
142180
});
143181
}
144182

183+
/**
184+
* Find the values for the given int property for objects matching the query.
185+
* <p>
186+
* Note: null values are excluded from results.
187+
* <p>
188+
* Note: results are not guaranteed to be in any particular order.
189+
* <p>
190+
* See also: {@link #distinct}
191+
*/
192+
public float[] findFloats() {
193+
return (float[]) query.callInReadTx(new Callable<float[]>() {
194+
@Override
195+
public float[] call() {
196+
long cursorHandle = InternalAccess.getActiveTxCursorHandle(query.box);
197+
return query.nativeFindFloats(query.handle, cursorHandle, property.id, distinct);
198+
}
199+
});
200+
}
201+
202+
/**
203+
* Find the values for the given int property for objects matching the query.
204+
* <p>
205+
* Note: null values are excluded from results.
206+
* <p>
207+
* Note: results are not guaranteed to be in any particular order.
208+
* <p>
209+
* See also: {@link #distinct}
210+
*/
211+
public double[] findDoubles() {
212+
return (double[]) query.callInReadTx(new Callable<double[]>() {
213+
@Override
214+
public double[] call() {
215+
long cursorHandle = InternalAccess.getActiveTxCursorHandle(query.box);
216+
return query.nativeFindDoubles(query.handle, cursorHandle, property.id, distinct);
217+
}
218+
});
219+
}
220+
145221
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,16 @@ native String[] nativeFindStrings(long handle, long cursorHandle, int propertyId
6666

6767
native int[] nativeFindInts(long handle, long cursorHandle, int propertyId, boolean distinct);
6868

69+
native short[] nativeFindShorts(long handle, long cursorHandle, int propertyId, boolean distinct);
70+
71+
native char[] nativeFindChars(long handle, long cursorHandle, int propertyId, boolean distinct);
72+
6973
native byte[] nativeFindBytes(long handle, long cursorHandle, int propertyId, boolean distinct);
7074

75+
native float[] nativeFindFloats(long handle, long cursorHandle, int propertyId, boolean distinct);
76+
77+
native double[] nativeFindDoubles(long handle, long cursorHandle, int propertyId, boolean distinct);
78+
7179
native long nativeCount(long handle, long cursorHandle);
7280

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

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,59 @@ public void testFindInts() {
120120
assertEquals(1, query.property(simpleInt).distinct().findInts().length);
121121
}
122122

123+
@Test
124+
public void testFindShorts() {
125+
putTestEntities(5);
126+
Query<TestEntity> query = box.query().greater(simpleInt, 2).build();
127+
short[] result = query.property(simpleShort).findShorts() ;
128+
assertEquals(3, result.length);
129+
assertEquals(103, result[0]);
130+
assertEquals(104, result[1]);
131+
assertEquals(105, result[2]);
132+
133+
putTestEntity(null, 5);
134+
135+
query = box.query().greater(simpleInt, 4).build();
136+
assertEquals(2, query.property(simpleShort).findShorts().length);
137+
assertEquals(1, query.property(simpleShort).distinct().findShorts().length);
138+
}
139+
140+
// TODO @Test for findChars (no char property in entity)
141+
142+
@Test
143+
public void testFindFloats() {
144+
putTestEntities(5);
145+
Query<TestEntity> query = box.query().greater(simpleInt, 2).build();
146+
float[] result = query.property(simpleFloat).findFloats() ;
147+
assertEquals(3, result.length);
148+
assertEquals(200.3f, result[0], 0.0001f);
149+
assertEquals(200.4f, result[1], 0.0001f);
150+
assertEquals(200.5f, result[2], 0.0001f);
151+
152+
putTestEntity(null, 5);
153+
154+
query = box.query().greater(simpleInt, 4).build();
155+
assertEquals(2, query.property(simpleFloat).findFloats().length);
156+
assertEquals(1, query.property(simpleFloat).distinct().findFloats().length);
157+
}
158+
159+
@Test
160+
public void testFindDoubles() {
161+
putTestEntities(5);
162+
Query<TestEntity> query = box.query().greater(simpleInt, 2).build();
163+
double[] result = query.property(simpleDouble).findDoubles() ;
164+
assertEquals(3, result.length);
165+
assertEquals(2000.03, result[0], 0.0001);
166+
assertEquals(2000.04, result[1], 0.0001);
167+
assertEquals(2000.05, result[2], 0.0001);
168+
169+
putTestEntity(null, 5);
170+
171+
query = box.query().greater(simpleInt, 4).build();
172+
assertEquals(2, query.property(simpleDouble).findDoubles().length);
173+
assertEquals(1, query.property(simpleDouble).distinct().findDoubles().length);
174+
}
175+
123176
@Test
124177
public void testFindBytes() {
125178
putTestEntities(5);
@@ -143,6 +196,18 @@ public void testFindLongs_wrongPropertyType() {
143196
box.query().build().property(simpleInt).findLongs();
144197
}
145198

199+
@Test(expected = IllegalArgumentException.class)
200+
public void testFindInts_wrongPropertyType() {
201+
putTestEntitiesStrings();
202+
box.query().build().property(simpleLong).findInts();
203+
}
204+
205+
@Test(expected = IllegalArgumentException.class)
206+
public void testFindShorts_wrongPropertyType() {
207+
putTestEntitiesStrings();
208+
box.query().build().property(simpleInt).findShorts();
209+
}
210+
146211
private List<TestEntity> putTestEntitiesScalars() {
147212
return putTestEntities(10, null, 2000);
148213
}

0 commit comments

Comments
 (0)