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>
AnOrderedRealmCollectionSnapshot
is a special type ofOrderedRealmCollection
. It can be created by callingOrderedRealmCollection.createSnapshot()
. UnlikeRealmResults
andRealmList
, its size and order of elements will never be changed after creation.OrderedRealmCollectionSnapshot
is 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.BaseRealm
baseRealm
TheBaseRealm
instance in which this collection resides.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
add(int index, E element)
Deprecated.boolean
add(E element)
Deprecated.boolean
addAll(int location, Collection<? extends E> collection)
Deprecated.boolean
addAll(Collection<? extends E> collection)
Deprecated.double
average(String fieldName)
Returns the average of a given field.void
clear()
Deprecated.boolean
contains(Object object)
Searches thisOrderedRealmCollection
for the specified object.OrderedRealmCollectionSnapshot<E>
createSnapshot()
Creates a snapshot from thisOrderedRealmCollection
.boolean
deleteAllFromRealm()
This deletes all objects in the collection from the underlying Realm.boolean
deleteFirstFromRealm()
Deletes the first object from the Realm.void
deleteFromRealm(int location)
Deletes the object at the given index from the Realm.boolean
deleteLastFromRealm()
Deletes the last object from the Realm.E
first()
Gets the first object from the collection.E
first(E defaultValue)
Gets the first object from the collection.OrderedRealmCollection<E>
freeze()
E
get(int location)
Returns the element at the specified location in this list.Realm
getRealm()
Returns theRealm
instance to which this collection belongs.boolean
isFrozen()
boolean
isLoaded()
Checks if a collection has finished loading its data yet.boolean
isManaged()
ARealmResults
or aOrderedRealmCollectionSnapshot
is always a managed collection.boolean
isValid()
Checks if the collection is still valid to use, i.e., theRealm
instance hasn't been closed.Iterator<E>
iterator()
Returns an iterator for the results of a query.E
last()
Gets the last object from the collection.E
last(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.boolean
load()
Blocks the collection until all data are available.Number
max(String fieldName)
Finds the maximum value of a field.Date
maxDate(String fieldName)
Finds the maximum date.Number
min(String fieldName)
Finds the minimum value of a field.Date
minDate(String fieldName)
Finds the minimum date.E
remove(int index)
Deprecated.boolean
remove(Object object)
Deprecated.boolean
removeAll(Collection<?> collection)
Deprecated.boolean
retainAll(Collection<?> collection)
Deprecated.E
set(int location, E object)
Deprecated.int
size()
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
.Number
sum(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 originalOrderedRealmCollection
instead.- Specified by:
sort
in 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
RealmResults
will 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 originalOrderedRealmCollection
instead.- Specified by:
sort
in 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
RealmResults
will 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 originalOrderedRealmCollection
instead.- Specified by:
sort
in 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
RealmResults
will 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 originalOrderedRealmCollection
instead.- Specified by:
sort
in 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
RealmResults
will 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 originalOrderedRealmCollection
instead.- Returns:
- a RealmQuery object.
- Throws:
UnsupportedOperationException
- See Also:
RealmQuery
-
isLoaded
public boolean isLoaded()
Checks if a collection has finished loading its data yet.- Returns:
true
if data has been loaded and is available,false
if data is still being loaded.
-
load
public boolean load()
Blocks the collection until all data are available.- Returns:
true
if the data could be successfully loaded,false
otherwise.
-
createSnapshot
public OrderedRealmCollectionSnapshot<E> createSnapshot()
Creates a snapshot from thisOrderedRealmCollection
.- Specified by:
createSnapshot
in 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:
deleteFromRealm
in 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:
deleteFirstFromRealm
in interfaceOrderedRealmCollection<E>
- Returns:
true
if an object was deleted,false
otherwise.- 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:
deleteLastFromRealm
in interfaceOrderedRealmCollection<E>
- Returns:
true
if an object was deleted,false
otherwise.- 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:
deleteAllFromRealm
in interfaceRealmCollection<E>
- Returns:
true
if objects was deleted,false
otherwise.- 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., theRealm
instance hasn't been closed. It will always returntrue
for an unmanaged collection.- Specified by:
isValid
in interfaceio.realm.internal.ManageableObject
- Specified by:
isValid
in interfaceRealmCollection<E>
- Returns:
true
if it is still valid to use or an unmanaged collection,false
otherwise.
-
isManaged
public boolean isManaged()
ARealmResults
or aOrderedRealmCollectionSnapshot
is always a managed collection.- Specified by:
isManaged
in interfaceio.realm.internal.ManageableObject
- Specified by:
isManaged
in interfaceRealmCollection<E>
- Returns:
true
.- See Also:
RealmCollection.isManaged()
-
contains
public boolean contains(@Nullable Object object)
Searches thisOrderedRealmCollection
for the specified object.- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceList<E>
- Specified by:
contains
in interfaceRealmCollection<E>
- Overrides:
contains
in classAbstractCollection<E>
- Parameters:
object
- the object to search for.- Returns:
true
ifobject
is an element of thisOrderedRealmCollection
,false
otherwise.
-
get
@Nullable public E get(int location)
Returns the element at the specified location in this list.- Specified by:
get
in interfaceList<E>
- Specified by:
get
in 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:
first
in 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:
first
in interfaceOrderedRealmCollection<E>
- Returns:
- the first object or the provided default.
-
last
@Nullable public E last()
Gets the last object from the collection.- Specified by:
last
in 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:
last
in 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 aConcurrentModificationException
if accessed.- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in interfaceList<E>
- Overrides:
iterator
in 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 aConcurrentModificationException
if accessed.- Specified by:
listIterator
in interfaceList<E>
- Overrides:
listIterator
in 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 aConcurrentModificationException
if accessed.- Specified by:
listIterator
in interfaceList<E>
- Overrides:
listIterator
in 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:
min
in 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
null
as the value for the given field,null
will be returned. Otherwise the minimum value is returned. When determining the minimum value, objects withnull
values are ignored.
-
minDate
public Date minDate(String fieldName)
Finds the minimum date.- Specified by:
minDate
in 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
null
as the value for the given date field,null
will be returned. Otherwise the minimum date is returned. When determining the minimum date, objects withnull
values are ignored.
-
max
public Number max(String fieldName)
Finds the maximum value of a field.- Specified by:
max
in 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
null
as the value for the given field,null
will be returned. Otherwise the maximum value is returned. When determining the maximum value, objects withnull
values are ignored.
-
maxDate
@Nullable public Date maxDate(String fieldName)
Finds the maximum date.- Specified by:
maxDate
in 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
null
as the value for the given date field,null
will be returned. Otherwise the maximum date is returned. When determining the maximum date, objects withnull
values 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:
sum
in interfaceRealmCollection<E>
- Parameters:
fieldName
- the field to sum. Only number fields are supported.- Returns:
- the sum. If no objects exist or they all have
null
as the value for the given field,0
will be returned. When computing the sum, objects withnull
values are ignored.
-
average
public double average(String fieldName)
Returns the average of a given field.- Specified by:
average
in 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
null
as the value for the given field,0
will be returned. When computing the average, objects withnull
values are ignored.
-
remove
@Deprecated public E remove(int index)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
remove
in interfaceList<E>
- Overrides:
remove
in classAbstractList<E>
- Throws:
UnsupportedOperationException
-
remove
@Deprecated public boolean remove(Object object)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceList<E>
- Overrides:
remove
in classAbstractCollection<E>
- Throws:
UnsupportedOperationException
-
removeAll
@Deprecated public boolean removeAll(Collection<?> collection)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceList<E>
- Overrides:
removeAll
in classAbstractCollection<E>
- Throws:
UnsupportedOperationException
-
set
@Deprecated public E set(int location, E object)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
set
in interfaceList<E>
- Overrides:
set
in classAbstractList<E>
- Throws:
UnsupportedOperationException
-
retainAll
@Deprecated public boolean retainAll(Collection<?> collection)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
retainAll
in interfaceCollection<E>
- Specified by:
retainAll
in interfaceList<E>
- Overrides:
retainAll
in classAbstractCollection<E>
- Throws:
UnsupportedOperationException
-
clear
@Deprecated public void clear()
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceList<E>
- Overrides:
clear
in classAbstractList<E>
- Throws:
UnsupportedOperationException
- always.
-
add
@Deprecated public boolean add(E element)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceList<E>
- Overrides:
add
in classAbstractList<E>
- Throws:
UnsupportedOperationException
- always.
-
add
@Deprecated public void add(int index, E element)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
add
in interfaceList<E>
- Overrides:
add
in classAbstractList<E>
- Throws:
UnsupportedOperationException
- always.
-
addAll
@Deprecated public boolean addAll(int location, Collection<? extends E> collection)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
addAll
in interfaceList<E>
- Overrides:
addAll
in classAbstractList<E>
- Throws:
UnsupportedOperationException
- always.
-
addAll
@Deprecated public boolean addAll(Collection<? extends E> collection)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceList<E>
- Overrides:
addAll
in classAbstractCollection<E>
- Throws:
UnsupportedOperationException
- always.
-
getRealm
public Realm getRealm()
Returns theRealm
instance 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:
Realm
instance this collection belongs to.- Throws:
IllegalStateException
- if the Realm is an instance ofDynamicRealm
or theRealm
was already closed.
-
-