public abstract class MutableRealmInteger extends Object implements Comparable<MutableRealmInteger>
MutableRealmInteger
is a mutable, Long
-like numeric quantity.
It behaves almost exactly as a reference to a Long
. More specifically:
MutableRealmInteger
may have the value null
.equals(java.lang.Object)
operator compares the contained Long
values. null
-valued MutableRealmInteger
are .equals
compareTo(io.realm.MutableRealmInteger)
operator compares the contained Long
values. It considers null
< any non-null
value.increment(long)
and decrement(long)
operators throw IllegalStateException
when applied to a null
-valued MutableRealmInteger
.
MutableRealmInteger
s are most interesting as members of a managed RealmModel
object.
When managed, the increment(long)
and decrement(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 of counter
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 of counter
to converge, eventually on the value 2.
MutableRealmInteger counter = realm.where(Users.class)
.equalTo("name", Fred)
.findFirst()
.counter.increment(1);
Note that the set(Long)
operator must be used with extreme care. It will quash the effects of any prior calls
to increment(long)
or decrement(long)
. Although the value of a MutableRealmInteger
will
always converge across devices, the specific value on which it converges will depend on the actual order in which
operations took place. Mixing set(Long)
with increment(long)
and decrement(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, managed MutableRealmInteger
s may not be moved across threads.
Unmanaged MutableRealmInteger
s may be moved across threads but require safe publication.
A MutableRealmInteger
, in a model class, must always be declared final
. For instance:
public final MutableRealmInteger counter = MutableRealmInteger.ofNull();
Although initializing the MutableRealmInteger
as null
may work very limited circumstances,
developers are advised not to do it:
public final MutableRealmInteger counter = null; // DO NOT DO THIS!
Also note that when a 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 the isManaged()
and isValid()
operators to determine whether a MutableRealmInteger
is
in a consistent state. Note, in particular, that a reference to a managed MutableRealmInteger
retains a reference to the model object to which it belongs. For example in this code:
MutableRealmInteger counter = realm.where(Users.class).findFirst().counter;
the counter
holds a reference to the User
model object from which it was obtained.
Neither can be GCed until all references to both are unreachable.Modifier and Type | Method and Description |
---|---|
int |
compareTo(MutableRealmInteger o)
MutableRealmInteger s compare strictly by their values. |
abstract void |
decrement(long dec)
Decrements the
MutableRealmInteger , subtracting the value of the argument. |
boolean |
equals(Object o)
Two
MutableRealmInteger s are .equals if and only if their longValues are equal. |
abstract Long |
get()
Gets the
MutableRealmInteger value. |
int |
hashCode()
A
MutableRealmInteger 's hash code is, exactly, the hash code of its value. |
abstract void |
increment(long inc)
Increments the
MutableRealmInteger , adding the value of the argument. |
boolean |
isNull() |
static MutableRealmInteger |
ofNull()
Creates a new, unmanaged
MutableRealmInteger whose value is null . |
void |
set(long newValue)
Sets the
MutableRealmInteger value. |
abstract void |
set(Long newValue)
Sets the
MutableRealmInteger value. |
static MutableRealmInteger |
valueOf(long value)
Creates a new, unmanaged
MutableRealmInteger with the specified initial value. |
static MutableRealmInteger |
valueOf(Long value)
Creates a new, unmanaged
MutableRealmInteger with the specified initial value. |
static MutableRealmInteger |
valueOf(String value)
Creates a new, unmanaged
MutableRealmInteger with the specified initial value. |
public static MutableRealmInteger valueOf(Long value)
MutableRealmInteger
with the specified initial value.value
- initial value.public static MutableRealmInteger ofNull()
MutableRealmInteger
whose value is null
.public static MutableRealmInteger valueOf(long value)
MutableRealmInteger
with the specified initial value.value
- initial value.public static MutableRealmInteger valueOf(String value)
MutableRealmInteger
with the specified initial value.value
- initial value: parsed by Long.parseLong(java.lang.String, int)
.public abstract Long get()
MutableRealmInteger
value.
The value may be null.public abstract void set(Long newValue)
MutableRealmInteger
value.
Calling set
forcibly sets the MutableRealmInteger
to the provided value.
Doing this obliterates the effects of any calls to increment(long)
and decrement(long)
perceived
before the call to set
.newValue
- new value.public final void set(long newValue)
MutableRealmInteger
value.
Calling set(java.lang.Long)
forcibly sets the MutableRealmInteger
to the provided value.
Doing this obliterates the effects of any calls to increment(long)
and decrement(long)
perceived
before the call to set(java.lang.Long)
.newValue
- new value.public abstract void increment(long inc)
MutableRealmInteger
, adding the value of the argument.
Increment/decrement from all devices are reflected in the new value, which is guaranteed to converge.inc
- quantity to be added to the MutableRealmInteger
.public abstract void decrement(long dec)
MutableRealmInteger
, subtracting the value of the argument.
Increment/decrement from all devices are reflected in the new value, which is guaranteed to converge.dec
- quantity to be subtracted from the MutableRealmInteger
.public final boolean isNull()
get()
will return null
.public final int compareTo(MutableRealmInteger o)
MutableRealmInteger
s compare strictly by their values.
Null is a legal value for a MutableRealmInteger
and null
< any non-null
valuecompareTo
in interface Comparable<MutableRealmInteger>
o
- the compare targetpublic final int hashCode()
MutableRealmInteger
's hash code is, exactly, the hash code of its value.