Skip to content

Commit f285246

Browse files
committed
added hasA and hasAll to ToMany
1 parent a2a671c commit f285246

File tree

2 files changed

+88
-10
lines changed
  • objectbox-java/src/main/java/io/objectbox/relation
  • tests/objectbox-java-test/src/main/java/io/objectbox/relation

2 files changed

+88
-10
lines changed

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

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
import io.objectbox.BoxStore;
3232
import io.objectbox.Cursor;
3333
import io.objectbox.InternalAccess;
34+
import io.objectbox.annotation.apihint.Beta;
3435
import io.objectbox.annotation.apihint.Experimental;
3536
import io.objectbox.annotation.apihint.Internal;
3637
import io.objectbox.exception.DbDetachedException;
37-
import io.objectbox.internal.*;
38+
import io.objectbox.internal.IdGetter;
39+
import io.objectbox.internal.ReflectionCache;
40+
import io.objectbox.query.QueryFilter;
3841
import io.objectbox.relation.ListFactory.CopyOnWriteArrayListFactory;
3942

4043
/**
@@ -75,10 +78,10 @@ public class ToMany<TARGET> implements List<TARGET>, Serializable {
7578
transient private Comparator<TARGET> comparator;
7679

7780
public ToMany(Object sourceEntity, RelationInfo<TARGET> relationInfo) {
78-
if(sourceEntity == null ) {
81+
if (sourceEntity == null) {
7982
throw new IllegalArgumentException("No source entity given (null)");
8083
}
81-
if(relationInfo == null) {
84+
if (relationInfo == null) {
8285
throw new IllegalArgumentException("No relation info given (null)");
8386
}
8487
this.entity = sourceEntity;
@@ -169,7 +172,7 @@ private void ensureEntities() {
169172
newEntities = targetBox.internalGetBacklinkEntities(relationInfo.targetInfo.getEntityId(),
170173
relationInfo.targetIdProperty, id);
171174
}
172-
if(comparator != null) {
175+
if (comparator != null) {
173176
Collections.sort(newEntities, comparator);
174177
}
175178
synchronized (this) {
@@ -350,7 +353,7 @@ public synchronized boolean retainAll(Collection<?> objects) {
350353
changes = true;
351354
}
352355
}
353-
if(toRemove != null) {
356+
if (toRemove != null) {
354357
entities.removeAll(toRemove);
355358
}
356359
return changes;
@@ -439,13 +442,18 @@ public void sortById() {
439442
public int compare(TARGET o1, TARGET o2) {
440443
long id1 = idGetter.getId(o1);
441444
long id2 = idGetter.getId(o2);
442-
if (id1 == 0) id1 = Long.MAX_VALUE;
443-
if (id2 == 0) id2 = Long.MAX_VALUE;
445+
if (id1 == 0)
446+
id1 = Long.MAX_VALUE;
447+
if (id2 == 0)
448+
id2 = Long.MAX_VALUE;
444449
long delta = id1 - id2;
445450
// because of long we cannot simply return delta
446-
if (delta < 0) return -1;
447-
else if (delta > 0) return 1;
448-
else return 0;
451+
if (delta < 0)
452+
return -1;
453+
else if (delta > 0)
454+
return 1;
455+
else
456+
return 0;
449457
}
450458
});
451459
}
@@ -482,6 +490,45 @@ public void run() {
482490
}
483491
}
484492

493+
/**
494+
* Returns true if at least one of the entities matches the given filter.
495+
* <p>
496+
* For use with {@link io.objectbox.query.QueryBuilder#filter(QueryFilter)} inside a {@link QueryFilter} to check
497+
* to-many relation entities.
498+
*/
499+
@Beta
500+
public boolean hasA(QueryFilter<TARGET> filter) {
501+
ensureEntities();
502+
Object[] objects = entities.toArray();
503+
for (Object target : objects) {
504+
if (filter.keep((TARGET) target)) {
505+
return true;
506+
}
507+
}
508+
return false;
509+
}
510+
511+
/**
512+
* Returns true if all of the entities match the given filter. Returns false if the list is empty.
513+
* <p>
514+
* For use with {@link io.objectbox.query.QueryBuilder#filter(QueryFilter)} inside a {@link QueryFilter} to check
515+
* to-many relation entities.
516+
*/
517+
@Beta
518+
public boolean hasAll(QueryFilter<TARGET> filter) {
519+
ensureEntities();
520+
Object[] objects = entities.toArray();
521+
if(objects.length == 0) {
522+
return false;
523+
}
524+
for (Object target : objects) {
525+
if (!filter.keep((TARGET) target)) {
526+
return false;
527+
}
528+
}
529+
return true;
530+
}
531+
485532
/**
486533
* For internal use only; do not use in your app.
487534
* 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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525

2626
import io.objectbox.TestUtils;
27+
import io.objectbox.query.QueryFilter;
2728

2829

2930
import static org.junit.Assert.assertEquals;
@@ -314,7 +315,37 @@ public void testSortById() {
314315
assertEquals("new1", toMany.get(3).getText());
315316
assertEquals("new2", toMany.get(4).getText());
316317
}
318+
@Test
319+
public void testHasA() {
320+
Customer customer = putCustomerWithOrders(3);
321+
ToMany<Order> toMany = (ToMany<Order>) customer.orders;
322+
QueryFilter<Order> filter = new QueryFilter<Order>() {
323+
@Override
324+
public boolean keep(Order entity) {
325+
return "order2".equals(entity.text);
326+
}
327+
};
328+
assertTrue(toMany.hasA(filter));
329+
toMany.remove(1);
330+
assertFalse(toMany.hasA(filter));
331+
}
317332

333+
@Test
334+
public void testHasAll() {
335+
Customer customer = putCustomerWithOrders(3);
336+
ToMany<Order> toMany = (ToMany<Order>) customer.orders;
337+
QueryFilter<Order> filter = new QueryFilter<Order>() {
338+
@Override
339+
public boolean keep(Order entity) {
340+
return entity.text.startsWith("order");
341+
}
342+
};
343+
assertTrue(toMany.hasAll(filter));
344+
toMany.get(0).text="nope";
345+
assertFalse(toMany.hasAll(filter));
346+
toMany.clear();
347+
assertFalse(toMany.hasAll(filter));
348+
}
318349

319350
@Test
320351
public void testSerializable() throws IOException, ClassNotFoundException {

0 commit comments

Comments
 (0)