|
31 | 31 | import io.objectbox.BoxStore;
|
32 | 32 | import io.objectbox.Cursor;
|
33 | 33 | import io.objectbox.InternalAccess;
|
| 34 | +import io.objectbox.annotation.apihint.Beta; |
34 | 35 | import io.objectbox.annotation.apihint.Experimental;
|
35 | 36 | import io.objectbox.annotation.apihint.Internal;
|
36 | 37 | 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; |
38 | 41 | import io.objectbox.relation.ListFactory.CopyOnWriteArrayListFactory;
|
39 | 42 |
|
40 | 43 | /**
|
@@ -75,10 +78,10 @@ public class ToMany<TARGET> implements List<TARGET>, Serializable {
|
75 | 78 | transient private Comparator<TARGET> comparator;
|
76 | 79 |
|
77 | 80 | public ToMany(Object sourceEntity, RelationInfo<TARGET> relationInfo) {
|
78 |
| - if(sourceEntity == null ) { |
| 81 | + if (sourceEntity == null) { |
79 | 82 | throw new IllegalArgumentException("No source entity given (null)");
|
80 | 83 | }
|
81 |
| - if(relationInfo == null) { |
| 84 | + if (relationInfo == null) { |
82 | 85 | throw new IllegalArgumentException("No relation info given (null)");
|
83 | 86 | }
|
84 | 87 | this.entity = sourceEntity;
|
@@ -169,7 +172,7 @@ private void ensureEntities() {
|
169 | 172 | newEntities = targetBox.internalGetBacklinkEntities(relationInfo.targetInfo.getEntityId(),
|
170 | 173 | relationInfo.targetIdProperty, id);
|
171 | 174 | }
|
172 |
| - if(comparator != null) { |
| 175 | + if (comparator != null) { |
173 | 176 | Collections.sort(newEntities, comparator);
|
174 | 177 | }
|
175 | 178 | synchronized (this) {
|
@@ -350,7 +353,7 @@ public synchronized boolean retainAll(Collection<?> objects) {
|
350 | 353 | changes = true;
|
351 | 354 | }
|
352 | 355 | }
|
353 |
| - if(toRemove != null) { |
| 356 | + if (toRemove != null) { |
354 | 357 | entities.removeAll(toRemove);
|
355 | 358 | }
|
356 | 359 | return changes;
|
@@ -439,13 +442,18 @@ public void sortById() {
|
439 | 442 | public int compare(TARGET o1, TARGET o2) {
|
440 | 443 | long id1 = idGetter.getId(o1);
|
441 | 444 | 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; |
444 | 449 | long delta = id1 - id2;
|
445 | 450 | // 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; |
449 | 457 | }
|
450 | 458 | });
|
451 | 459 | }
|
@@ -482,6 +490,45 @@ public void run() {
|
482 | 490 | }
|
483 | 491 | }
|
484 | 492 |
|
| 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 | + |
485 | 532 | /**
|
486 | 533 | * For internal use only; do not use in your app.
|
487 | 534 | * Called after relation source entity is put (so we have its ID).
|
|
0 commit comments