Class ToMany<TARGET>
- java.lang.Object
-
- io.objectbox.relation.ToMany<TARGET>
-
- Type Parameters:
TARGET- target object type (@Entityclass).
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<TARGET>,java.util.Collection<TARGET>,java.util.List<TARGET>
public class ToMany<TARGET> extends java.lang.Object implements java.util.List<TARGET>, java.io.SerializableA to-many relation of an entity that references multiple objects of aToManyentity.Example:
{@code // Java- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private BoxStoreboxStoreprivate java.util.Comparator<TARGET>comparatorprivate java.util.List<TARGET>entitiesprivate java.util.Map<TARGET,java.lang.Boolean>entitiesAddedEntities added since last put/sync.private java.util.Map<TARGET,java.lang.Boolean>entitiesRemovedEntities removed since last put/sync.(package private) java.util.List<TARGET>entitiesToPut(package private) java.util.List<TARGET>entitiesToRemoveFromDbprivate java.lang.Objectentityprivate Box<java.lang.Object>entityBoxprivate java.util.Map<TARGET,java.lang.Integer>entityCountsCounts of all entities in the list (entities).private ListFactorylistFactoryprivate static java.lang.IntegerONEprivate RelationInfo<java.lang.Object,TARGET>relationInfoprivate booleanremoveFromTargetBoxprivate static longserialVersionUIDprivate Box<TARGET>targetBox
-
Constructor Summary
Constructors Constructor Description ToMany(java.lang.Object sourceEntity, RelationInfo<?,TARGET> relationInfo)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int location, TARGET object)Seeadd(Object)for general comments.booleanadd(TARGET object)Prepares to add the given target object to this relation.booleanaddAll(int index, java.util.Collection<? extends TARGET> objects)Seeadd(Object)for general comments.booleanaddAll(java.util.Collection<? extends TARGET> objects)Seeadd(Object)for general comments.private voidaddStandaloneRelations(Cursor<?> cursor, long sourceEntityId, TARGET[] added, IdGetter<TARGET> targetIdGetter)The target array may not contain non-persisted entities.voidapplyChangesToDb()Saves changes (added and removed objects) made to this relation to the database.voidclear()booleancontains(java.lang.Object object)booleancontainsAll(java.util.Collection<?> collection)private voidensureBoxes()private voidensureEntities()private voidensureEntitiesWithTrackingLists()TARGETget(int location)Gets the target object at the given index.intgetAddCount()TARGETgetById(long id)Gets an object by its ID.(package private) java.lang.ObjectgetEntity()For testsListFactorygetListFactory()intgetRemoveCount()booleanhasA(QueryFilter<TARGET> filter)Returns true if at least one of the target objects matches the given filter.booleanhasAll(QueryFilter<TARGET> filter)Returns true if all of the target objects match the given filter.booleanhasPendingDbChanges()Returns true if there are pending changes for the DB.intindexOf(java.lang.Object object)intindexOfId(long id)Gets the index of the object with the given ID.voidinternalApplyToDb(Cursor<?> sourceCursor, Cursor<TARGET> targetCursor)For internal use only; do not use in your app.booleaninternalCheckApplyToDbRequired()For internal use only; do not use in your app.booleanisEmpty()booleanisResolved()java.util.Iterator<TARGET>iterator()intlastIndexOf(java.lang.Object object)java.util.ListIterator<TARGET>listIterator()java.util.ListIterator<TARGET>listIterator(int location)The returned iterator does not track any potential calls toIterator.remove().private booleanprepareToManyBacklinkEntitiesForDb(long entityId, IdGetter<TARGET> idGetter, java.util.Map<TARGET,java.lang.Boolean> setAdded, java.util.Map<TARGET,java.lang.Boolean> setRemoved)Modifies thelinkedToMany relation of added or removed target objects and schedules put byinternalApplyToDb(io.objectbox.Cursor<?>, io.objectbox.Cursor<TARGET>)for them.private booleanprepareToOneBacklinkEntitiesForDb(long entityId, IdGetter<TARGET> idGetter, java.util.Map<TARGET,java.lang.Boolean> setAdded, java.util.Map<TARGET,java.lang.Boolean> setRemoved)TARGETremove(int location)Likeremove(Object), but using the location of the target object.booleanremove(java.lang.Object object)Prepares to remove the target object from this relation.booleanremoveAll(java.util.Collection<?> objects)TARGETremoveById(long id)Likeremove(Object), but using just the ID of the target object.private voidremoveStandaloneRelations(Cursor<?> cursor, long sourceEntityId, java.util.List<TARGET> removed, IdGetter<TARGET> targetIdGetter)The list of removed entities may contain non-persisted entities, which will be ignored (removed from the list).voidreset()Resets the already loaded (cached) objects of this list, so they will be re-loaded when accessing this list again.booleanretainAll(java.util.Collection<?> objects)TARGETset(int location, TARGET object)voidsetComparator(java.util.Comparator<TARGET> comparator)Set an comparator to define the order of entities.voidsetListFactory(ListFactory listFactory)Currently only used for non-persisted entities (id == 0).voidsetRemoveFromTargetBox(boolean removeFromTargetBox)On put, this also deletes removed entities from the target Box.intsize()voidsortById()Sorts the list by the "natural" ObjectBox order for to-many list (by object ID).java.util.List<TARGET>subList(int start, int end)The returned sub list does not do any change tracking.java.lang.Object[]toArray()<T> T[]toArray(T[] array)private voidtrackAdd(java.util.Collection<? extends TARGET> objects)Must be called from a synchronized methodprivate voidtrackAdd(TARGET object)Must be called from a synchronized methodprivate voidtrackRemove(TARGET object)Must be called from a synchronized method
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
ONE
private static final java.lang.Integer ONE
-
entity
private final java.lang.Object entity
-
relationInfo
private final RelationInfo<java.lang.Object,TARGET> relationInfo
-
listFactory
private volatile ListFactory listFactory
-
entities
private java.util.List<TARGET> entities
-
entityCounts
private java.util.Map<TARGET,java.lang.Integer> entityCounts
Counts of all entities in the list (entities).
-
entitiesAdded
private volatile java.util.Map<TARGET,java.lang.Boolean> entitiesAdded
Entities added since last put/sync. Map is used as a set (value is always Boolean.TRUE).
-
entitiesRemoved
private java.util.Map<TARGET,java.lang.Boolean> entitiesRemoved
Entities removed since last put/sync. Map is used as a set (value is always Boolean.TRUE).
-
entitiesToPut
java.util.List<TARGET> entitiesToPut
-
entitiesToRemoveFromDb
java.util.List<TARGET> entitiesToRemoveFromDb
-
boxStore
private transient BoxStore boxStore
-
entityBox
private transient Box<java.lang.Object> entityBox
-
removeFromTargetBox
private transient boolean removeFromTargetBox
-
comparator
private transient java.util.Comparator<TARGET> comparator
-
-
Constructor Detail
-
ToMany
public ToMany(java.lang.Object sourceEntity, RelationInfo<?,TARGET> relationInfo)
-
-
Method Detail
-
setListFactory
@Experimental public void setListFactory(ListFactory listFactory)
Currently only used for non-persisted entities (id == 0).
-
setComparator
@Experimental public void setComparator(java.util.Comparator<TARGET> comparator)
Set an comparator to define the order of entities.
-
setRemoveFromTargetBox
@Experimental public void setRemoveFromTargetBox(boolean removeFromTargetBox)
On put, this also deletes removed entities from the target Box. Note: removed target entities won't cascade the delete.
-
getListFactory
public ListFactory getListFactory()
-
ensureBoxes
private void ensureBoxes()
-
ensureEntitiesWithTrackingLists
private void ensureEntitiesWithTrackingLists()
-
ensureEntities
private void ensureEntities()
-
add
public boolean add(TARGET object)
Prepares to add the given target object to this relation.To apply changes, call
applyChangesToDb()or put the object with the ToMany. For important details, see the notes about relations ofBox.put(Object).
-
trackAdd
private void trackAdd(TARGET object)
Must be called from a synchronized method
-
trackAdd
private void trackAdd(java.util.Collection<? extends TARGET> objects)
Must be called from a synchronized method
-
trackRemove
private void trackRemove(TARGET object)
Must be called from a synchronized method
-
add
public void add(int location, TARGET object)Seeadd(Object)for general comments.- Specified by:
addin interfacejava.util.List<TARGET>
-
addAll
public boolean addAll(java.util.Collection<? extends TARGET> objects)
Seeadd(Object)for general comments.
-
addAll
public boolean addAll(int index, java.util.Collection<? extends TARGET> objects)Seeadd(Object)for general comments.- Specified by:
addAllin interfacejava.util.List<TARGET>
-
clear
public void clear()
-
contains
public boolean contains(java.lang.Object object)
-
containsAll
public boolean containsAll(java.util.Collection<?> collection)
-
get
public TARGET get(int location)
Gets the target object at the given index.ToManyuses lazy initialization, so on first access this will read the target objects from the database.- Specified by:
getin interfacejava.util.List<TARGET>
-
indexOf
public int indexOf(java.lang.Object object)
- Specified by:
indexOfin interfacejava.util.List<TARGET>
-
isEmpty
public boolean isEmpty()
-
iterator
@Nonnull public java.util.Iterator<TARGET> iterator()
-
lastIndexOf
public int lastIndexOf(java.lang.Object object)
- Specified by:
lastIndexOfin interfacejava.util.List<TARGET>
-
listIterator
@Nonnull public java.util.ListIterator<TARGET> listIterator()
- Specified by:
listIteratorin interfacejava.util.List<TARGET>
-
listIterator
@Nonnull public java.util.ListIterator<TARGET> listIterator(int location)
The returned iterator does not track any potential calls toIterator.remove(). Thus these removes will NOT be synced to the target Box.- Specified by:
listIteratorin interfacejava.util.List<TARGET>
-
remove
public TARGET remove(int location)
Likeremove(Object), but using the location of the target object.- Specified by:
removein interfacejava.util.List<TARGET>
-
remove
public boolean remove(java.lang.Object object)
Prepares to remove the target object from this relation.To apply changes, call
applyChangesToDb()or put the object with the ToMany. For important details, see the notes about relations ofBox.put(Object).
-
removeById
public TARGET removeById(long id)
Likeremove(Object), but using just the ID of the target object.
-
removeAll
public boolean removeAll(java.util.Collection<?> objects)
-
retainAll
public boolean retainAll(java.util.Collection<?> objects)
-
set
public TARGET set(int location, TARGET object)
- Specified by:
setin interfacejava.util.List<TARGET>
-
size
public int size()
-
subList
@Nonnull public java.util.List<TARGET> subList(int start, int end)
The returned sub list does not do any change tracking. Thus any modifications to the sublist won't be synced to the target Box.- Specified by:
subListin interfacejava.util.List<TARGET>
-
toArray
@Nonnull public java.lang.Object[] toArray()
-
toArray
@Nonnull public <T> T[] toArray(T[] array)
-
reset
public void reset()
Resets the already loaded (cached) objects of this list, so they will be re-loaded when accessing this list again.Use this to sync with changes to this relation or target objects made outside of this ToMany.
-
isResolved
public boolean isResolved()
-
getAddCount
public int getAddCount()
-
getRemoveCount
public int getRemoveCount()
-
sortById
public void sortById()
Sorts the list by the "natural" ObjectBox order for to-many list (by object ID). This will be the order when you get the objects fresh (e.g. initially or after callingreset()). Note that non persisted objects (ID is zero) will be put to the end as they are still to get an ID.
-
applyChangesToDb
public void applyChangesToDb()
Saves changes (added and removed objects) made to this relation to the database. For some important details, see the notes about relations ofBox.put(Object).Note that this is called already when the object that contains this ToMany is put. However, if only this ToMany has changed, it is more efficient to just use this method.
- Throws:
java.lang.IllegalStateException- If the object that contains this ToMany has no ID assigned (it must have been put before).
-
hasA
@Beta public boolean hasA(QueryFilter<TARGET> filter)
Returns true if at least one of the target objects matches the given filter.For use with
QueryBuilder.filter(QueryFilter)inside aQueryFilterto check to-many relation objects.
-
hasAll
@Beta public boolean hasAll(QueryFilter<TARGET> filter)
Returns true if all of the target objects match the given filter. Returns false if the list is empty.For use with
QueryBuilder.filter(QueryFilter)inside aQueryFilterto check to-many relation objects.
-
indexOfId
@Beta public int indexOfId(long id)
Gets the index of the object with the given ID.
-
hasPendingDbChanges
public boolean hasPendingDbChanges()
Returns true if there are pending changes for the DB. Changes will be automatically persisted once the object with the ToMany is put, or an explicit call toapplyChangesToDb()is made.
-
internalCheckApplyToDbRequired
@Internal public boolean internalCheckApplyToDbRequired()
For internal use only; do not use in your app. Called after relation source object is put (so we have its ID). Prepares data forinternalApplyToDb(Cursor, Cursor)
-
prepareToManyBacklinkEntitiesForDb
private boolean prepareToManyBacklinkEntitiesForDb(long entityId, IdGetter<TARGET> idGetter, @Nullable java.util.Map<TARGET,java.lang.Boolean> setAdded, @Nullable java.util.Map<TARGET,java.lang.Boolean> setRemoved)Modifies thelinkedToMany relation of added or removed target objects and schedules put byinternalApplyToDb(io.objectbox.Cursor<?>, io.objectbox.Cursor<TARGET>)for them.If
setRemoveFromTargetBox(boolean)is true, removed target objects are scheduled for removal instead of just updating their ToMany relation.If target objects are new, schedules a put if they were added, but never if they were removed from this relation.
- Returns:
- Whether there are any target objects to put or remove.
-
prepareToOneBacklinkEntitiesForDb
private boolean prepareToOneBacklinkEntitiesForDb(long entityId, IdGetter<TARGET> idGetter, @Nullable java.util.Map<TARGET,java.lang.Boolean> setAdded, @Nullable java.util.Map<TARGET,java.lang.Boolean> setRemoved)
-
internalApplyToDb
@Internal public void internalApplyToDb(Cursor<?> sourceCursor, Cursor<TARGET> targetCursor)
For internal use only; do not use in your app. Convention:internalCheckApplyToDbRequired()must be called before this call as it prepares .
-
removeStandaloneRelations
private void removeStandaloneRelations(Cursor<?> cursor, long sourceEntityId, java.util.List<TARGET> removed, IdGetter<TARGET> targetIdGetter)
The list of removed entities may contain non-persisted entities, which will be ignored (removed from the list).
-
addStandaloneRelations
private void addStandaloneRelations(Cursor<?> cursor, long sourceEntityId, TARGET[] added, IdGetter<TARGET> targetIdGetter)
The target array may not contain non-persisted entities.
-
getEntity
java.lang.Object getEntity()
For tests
-
-