Class MutableRealmInteger
- java.lang.Object
-
- io.realm.MutableRealmInteger
-
- All Implemented Interfaces:
io.realm.internal.ManageableObject
,Comparable<MutableRealmInteger>
public abstract class MutableRealmInteger extends Object implements Comparable<MutableRealmInteger>, io.realm.internal.ManageableObject
AMutableRealmInteger
is a mutable,Long
-like numeric quantity. It behaves almost exactly as a reference to aLong
. More specifically:- A
MutableRealmInteger
may have the valuenull
. - The
equals(java.lang.Object)
operator compares the containedLong
values.null
-valuedMutableRealmInteger
are.equals
- The
compareTo(io.realm.MutableRealmInteger)
operator compares the containedLong
values. It considersnull
< any non-null
value. - The
increment(long)
anddecrement(long)
operators throwIllegalStateException
when applied to anull
-valuedMutableRealmInteger
.
MutableRealmInteger
s are most interesting as members of a managedRealmModel
object. When managed, theincrement(long)
anddecrement(long)
operators implement a conflict free replicated data type: Simultaneous increments and decrements from multiple distributed clients will be aggregated correctly. For instance, if the value ofcounter
field for the object representing user "Fred" is currently 0, then the following code, executed on two different devices, simultaneously, even if connected by only a slow, unreliable network, will always cause the value ofcounter
to converge, eventually on the value 2.MutableRealmInteger counter = realm.where(Users.class) .equalTo("name", Fred) .findFirst() .counter.increment(1);
set(Long)
operator must be used with extreme care. It will quash the effects of any prior calls toincrement(long)
ordecrement(long)
. Although the value of aMutableRealmInteger
will always converge across devices, the specific value on which it converges will depend on the actual order in which operations took place. Mixingset(Long)
withincrement(long)
anddecrement(long)
is, therefore, not advised, unless fuzzy counting is acceptable.MutableRealmInteger
s may not be primary keys. Their implementations are not thread safe. Like all managed Realm objects, managedMutableRealmInteger
s may not be moved across threads. UnmanagedMutableRealmInteger
s may be moved across threads but require safe publication.A
MutableRealmInteger
, in a model class, must always be declaredfinal
. For instance:public final MutableRealmInteger counter = MutableRealmInteger.ofNull();
MutableRealmInteger
asnull
may work very limited circumstances, developers are advised not to do it:public final MutableRealmInteger counter = null; // DO NOT DO THIS!
MutableRealmInteger
is@Required
, it is better, though not required, to initialize it with a non-null value.@Required public final MutableRealmInteger counter = MutableRealmInteger.valueOf(0L);
A reference to a managed
MutableRealmInteger
is subject to all of the constraints that apply to the model object from which it was obtained: It can only be mutated within a transaction and it becomes invalid if the Realm backing it is closed. Use theisManaged()
andisValid()
operators to determine whether aMutableRealmInteger
is in a consistent state. Note, in particular, that a reference to a managedMutableRealmInteger
retains a reference to the model object to which it belongs. For example in this code:MutableRealmInteger counter = realm.where(Users.class).findFirst().counter;
counter
holds a reference to theUser
model object from which it was obtained. Neither can be GCed until all references to both are unreachable.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description int
compareTo(MutableRealmInteger o)
MutableRealmInteger
s compare strictly by their values.abstract void
decrement(long dec)
Decrements theMutableRealmInteger
, subtracting the value of the argument.boolean
equals(Object o)
TwoMutableRealmInteger
s are.equals
if and only if theirlongValues
are equal.abstract Long
get()
Gets theMutableRealmInteger
value.int
hashCode()
AMutableRealmInteger
's hash code is, exactly, the hash code of its value.abstract void
increment(long inc)
Increments theMutableRealmInteger
, adding the value of the argument.boolean
isNull()
static MutableRealmInteger
ofNull()
Creates a new, unmanagedMutableRealmInteger
whose value isnull
.void
set(long newValue)
Sets theMutableRealmInteger
value.abstract void
set(Long newValue)
Sets theMutableRealmInteger
value.static MutableRealmInteger
valueOf(long value)
Creates a new, unmanagedMutableRealmInteger
with the specified initial value.static MutableRealmInteger
valueOf(Long value)
Creates a new, unmanagedMutableRealmInteger
with the specified initial value.static MutableRealmInteger
valueOf(String value)
Creates a new, unmanagedMutableRealmInteger
with the specified initial value.
-
-
-
Method Detail
-
valueOf
public static MutableRealmInteger valueOf(Long value)
Creates a new, unmanagedMutableRealmInteger
with the specified initial value.- Parameters:
value
- initial value.
-
ofNull
public static MutableRealmInteger ofNull()
Creates a new, unmanagedMutableRealmInteger
whose value isnull
.
-
valueOf
public static MutableRealmInteger valueOf(long value)
Creates a new, unmanagedMutableRealmInteger
with the specified initial value.- Parameters:
value
- initial value.
-
valueOf
public static MutableRealmInteger valueOf(String value)
Creates a new, unmanagedMutableRealmInteger
with the specified initial value.- Parameters:
value
- initial value: parsed byLong.parseLong(java.lang.String, int)
.
-
get
@Nullable public abstract Long get()
Gets theMutableRealmInteger
value. The value may be null.- Returns:
- the value.
-
set
public abstract void set(@Nullable Long newValue)
Sets theMutableRealmInteger
value. Callingset
forcibly sets theMutableRealmInteger
to the provided value. Doing this obliterates the effects of any calls toincrement(long)
anddecrement(long)
perceived before the call toset
.- Parameters:
newValue
- new value.
-
set
public final void set(long newValue)
Sets theMutableRealmInteger
value. Callingset(java.lang.Long)
forcibly sets theMutableRealmInteger
to the provided value. Doing this obliterates the effects of any calls toincrement(long)
anddecrement(long)
perceived before the call toset(java.lang.Long)
.- Parameters:
newValue
- new value.
-
increment
public abstract void increment(long inc)
Increments theMutableRealmInteger
, adding the value of the argument. Increment/decrement from all devices are reflected in the new value, which is guaranteed to converge.- Parameters:
inc
- quantity to be added to theMutableRealmInteger
.
-
decrement
public abstract void decrement(long dec)
Decrements theMutableRealmInteger
, subtracting the value of the argument. Increment/decrement from all devices are reflected in the new value, which is guaranteed to converge.- Parameters:
dec
- quantity to be subtracted from theMutableRealmInteger
.
-
isNull
public final boolean isNull()
- Returns:
- true if and only if
get()
will returnnull
.
-
compareTo
public final int compareTo(MutableRealmInteger o)
MutableRealmInteger
s compare strictly by their values. Null is a legal value for aMutableRealmInteger
andnull
< any non-null
value- Specified by:
compareTo
in interfaceComparable<MutableRealmInteger>
- Parameters:
o
- the compare target- Returns:
- -1, 0, or 1, depending on whether this object's value is <, =, or > the target's.
-
hashCode
public final int hashCode()
AMutableRealmInteger
's hash code is, exactly, the hash code of its value.
-
-