Skip to content

Commit 1709164

Browse files
committed
Box.remove() returns success flag
1 parent a7463cf commit 1709164

3 files changed

Lines changed: 22 additions & 14 deletions

File tree

objectbox-java/src/main/java/io/objectbox/Box.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,18 @@ public void put(@Nullable Collection<T> entities) {
387387

388388
/**
389389
* Removes (deletes) the Object by its ID.
390+
* @return true if an entity was actually removed (false if no entity exists with the given ID)
390391
*/
391-
public void remove(long id) {
392+
public boolean remove(long id) {
392393
Cursor<T> cursor = getWriter();
394+
boolean removed;
393395
try {
394-
cursor.deleteEntity(id);
396+
removed = cursor.deleteEntity(id);
395397
commitWriter(cursor);
396398
} finally {
397399
releaseWriter(cursor);
398400
}
401+
return removed;
399402
}
400403

401404
/**
@@ -437,16 +440,19 @@ public void removeByKeys(@Nullable Collection<Long> ids) {
437440

438441
/**
439442
* Removes (deletes) the given Object.
443+
* @return true if an entity was actually removed (false if no entity exists with the given ID)
440444
*/
441-
public void remove(T object) {
445+
public boolean remove(T object) {
442446
Cursor<T> cursor = getWriter();
447+
boolean removed;
443448
try {
444-
long key = cursor.getId(object);
445-
cursor.deleteEntity(key);
449+
long id = cursor.getId(object);
450+
removed = cursor.deleteEntity(id);
446451
commitWriter(cursor);
447452
} finally {
448453
releaseWriter(cursor);
449454
}
455+
return removed;
450456
}
451457

452458
/**

objectbox-java/src/main/java/io/objectbox/Cursor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public abstract class Cursor<T> implements Closeable {
4444

4545
static native void nativeDestroy(long cursor);
4646

47-
static native void nativeDeleteEntity(long cursor, long key);
47+
static native boolean nativeDeleteEntity(long cursor, long key);
4848

4949
static native void nativeDeleteAll(long cursor);
5050

@@ -195,8 +195,8 @@ public List<T> getAll() {
195195
return (List) nativeGetAllEntities(cursor);
196196
}
197197

198-
public void deleteEntity(long key) {
199-
nativeDeleteEntity(cursor, key);
198+
public boolean deleteEntity(long key) {
199+
return nativeDeleteEntity(cursor, key);
200200
}
201201

202202
public void deleteAll() {

tests/objectbox-java-test/src/test/java/io/objectbox/BoxTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public void testPutGetUpdateGetRemove() {
5656
entity.setSimpleLong(54321);
5757
String value1 = "lulu321";
5858
entity.setSimpleString(value1);
59-
long key = box.put(entity);
59+
long id = box.put(entity);
6060

6161
// get it
62-
TestEntity entityRead = box.get(key);
62+
TestEntity entityRead = box.get(id);
6363
assertNotNull(entityRead);
6464
assertEquals(1977, entityRead.getSimpleInt());
6565
assertEquals(54321, entityRead.getSimpleLong());
@@ -72,15 +72,16 @@ public void testPutGetUpdateGetRemove() {
7272
box.put(entityRead);
7373

7474
// get the changed entity
75-
entityRead = box.get(key);
75+
entityRead = box.get(id);
7676
assertNotNull(entityRead);
7777
assertEquals(1977, entityRead.getSimpleInt());
7878
assertEquals(12345, entityRead.getSimpleLong());
7979
assertEquals(value2, entityRead.getSimpleString());
8080

8181
// and remove it
82-
box.remove(key);
83-
assertNull(box.get(key));
82+
assertTrue(box.remove(id));
83+
assertNull(box.get(id));
84+
assertFalse(box.remove(id));
8485
}
8586

8687
@Test
@@ -113,7 +114,8 @@ public void testRemoveMany() {
113114
box.put(entities);
114115
assertEquals(entities.size(), box.count());
115116

116-
box.remove(entities.get(1));
117+
assertTrue(box.remove(entities.get(1)));
118+
assertFalse(box.remove(entities.get(1)));
117119
assertEquals(entities.size() - 1, box.count());
118120
box.remove(entities.get(4), entities.get(5));
119121
assertEquals(entities.size() - 3, box.count());

0 commit comments

Comments
 (0)