Skip to content

Commit ffffd1e

Browse files
committed
ToMany: added indexOfId() and getById()
1 parent aaff4df commit ffffd1e

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.objectbox.reactive;
2+
3+
import java.util.List;
4+
5+
public class DataSubscriptionList implements DataSubscription {
6+
private boolean canceled;
7+
List<DataSubscription> subscriptions;
8+
9+
public synchronized void add(DataSubscription subscription) {
10+
subscriptions.add(subscription);
11+
}
12+
13+
@Override
14+
public synchronized void cancel() {
15+
canceled = true;
16+
for (DataSubscription subscription : subscriptions) {
17+
subscription.cancel();
18+
}
19+
subscriptions.clear();
20+
}
21+
22+
@Override
23+
public synchronized boolean isCanceled() {
24+
return canceled;
25+
}
26+
27+
public synchronized int getActiveSubscriptionCount() {
28+
return subscriptions.size();
29+
}
30+
}

objectbox-java/src/main/java/io/objectbox/relation/ToMany.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,38 @@ public boolean hasAll(QueryFilter<TARGET> filter) {
529529
return true;
530530
}
531531

532+
@Beta
533+
/** Gets an object by its entity ID. */
534+
public TARGET getById(long id) {
535+
ensureEntities();
536+
Object[] objects = entities.toArray();
537+
IdGetter<TARGET> idGetter = relationInfo.targetInfo.getIdGetter();
538+
for (Object target : objects) {
539+
TARGET candidate = (TARGET) target;
540+
if (idGetter.getId(candidate) == id) {
541+
return candidate;
542+
}
543+
}
544+
return null;
545+
}
546+
547+
@Beta
548+
/** Gets the index of the object with the given entity ID. */
549+
public int indexOfId(long id) {
550+
ensureEntities();
551+
Object[] objects = entities.toArray();
552+
IdGetter<TARGET> idGetter = relationInfo.targetInfo.getIdGetter();
553+
int index = 0;
554+
for (Object target : objects) {
555+
TARGET candidate = (TARGET) target;
556+
if (idGetter.getId(candidate) == id) {
557+
return index;
558+
}
559+
index++;
560+
}
561+
return -1;
562+
}
563+
532564
/**
533565
* For internal use only; do not use in your app.
534566
* Called after relation source entity is put (so we have its ID).

tests/objectbox-java-test/src/main/java/io/objectbox/relation/ToManyTest.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
import io.objectbox.TestUtils;
2727
import io.objectbox.query.QueryFilter;
2828

29-
30-
import static org.junit.Assert.assertEquals;
31-
import static org.junit.Assert.assertFalse;
32-
import static org.junit.Assert.assertTrue;
29+
import static org.junit.Assert.*;
3330

3431
public class ToManyTest extends AbstractRelationTest {
3532

@@ -315,6 +312,7 @@ public void testSortById() {
315312
assertEquals("new1", toMany.get(3).getText());
316313
assertEquals("new2", toMany.get(4).getText());
317314
}
315+
318316
@Test
319317
public void testHasA() {
320318
Customer customer = putCustomerWithOrders(3);
@@ -341,12 +339,32 @@ public boolean keep(Order entity) {
341339
}
342340
};
343341
assertTrue(toMany.hasAll(filter));
344-
toMany.get(0).text="nope";
342+
toMany.get(0).text = "nope";
345343
assertFalse(toMany.hasAll(filter));
346344
toMany.clear();
347345
assertFalse(toMany.hasAll(filter));
348346
}
349347

348+
@Test
349+
public void testIndexOfId() {
350+
Customer customer = putCustomerWithOrders(3);
351+
ToMany<Order> toMany = (ToMany<Order>) customer.orders;
352+
assertEquals(1, toMany.indexOfId(toMany.get(1).getId()));
353+
assertEquals(2, toMany.indexOfId(toMany.get(2).getId()));
354+
assertEquals(0, toMany.indexOfId(toMany.get(0).getId()));
355+
assertEquals(-1, toMany.indexOfId(42));
356+
}
357+
358+
@Test
359+
public void testGetById() {
360+
Customer customer = putCustomerWithOrders(3);
361+
ToMany<Order> toMany = (ToMany<Order>) customer.orders;
362+
assertEquals(toMany.get(1), toMany.getById(toMany.get(1).getId()));
363+
assertEquals(toMany.get(2), toMany.getById(toMany.get(2).getId()));
364+
assertEquals(toMany.get(0), toMany.getById(toMany.get(0).getId()));
365+
assertNull(toMany.getById(42));
366+
}
367+
350368
@Test
351369
public void testSerializable() throws IOException, ClassNotFoundException {
352370
Customer customer = new Customer();

0 commit comments

Comments
 (0)