Skip to content

Commit 7820070

Browse files
committed
PropertyQuery.unique flag instead of findFirst/Unique methods
1 parent 5fb1a26 commit 7820070

File tree

2 files changed

+55
-79
lines changed

2 files changed

+55
-79
lines changed

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

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class PropertyQuery {
3333
boolean distinct;
3434
boolean noCaseIfDistinct = true;
3535
boolean enableNull;
36+
boolean unique;
3637

3738
double nullValueDouble;
3839
float nullValueFloat;
@@ -48,6 +49,7 @@ public class PropertyQuery {
4849
public PropertyQuery reset() {
4950
distinct = false;
5051
noCaseIfDistinct = true;
52+
unique = false;
5153
enableNull = false;
5254
nullValueDouble = 0;
5355
nullValueFloat = 0;
@@ -80,6 +82,20 @@ public PropertyQuery distinct(QueryBuilder.StringOrder stringOrder) {
8082
return this;
8183
}
8284

85+
/**
86+
* For find methods returning single values, e.g. {@link #findInt()}, this will additional verify that the
87+
* resulting value is unique.
88+
* If there is any other resulting value resulting from this query, an exception will be thrown.
89+
* <p>
90+
* Can be combined with {@link #distinct()}.
91+
* <p>
92+
* Will be ignored for find methods returning multiple values, e.g. {@link #findInts()}.
93+
*/
94+
public PropertyQuery unique() {
95+
unique = true;
96+
return this;
97+
}
98+
8399
public PropertyQuery nullValue(long nullValue) {
84100
enableNull = true;
85101
this.nullValueLong = nullValue;
@@ -263,7 +279,7 @@ public double[] call() {
263279
});
264280
}
265281

266-
private String findString(final boolean unique) {
282+
public String findString() {
267283
return (String) query.callInReadTx(new Callable<String>() {
268284
@Override
269285
public String call() {
@@ -274,15 +290,7 @@ public String call() {
274290
});
275291
}
276292

277-
public String findFirstString() {
278-
return findString(false);
279-
}
280-
281-
public String findUniqueString() {
282-
return findString(true);
283-
}
284-
285-
private Object findNumber(final boolean unique) {
293+
private Object findNumber() {
286294
return query.callInReadTx(new Callable<Object>() {
287295
@Override
288296
public Object call() {
@@ -292,68 +300,36 @@ public Object call() {
292300
});
293301
}
294302

295-
public Long findFirstLong() {
296-
return (Long) findNumber(false);
297-
}
298-
299-
public Long findUniqueLong() {
300-
return (Long) findNumber(true);
301-
}
302-
303-
public Integer findFirstInt() {
304-
return (Integer) findNumber(false);
305-
}
306-
307-
public Integer findUniqueInt() {
308-
return (Integer) findNumber(true);
309-
}
310-
311-
public Short findFirstShort() {
312-
return (Short) findNumber(false);
313-
}
314-
315-
public Short findUniqueShort() {
316-
return (Short) findNumber(true);
317-
}
318-
319-
public Character findFirstChar() {
320-
return (Character) findNumber(false);
321-
}
322-
323-
public Character findUniqueChar() {
324-
return (Character) findNumber(true);
325-
}
326-
327-
public Byte findFirstByte() {
328-
return (Byte) findNumber(false);
303+
public Long findLong() {
304+
return (Long) findNumber();
329305
}
330306

331-
public Byte findUniqueByte() {
332-
return (Byte) findNumber(true);
307+
public Integer findInt() {
308+
return (Integer) findNumber();
333309
}
334310

335-
public Boolean findFirstBoolean() {
336-
return (Boolean) findNumber(false);
311+
public Short findShort() {
312+
return (Short) findNumber();
337313
}
338314

339-
public Boolean findUniqueBoolean() {
340-
return (Boolean) findNumber(true);
315+
public Character findChar() {
316+
return (Character) findNumber();
341317
}
342318

343-
public Float findFirstFloat() {
344-
return (Float) findNumber(false);
319+
public Byte findByte() {
320+
return (Byte) findNumber();
345321
}
346322

347-
public Float findUniqueFloat() {
348-
return (Float) findNumber(true);
323+
public Boolean findBoolean() {
324+
return (Boolean) findNumber();
349325
}
350326

351-
public Double findFirstDouble() {
352-
return (Double) findNumber(false);
327+
public Float findFloat() {
328+
return (Float) findNumber();
353329
}
354330

355-
public Double findUniqueDouble() {
356-
return (Double) findNumber(true);
331+
public Double findDouble() {
332+
return (Double) findNumber();
357333
}
358334

359335
}

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,26 @@ public void testFindStrings_wrongPropertyType() {
135135
public void testFindString() {
136136
Query<TestEntity> query = box.query().greater(simpleLong, 1002).build();
137137
PropertyQuery propertyQuery = query.property(simpleString);
138-
assertNull(propertyQuery.findFirstString());
139-
assertNull(propertyQuery.reset().findUniqueString());
138+
assertNull(propertyQuery.findString());
139+
assertNull(propertyQuery.reset().unique().findString());
140140
putTestEntities(5);
141-
assertEquals("foo3", propertyQuery.reset().findFirstString());
141+
assertEquals("foo3", propertyQuery.reset().findString());
142142

143143
query = box.query().greater(simpleLong, 1004).build();
144144
propertyQuery = query.property(simpleString);
145-
assertEquals("foo5", propertyQuery.reset().findUniqueString());
145+
assertEquals("foo5", propertyQuery.reset().unique().findString());
146146

147147
putTestEntity(null, 6);
148148
// TODO XXX enable me after fixing combination of unique and distinct: putTestEntity(null, 7);
149149
query.setParameter(simpleLong, 1005);
150-
assertEquals("nope", propertyQuery.reset().distinct().nullValue("nope").findUniqueString());
150+
assertEquals("nope", propertyQuery.reset().distinct().nullValue("nope").unique().findString());
151151
}
152152

153153
@Test(expected = DbException.class)
154154
public void testFindString_uniqueFails() {
155155
putTestEntity("foo", 1);
156156
putTestEntity("foo", 2);
157-
box.query().build().property(simpleString).findUniqueString();
157+
box.query().build().property(simpleString).unique().findString();
158158
}
159159

160160
@Test
@@ -177,32 +177,32 @@ public void testFindLongs() {
177177
@Test
178178
public void testFindLong() {
179179
Query<TestEntity> query = box.query().greater(simpleLong, 1002).build();
180-
assertNull(query.property(simpleLong).findFirstLong());
181-
assertNull(query.property(simpleLong).findUniqueLong());
180+
assertNull(query.property(simpleLong).findLong());
181+
assertNull(query.property(simpleLong).findLong());
182182
putTestEntities(5);
183-
assertEquals(1003, (long) query.property(simpleLong).findFirstLong());
183+
assertEquals(1003, (long) query.property(simpleLong).findLong());
184184

185185
query = box.query().greater(simpleLong, 1004).build();
186-
assertEquals(1005, (long) query.property(simpleLong).distinct().findUniqueLong());
186+
assertEquals(1005, (long) query.property(simpleLong).distinct().findLong());
187187
}
188188

189189
@Test(expected = DbException.class)
190190
public void testFindLong_uniqueFails() {
191191
putTestEntity(null, 1);
192192
putTestEntity(null, 1);
193-
box.query().build().property(simpleLong).findUniqueLong();
193+
box.query().build().property(simpleLong).unique().findLong();
194194
}
195195

196196
@Test
197197
public void testFindInt() {
198198
Query<TestEntity> query = box.query().greater(simpleLong, 1002).build();
199-
assertNull(query.property(simpleInt).findFirstInt());
200-
assertNull(query.property(simpleInt).findUniqueInt());
199+
assertNull(query.property(simpleInt).findInt());
200+
assertNull(query.property(simpleInt).unique().findInt());
201201
putTestEntities(5);
202-
assertEquals(3, (int) query.property(simpleInt).findFirstInt());
202+
assertEquals(3, (int) query.property(simpleInt).findInt());
203203

204204
query = box.query().greater(simpleLong, 1004).build();
205-
assertEquals(5, (int) query.property(simpleInt).distinct().findUniqueInt());
205+
assertEquals(5, (int) query.property(simpleInt).distinct().unique().findInt());
206206

207207
TestEntityCursor.INT_NULL_HACK = true;
208208
try {
@@ -211,26 +211,26 @@ public void testFindInt() {
211211
TestEntityCursor.INT_NULL_HACK = false;
212212
}
213213
query.setParameter(simpleLong, 1005);
214-
assertEquals(-99, (int) query.property(simpleInt).nullValue(-99).findUniqueInt());
214+
assertEquals(-99, (int) query.property(simpleInt).nullValue(-99).unique().findInt());
215215
}
216216

217217
@Test(expected = DbException.class)
218218
public void testFindInt_uniqueFails() {
219219
putTestEntity(null, 1);
220220
putTestEntity(null, 1);
221-
box.query().build().property(simpleInt).findUniqueInt();
221+
box.query().build().property(simpleInt).unique().findInt();
222222
}
223223

224224
@Test
225225
public void testFindDouble() {
226226
Query<TestEntity> query = box.query().greater(simpleLong, 1002).build();
227-
assertNull(query.property(simpleDouble).findFirstDouble());
228-
assertNull(query.property(simpleDouble).findUniqueDouble());
227+
assertNull(query.property(simpleDouble).findDouble());
228+
assertNull(query.property(simpleDouble).unique().findDouble());
229229
putTestEntities(5);
230-
assertEquals(2000.03, query.property(simpleDouble).findFirstDouble(), 0.001);
230+
assertEquals(2000.03, query.property(simpleDouble).findDouble(), 0.001);
231231

232232
query = box.query().greater(simpleLong, 1004).build();
233-
assertEquals(2000.05, query.property(simpleDouble).distinct().findUniqueDouble(), 0.001);
233+
assertEquals(2000.05, query.property(simpleDouble).distinct().unique().findDouble(), 0.001);
234234
}
235235

236236
// TODO add test for other types of single object find methods

0 commit comments

Comments
 (0)