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
AMutableRealmIntegeris a mutable,Long-like numeric quantity. It behaves almost exactly as a reference to aLong. More specifically:- A
MutableRealmIntegermay have the valuenull. - The
equals(java.lang.Object)operator compares the containedLongvalues.null-valuedMutableRealmIntegerare.equals - The
compareTo(io.realm.MutableRealmInteger)operator compares the containedLongvalues. It considersnull< any non-nullvalue. - The
increment(long)anddecrement(long)operators throwIllegalStateExceptionwhen applied to anull-valuedMutableRealmInteger.
MutableRealmIntegers are most interesting as members of a managedRealmModelobject. 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 ofcounterfield 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 ofcounterto converge, eventually on the value 2.
Note that theMutableRealmInteger 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 aMutableRealmIntegerwill 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.MutableRealmIntegers may not be primary keys. Their implementations are not thread safe. Like all managed Realm objects, managedMutableRealmIntegers may not be moved across threads. UnmanagedMutableRealmIntegers may be moved across threads but require safe publication.A
MutableRealmInteger, in a model class, must always be declaredfinal. For instance:
Although initializing thepublic final MutableRealmInteger counter = MutableRealmInteger.ofNull();MutableRealmIntegerasnullmay work very limited circumstances, developers are advised not to do it:
Also note that when apublic final MutableRealmInteger counter = null; // DO NOT DO THIS!MutableRealmIntegeris@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
MutableRealmIntegeris 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 aMutableRealmIntegeris in a consistent state. Note, in particular, that a reference to a managedMutableRealmIntegerretains a reference to the model object to which it belongs. For example in this code:
theMutableRealmInteger counter = realm.where(Users.class).findFirst().counter;counterholds a reference to theUsermodel 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 intcompareTo(MutableRealmInteger o)MutableRealmIntegers compare strictly by their values.abstract voiddecrement(long dec)Decrements theMutableRealmInteger, subtracting the value of the argument.booleanequals(Object o)TwoMutableRealmIntegers are.equalsif and only if theirlongValuesare equal.abstract Longget()Gets theMutableRealmIntegervalue.inthashCode()AMutableRealmInteger's hash code is, exactly, the hash code of its value.abstract voidincrement(long inc)Increments theMutableRealmInteger, adding the value of the argument.booleanisNull()static MutableRealmIntegerofNull()Creates a new, unmanagedMutableRealmIntegerwhose value isnull.voidset(long newValue)Sets theMutableRealmIntegervalue.abstract voidset(Long newValue)Sets theMutableRealmIntegervalue.static MutableRealmIntegervalueOf(long value)Creates a new, unmanagedMutableRealmIntegerwith the specified initial value.static MutableRealmIntegervalueOf(Long value)Creates a new, unmanagedMutableRealmIntegerwith the specified initial value.static MutableRealmIntegervalueOf(String value)Creates a new, unmanagedMutableRealmIntegerwith the specified initial value.
-
-
-
Method Detail
-
valueOf
public static MutableRealmInteger valueOf(Long value)
Creates a new, unmanagedMutableRealmIntegerwith the specified initial value.- Parameters:
value- initial value.
-
ofNull
public static MutableRealmInteger ofNull()
Creates a new, unmanagedMutableRealmIntegerwhose value isnull.
-
valueOf
public static MutableRealmInteger valueOf(long value)
Creates a new, unmanagedMutableRealmIntegerwith the specified initial value.- Parameters:
value- initial value.
-
valueOf
public static MutableRealmInteger valueOf(String value)
Creates a new, unmanagedMutableRealmIntegerwith the specified initial value.- Parameters:
value- initial value: parsed byLong.parseLong(java.lang.String, int).
-
get
@Nullable public abstract Long get()
Gets theMutableRealmIntegervalue. The value may be null.- Returns:
- the value.
-
set
public abstract void set(@Nullable Long newValue)Sets theMutableRealmIntegervalue. Callingsetforcibly sets theMutableRealmIntegerto 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 theMutableRealmIntegervalue. Callingset(java.lang.Long)forcibly sets theMutableRealmIntegerto 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)
MutableRealmIntegers compare strictly by their values. Null is a legal value for aMutableRealmIntegerandnull< any non-nullvalue- Specified by:
compareToin 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.
-
-