Skip to content

Commit 9e1f6f5

Browse files
committed
Box: add contains()
1 parent 3ca47e3 commit 9e1f6f5

File tree

2 files changed

+16
-0
lines changed
  • objectbox-java/src/main/java/io/objectbox
  • tests/objectbox-java-test/src/test/java/io/objectbox

2 files changed

+16
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,20 @@ public List<T> getAll() {
317317
}
318318
}
319319

320+
/**
321+
* Check if an object with the given ID exists in the database.
322+
* @since 2.7
323+
* @return true if a object with the given ID was found, false otherwise
324+
*/
325+
public boolean contains(long id) {
326+
Cursor<T> reader = getReader();
327+
try {
328+
return reader.seek(id);
329+
} finally {
330+
releaseReader(reader);
331+
}
332+
}
333+
320334
/**
321335
* Puts the given object in the box (aka persisting it). If this is a new entity (its ID property is 0), a new ID
322336
* will be assigned to the entity (and returned). If the entity was already put in the box before, it will be

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ public void testPutGetUpdateGetRemove() {
7979
assertEquals(value2, entityRead.getSimpleString());
8080

8181
// and remove it
82+
assertTrue(box.contains(id));
8283
assertTrue(box.remove(id));
8384
assertNull(box.get(id));
8485
assertFalse(box.remove(id));
86+
assertFalse(box.contains(id));
8587
}
8688

8789
@Test

0 commit comments

Comments
 (0)