Package io.realm
Class OrderedRealmCollectionSnapshot<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- io.realm.OrderedRealmCollectionSnapshot<E>
-
- All Implemented Interfaces:
io.realm.internal.Freezable<RealmCollection<E>>,io.realm.internal.ManageableObject,OrderedRealmCollection<E>,RealmCollection<E>,Iterable<E>,Collection<E>,List<E>
public class OrderedRealmCollectionSnapshot<E> extends AbstractList<E>
AnOrderedRealmCollectionSnapshotis a special type ofOrderedRealmCollection. It can be created by callingOrderedRealmCollection.createSnapshot(). UnlikeRealmResultsandRealmList, its size and order of elements will never be changed after creation.OrderedRealmCollectionSnapshotis useful when making changes which may impact the size or order of the collection in simple loops. For example:final RealmResults<Dog> dogs = realm.where(Dog.class).findAll(); final OrderedRealmCollectionSnapshot<Dog> snapshot = dogs.createSnapshot(); final int dogsCount = snapshot.size(); // dogs.size() == snapshot.size() == 10 realm.executeTransaction(new Realm.Transaction() { /@Override public void execute(Realm realm) { for (int i = 0; i < dogsCount; i++) { // This won't work since RealmResults is always up-to-date, its size gets decreased by 1 after every loop. An // IndexOutOfBoundsException will be thrown after 5 loops. // dogs.deleteFromRealm(i); snapshot.deleteFromRealm(i); // Deletion on OrderedRealmCollectionSnapshot won't change the size of it. } } });
-
-
Field Summary
Fields Modifier and Type Field Description io.realm.BaseRealmbaseRealmTheBaseRealminstance in which this collection resides.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidadd(int index, E element)Deprecated.booleanadd(E element)Deprecated.booleanaddAll(int location, Collection<? extends E> collection)Deprecated.booleanaddAll(Collection<? extends E> collection)Deprecated.doubleaverage(String fieldName)Returns the average of a given field.voidclear()Deprecated.booleancontains(Object object)Searches thisOrderedRealmCollectionfor the specified object.OrderedRealmCollectionSnapshot<E>createSnapshot()Creates a snapshot from thisOrderedRealmCollection.booleandeleteAllFromRealm()This deletes all objects in the collection from the underlying Realm.booleandeleteFirstFromRealm()Deletes the first object from the Realm.voiddeleteFromRealm(int location)Deletes the object at the given index from the Realm.booleandeleteLastFromRealm()Deletes the last object from the Realm.Efirst()Gets the first object from the collection.Efirst(E defaultValue)Gets the first object from the collection.OrderedRealmCollection<E>freeze()Eget(int location)Returns the element at the specified location in this list.RealmgetRealm()Returns theRealminstance to which this collection belongs.booleanisFrozen()booleanisLoaded()Checks if a collection has finished loading its data yet.booleanisManaged()ARealmResultsor aOrderedRealmCollectionSnapshotis always a managed collection.booleanisValid()Checks if the collection is still valid to use, i.e., theRealminstance hasn't been closed.Iterator<E>iterator()Returns an iterator for the results of a query.Elast()Gets the last object from the collection.Elast(E defaultValue)Gets the last object from the collection.ListIterator<E>listIterator()Returns a list iterator for the results of a query.ListIterator<E>listIterator(int location)Returns a list iterator on the results of a query.booleanload()Blocks the collection until all data are available.Numbermax(String fieldName)Finds the maximum value of a field.DatemaxDate(String fieldName)Finds the maximum date.Numbermin(String fieldName)Finds the minimum value of a field.DateminDate(String fieldName)Finds the minimum date.Eremove(int index)Deprecated.booleanremove(Object object)Deprecated.booleanremoveAll(Collection<?> collection)Deprecated.booleanretainAll(Collection<?> collection)Deprecated.Eset(int location, E object)Deprecated.intsize()Returns the number of elements in this query result.RealmResults<E>sort(String fieldName)Not supported byOrderedRealmCollectionSnapshot.RealmResults<E>sort(String[] fieldNames, Sort[] sortOrders)Not supported byOrderedRealmCollectionSnapshot.RealmResults<E>sort(String fieldName, Sort sortOrder)Not supported byOrderedRealmCollectionSnapshot.RealmResults<E>sort(String fieldName1, Sort sortOrder1, String fieldName2, Sort sortOrder2)Not supported byOrderedRealmCollectionSnapshot.Numbersum(String fieldName)Calculates the sum of a given field.RealmQuery<E>where()Deprecated.-
Methods inherited from class java.util.AbstractList
equals, hashCode, indexOf, lastIndexOf, subList
-
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, toArray, toArray, toString
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.List
containsAll, equals, hashCode, indexOf, isEmpty, lastIndexOf, replaceAll, sort, spliterator, subList, toArray, toArray
-
-
-
-
Method Detail
-
size
public int size()
Returns the number of elements in this query result.
-
sort
public RealmResults<E> sort(String fieldName)
Not supported byOrderedRealmCollectionSnapshot. Use 'sort()' on the originalOrderedRealmCollectioninstead.- Specified by:
sortin interfaceOrderedRealmCollection<E>- Parameters:
fieldName- the field name to sort by. Only fields of type boolean, short, int, long, float, double, Date, and String are supported.- Returns:
- a new sorted
RealmResultswill be created and returned. The original collection stays unchanged. - Throws:
UnsupportedOperationException
-
sort
public RealmResults<E> sort(String fieldName, Sort sortOrder)
Not supported byOrderedRealmCollectionSnapshot. Use 'sort()' on the originalOrderedRealmCollectioninstead.- Specified by:
sortin interfaceOrderedRealmCollection<E>- Parameters:
fieldName- the field name to sort by. Only fields of type boolean, short, int, long, float, double, Date, and String are supported.sortOrder- the direction to sort by.- Returns:
- a new sorted
RealmResultswill be created and returned. The original collection stays unchanged. - Throws:
UnsupportedOperationException
-
sort
public RealmResults<E> sort(String fieldName1, Sort sortOrder1, String fieldName2, Sort sortOrder2)
Not supported byOrderedRealmCollectionSnapshot. Use 'sort()' on the originalOrderedRealmCollectioninstead.- Specified by:
sortin interfaceOrderedRealmCollection<E>- Parameters:
fieldName1- first field name. Only fields of type boolean, short, int, long, float, double, Date, and String are supported.sortOrder1- sort order for first field.fieldName2- second field name. Only fields of type boolean, short, int, long, float, double, Date, and String are supported.sortOrder2- sort order for second field.- Returns:
- a new sorted
RealmResultswill be created and returned. The original collection stays unchanged. - Throws:
UnsupportedOperationException
-
sort
public RealmResults<E> sort(String[] fieldNames, Sort[] sortOrders)
Not supported byOrderedRealmCollectionSnapshot. Use 'sort()' on the originalOrderedRealmCollectioninstead.- Specified by:
sortin interfaceOrderedRealmCollection<E>- Parameters:
fieldNames- an array of field names to sort by. Only fields of type boolean, short, int, long, float, double, Date, and String are supported.sortOrders- the directions to sort by.- Returns:
- a new sorted
RealmResultswill be created and returned. The original collection stays unchanged. - Throws:
UnsupportedOperationException
-
where
@Deprecated public RealmQuery<E> where()
Deprecated.Not supported byOrderedRealmCollectionSnapshot. Use 'where()' on the originalOrderedRealmCollectioninstead.- Returns:
- a RealmQuery object.
- Throws:
UnsupportedOperationException- See Also:
RealmQuery
-
isLoaded
public boolean isLoaded()
Checks if a collection has finished loading its data yet.- Returns:
trueif data has been loaded and is available,falseif data is still being loaded.
-
load
public boolean load()
Blocks the collection until all data are available.- Returns:
trueif the data could be successfully loaded,falseotherwise.
-
createSnapshot
public OrderedRealmCollectionSnapshot<E> createSnapshot()
Creates a snapshot from thisOrderedRealmCollection.- Specified by:
createSnapshotin interfaceOrderedRealmCollection<E>- Returns:
- the snapshot of this collection.
- See Also:
OrderedRealmCollectionSnapshot
-
freeze
public OrderedRealmCollection<E> freeze()
-
deleteFromRealm
public void deleteFromRealm(int location)
Deletes the object at the given index from the Realm. The object at the given index will become invalid. Just returns if the object is invalid already.- Specified by:
deleteFromRealmin interfaceOrderedRealmCollection<E>- Parameters:
location- the array index identifying the object to be removed.- Throws:
IndexOutOfBoundsException- iflocation < 0 || location >= size().IllegalStateException- if the Realm is closed or the method is called from the wrong thread.
-
deleteFirstFromRealm
public boolean deleteFirstFromRealm()
Deletes the first object from the Realm. The first object will become invalid.- Specified by:
deleteFirstFromRealmin interfaceOrderedRealmCollection<E>- Returns:
trueif an object was deleted,falseotherwise.- Throws:
IllegalStateException- if the Realm is closed or the method is called on the wrong thread.
-
deleteLastFromRealm
public boolean deleteLastFromRealm()
Deletes the last object from the Realm. The last object will become invalid.- Specified by:
deleteLastFromRealmin interfaceOrderedRealmCollection<E>- Returns:
trueif an object was deleted,falseotherwise.- Throws:
IllegalStateException- if the Realm is closed or the method is called from the wrong thread.
-
deleteAllFromRealm
public boolean deleteAllFromRealm()
This deletes all objects in the collection from the underlying Realm. All objects in the collection snapshot will become invalid.- Specified by:
deleteAllFromRealmin interfaceRealmCollection<E>- Returns:
trueif objects was deleted,falseotherwise.- Throws:
IllegalStateException- if the corresponding Realm is closed or in an incorrect thread.IllegalStateException- if the Realm has been closed or called from an incorrect thread.
-
isFrozen
public boolean isFrozen()
-
isValid
public boolean isValid()
Checks if the collection is still valid to use, i.e., theRealminstance hasn't been closed. It will always returntruefor an unmanaged collection.- Specified by:
isValidin interfaceio.realm.internal.ManageableObject- Specified by:
isValidin interfaceRealmCollection<E>- Returns:
trueif it is still valid to use or an unmanaged collection,falseotherwise.
-
isManaged
public boolean isManaged()
ARealmResultsor aOrderedRealmCollectionSnapshotis always a managed collection.- Specified by:
isManagedin interfaceio.realm.internal.ManageableObject- Specified by:
isManagedin interfaceRealmCollection<E>- Returns:
true.- See Also:
RealmCollection.isManaged()
-
contains
public boolean contains(@Nullable Object object)Searches thisOrderedRealmCollectionfor the specified object.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Specified by:
containsin interfaceRealmCollection<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
object- the object to search for.- Returns:
trueifobjectis an element of thisOrderedRealmCollection,falseotherwise.
-
get
@Nullable public E get(int location)
Returns the element at the specified location in this list.- Specified by:
getin interfaceList<E>- Specified by:
getin classAbstractList<E>- Parameters:
location- the index of the element to return.- Returns:
- the element at the specified index.
- Throws:
IndexOutOfBoundsException- iflocation < 0 || location >= size().
-
first
@Nullable public E first()
Gets the first object from the collection.- Specified by:
firstin interfaceOrderedRealmCollection<E>- Returns:
- the first object.
-
first
@Nullable public E first(@Nullable E defaultValue)Gets the first object from the collection. If the collection is empty, the provided default will be used instead.- Specified by:
firstin interfaceOrderedRealmCollection<E>- Returns:
- the first object or the provided default.
-
last
@Nullable public E last()
Gets the last object from the collection.- Specified by:
lastin interfaceOrderedRealmCollection<E>- Returns:
- the last object.
-
last
@Nullable public E last(@Nullable E defaultValue)Gets the last object from the collection. If the collection is empty, the provided default will be used instead.- Specified by:
lastin interfaceOrderedRealmCollection<E>- Returns:
- the last object or the provided default.
-
iterator
public Iterator<E> iterator()
Returns an iterator for the results of a query. Any change to Realm while iterating will cause this iterator to throw aConcurrentModificationExceptionif accessed.- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin interfaceList<E>- Overrides:
iteratorin classAbstractList<E>- Returns:
- an iterator on the elements of this list.
- See Also:
Iterator
-
listIterator
public ListIterator<E> listIterator()
Returns a list iterator for the results of a query. Any change to Realm while iterating will cause the iterator to throw aConcurrentModificationExceptionif accessed.- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin classAbstractList<E>- Returns:
- a ListIterator on the elements of this list.
- See Also:
ListIterator
-
listIterator
public ListIterator<E> listIterator(int location)
Returns a list iterator on the results of a query. Any change to Realm while iterating will cause the iterator to throw aConcurrentModificationExceptionif accessed.- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin classAbstractList<E>- Parameters:
location- the index at which to start the iteration.- Returns:
- a ListIterator on the elements of this list.
- Throws:
IndexOutOfBoundsException- iflocation < 0 || location > size().- See Also:
ListIterator
-
min
public Number min(String fieldName)
Finds the minimum value of a field.- Specified by:
minin interfaceRealmCollection<E>- Parameters:
fieldName- the field to look for a minimum on. Only number fields are supported.- Returns:
- if no objects exist or they all have
nullas the value for the given field,nullwill be returned. Otherwise the minimum value is returned. When determining the minimum value, objects withnullvalues are ignored.
-
minDate
public Date minDate(String fieldName)
Finds the minimum date.- Specified by:
minDatein interfaceRealmCollection<E>- Parameters:
fieldName- the field to look for the minimum date. If fieldName is not of Date type, an exception is thrown.- Returns:
- if no objects exist or they all have
nullas the value for the given date field,nullwill be returned. Otherwise the minimum date is returned. When determining the minimum date, objects withnullvalues are ignored.
-
max
public Number max(String fieldName)
Finds the maximum value of a field.- Specified by:
maxin interfaceRealmCollection<E>- Parameters:
fieldName- the field to look for a maximum on. Only number fields are supported.- Returns:
- if no objects exist or they all have
nullas the value for the given field,nullwill be returned. Otherwise the maximum value is returned. When determining the maximum value, objects withnullvalues are ignored.
-
maxDate
@Nullable public Date maxDate(String fieldName)
Finds the maximum date.- Specified by:
maxDatein interfaceRealmCollection<E>- Parameters:
fieldName- the field to look for the maximum date. If fieldName is not of Date type, an exception is thrown.- Returns:
- if no objects exist or they all have
nullas the value for the given date field,nullwill be returned. Otherwise the maximum date is returned. When determining the maximum date, objects withnullvalues are ignored. - Throws:
IllegalArgumentException- if fieldName is not a Date field.
-
sum
public Number sum(String fieldName)
Calculates the sum of a given field.- Specified by:
sumin interfaceRealmCollection<E>- Parameters:
fieldName- the field to sum. Only number fields are supported.- Returns:
- the sum. If no objects exist or they all have
nullas the value for the given field,0will be returned. When computing the sum, objects withnullvalues are ignored.
-
average
public double average(String fieldName)
Returns the average of a given field.- Specified by:
averagein interfaceRealmCollection<E>- Parameters:
fieldName- the field to calculate average on. Only number fields are supported.- Returns:
- the average for the given field amongst objects in query results. This will be of type double for all
types of number fields. If no objects exist or they all have
nullas the value for the given field,0will be returned. When computing the average, objects withnullvalues are ignored.
-
remove
@Deprecated public E remove(int index)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractList<E>- Throws:
UnsupportedOperationException
-
remove
@Deprecated public boolean remove(Object object)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>- Throws:
UnsupportedOperationException
-
removeAll
@Deprecated public boolean removeAll(Collection<?> collection)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceList<E>- Overrides:
removeAllin classAbstractCollection<E>- Throws:
UnsupportedOperationException
-
set
@Deprecated public E set(int location, E object)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
setin interfaceList<E>- Overrides:
setin classAbstractList<E>- Throws:
UnsupportedOperationException
-
retainAll
@Deprecated public boolean retainAll(Collection<?> collection)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceList<E>- Overrides:
retainAllin classAbstractCollection<E>- Throws:
UnsupportedOperationException
-
clear
@Deprecated public void clear()
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>- Throws:
UnsupportedOperationException- always.
-
add
@Deprecated public boolean add(E element)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Throws:
UnsupportedOperationException- always.
-
add
@Deprecated public void add(int index, E element)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Throws:
UnsupportedOperationException- always.
-
addAll
@Deprecated public boolean addAll(int location, Collection<? extends E> collection)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractList<E>- Throws:
UnsupportedOperationException- always.
-
addAll
@Deprecated public boolean addAll(Collection<? extends E> collection)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Throws:
UnsupportedOperationException- always.
-
getRealm
public Realm getRealm()
Returns theRealminstance to which this collection belongs.Calling
Closeable.close()on the returned instance is discouraged as it is the same as calling it on the original Realm instance which may cause the Realm to fully close invalidating the query result.- Returns:
Realminstance this collection belongs to.- Throws:
IllegalStateException- if the Realm is an instance ofDynamicRealmor theRealmwas already closed.
-
-