Docs Menu

Docs HomeDevelop ApplicationsAtlas Device SDKs

Class MutableRealmInteger

On this page

  • io.realm
  • Method Summary
  • Inherited Methods
  • Method Detail
  • compareTo
  • decrement
  • equals
  • get
  • hashCode
  • increment
  • isNull
  • ofNull
  • set
  • valueOf
io.realm.MutableRealmInteger

Implemented interfaces:

A MutableRealmInteger is a mutable, Long -like numeric quantity. It behaves almost exactly as a reference to a Long . More specifically:

  • A MutableRealmInteger may have the value null .

  • The equals operator compares the contained Long values. null -valued MutableRealmInteger are .equals

  • The compareTo operator compares the contained Long values. It considers null < any non-null value.

  • The increment and decrement 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 and decrement 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
public final int

MutableRealmInteger s compare strictly by their values.

public abstract void
long dec
)

Decrements the MutableRealmInteger , subtracting the value of the argument.

public final boolean

Two MutableRealmInteger s are .equals if and only if their longValues are equal.

public abstract Long
get ()

Gets the MutableRealmInteger value.

public final int

A MutableRealmInteger 's hash code is, exactly, the hash code of its value.

public abstract void
long inc
)

Increments the MutableRealmInteger , adding the value of the argument.

public final boolean
public static MutableRealmInteger

Creates a new, unmanaged MutableRealmInteger whose value is null .

public final void
set (
long newValue
)

Sets the MutableRealmInteger value.

public abstract void
set (
Long newValue
)

Sets the MutableRealmInteger value.

public static MutableRealmInteger
String value
)

Creates a new, unmanaged MutableRealmInteger with the specified initial value.

public static MutableRealmInteger
long value
)

Creates a new, unmanaged MutableRealmInteger with the specified initial value.

public static MutableRealmInteger
Long value
)

Creates a new, unmanaged MutableRealmInteger with the specified initial value.

  • Methods inherited from class java.lang.Object : getClass , hashCode , equals , clone , toString , notify , notifyAll , wait , wait , wait , finalize

public final int compareTo (
)

MutableRealmInteger s compare strictly by their values. Null is a legal value for a MutableRealmInteger and null < any non-null value

Parameters

  • o - the compare target

Returns

-1, 0, or 1, depending on whether this object's value is <, =, or > the target's.

public abstract void decrement (
long dec
)

Decrements the MutableRealmInteger , 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 the MutableRealmInteger .

public final boolean equals (
)

Two MutableRealmInteger s are .equals if and only if their longValues are equal.

Parameters

  • o - compare target

Returns

true if the target has the same value.

Overrides

equals in class Object

public abstract Long get ()

Gets the MutableRealmInteger value. The value may be null.

Returns

the value.

public final int hashCode ()

A MutableRealmInteger 's hash code is, exactly, the hash code of its value.

Returns

true if the target has the same value.

Overrides

hashCode in class Object

public abstract void increment (
long inc
)

Increments the MutableRealmInteger , 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 the MutableRealmInteger .

public final boolean isNull ()

Returns

true if and only if get will return null .

public static MutableRealmInteger ofNull ()

Creates a new, unmanaged MutableRealmInteger whose value is null .
public final void set (
long newValue
)

Sets the MutableRealmInteger value. Calling set forcibly sets the MutableRealmInteger to the provided value. Doing this obliterates the effects of any calls to increment and decrement perceived before the call to set .

Parameters

  • newValue - new value.

public abstract void set (
Long newValue
)

Sets the MutableRealmInteger value. Calling set forcibly sets the MutableRealmInteger to the provided value. Doing this obliterates the effects of any calls to increment and decrement perceived before the call to set .

Parameters

  • newValue - new value.

String value
)

Creates a new, unmanaged MutableRealmInteger with the specified initial value.

Parameters

long value
)

Creates a new, unmanaged MutableRealmInteger with the specified initial value.

Parameters

  • value - initial value.

Long value
)

Creates a new, unmanaged MutableRealmInteger with the specified initial value.

Parameters

  • value - initial value.

← Interface MapChangeSet