Docs Menu

Class RealmAny

On this page

  • io.realm
  • Nested Class Summary
  • Method Summary
  • Inherited Methods
  • Method Detail
  • asBinary
  • asBoolean
  • asByte
  • asDate
  • asDecimal128
  • asDouble
  • asFloat
  • asInteger
  • asLong
  • asObjectId
  • asRealmModel
  • asShort
  • asString
  • asUUID
  • coercedEquals
  • equals
  • getType
  • getValueClass
  • hashCode
  • isNull
  • nullValue
  • toString
  • valueOf
java.lang.Object
io.realm.RealmAny

io.realm.RealmAny is used to represent a polymorphic Realm value.

At any particular moment an instance of this class stores a definite value of a definite type. If, for instance, that is an double value, you may call asDouble() to extract that value. You may call getType() to discover what type of value is currently stored. Calling asDouble() on an instance that does not store an double would raise a java.lang.ClassCastException .

RealmAny behaves like a value type on all the supported types except on Realm objects. It means that Realm will not persist any change to the RealmAny value except when the type is Realm object. When a RealmAny holds a Realm object, it just holds the reference to it, not a copy of the object. So modifications to the Realm object are reflected in the RealmAny value, including if the object is deleted. Because RealmAny instances are immutable, a new instance is needed to update a RealmAny attribute.

anObject.realmAnyAttribute = RealmAny.valueOf(5);
anObject.realmAnyAttribute = RealmAny.valueOf(10.f);

It is crucial to understand that the act of extracting a value of a particular type requires definite knowledge about the stored type. Calling a getter method for any particular type, that is not the same type as the stored value, would raise an exception.Our recommendation to handle the RealmAny polymorphism is to write a switch case around the RealmAny type and its inner value class.

RealmAny realmAny = aRealmObject.realmAnyAttribute;
switch (realmAny.getType()) {
case OBJECT:
if (realmAny.getValueClass().equals(DogRealmModel.class)) {
DogRealmModel value = realmAny.asRealmModel(DogRealmModel.class);
}
case INTEGER:
performAction(realmAny.asInteger());
break;
case BOOLEAN:
performAction(realmAny.asBoolean());
break;
case STRING:
performAction(realmAny.asString());
break;
case BINARY:
performAction(realmAny.asBinary());
break;
case DATE:
performAction(realmAny.asDate());
break;
case FLOAT:
performAction(realmAny.asFloat());
break;
case DOUBLE:
performAction(realmAny.asDouble());
break;
case DECIMAL128:
performAction(realmAny.asDecimal128());
break;
case OBJECT_ID:
performAction(realmAny.asObjectId());
break;
case UUID:
performAction(realmAny.asUUID());
break;
case NULL:
performNullAction();
break;
}

getValueClass() returns the Java class that represents the inner value wrapped by the RealmAny instance. If the resulting class is a realization of io.realm.RealmModel asRealmModel() can be called to cast the RealmAny value to a Realm object reference.

RealmAny values can also be sorted. The sorting order used between different RealmAny types, from lowest to highest, is:

  1. Boolean
  2. Byte/Short/Integer/Long/Float/Double/Decimal128
  3. byte[]/String
  4. Date
  5. ObjectId
  6. UUID
  7. RealmObject

This has implications on how RealmQuery.sort(String) , RealmQuery.minRealmAny(String) and RealmQuery.maxRealmAny(String) work. Especially min() and max() will not only take numeric fields into account, but will use the sorting order to determine the "largest" or "lowest" value.

Modifier and Type
Class and Description
public static final
Modifier and Type
Method and Description
public byte

Gets this value as a byte[] if it is one, otherwise throws exception.

public Boolean

Gets this value as a Boolean if it is one, otherwise throws exception.

public Byte

Gets this value as a Byte if it is one, otherwise throws exception.

public Date

Gets this value as a Date if it is one, otherwise throws exception.

public Decimal128

Gets this value as a Decimal128 if it is one, otherwise throws exception.

public Double

Gets this value as a Double if it is one, otherwise throws exception.

public Float

Gets this value as a Float if it is one, otherwise throws exception.

public Integer

Gets this value as a Integer if it is one, otherwise throws exception.

public Long

Gets this value as a Long if it is one, otherwise throws exception.

public ObjectId

Gets this value as a ObjectId if it is one, otherwise throws exception.

public T

Gets this value as a RealmModel if it is one, otherwise throws exception.

public Short

Gets this value as a Short if it is one, otherwise throws exception.

public String

Gets this value as a String if it is one, otherwise throws exception.

public UUID

Gets this value as a UUID if it is one, otherwise throws exception.

public final boolean
public final boolean
Object other
)

Two RealmAny s are .equals if and only if their contents are equal.

Gets the inner type of this RealmAny object.

public Class

Returns the Java class that represents the inner value wrapped by this RealmAny value.

public final int

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

public boolean

Returns true if the inner value is null, false otherwise.

public static RealmAny

Creates a new RealmAny of a null value.

public String
public static RealmAny
Decimal128 value
)

Creates a new RealmAny with the specified value.

public static RealmAny
ObjectId value
)

Creates a new RealmAny with the specified value.

public static RealmAny
Date value
)

Creates a new RealmAny with the specified value.

public static RealmAny
byte[] value
)

Creates a new RealmAny with the specified value.

public static RealmAny
String value
)

Creates a new RealmAny with the specified value.

public static RealmAny
Double value
)

Creates a new RealmAny with the specified value.

public static RealmAny
Float value
)

Creates a new RealmAny with the specified value.

public static RealmAny

Creates a new RealmAny with the specified value.

public static RealmAny
Long value
)

Creates a new RealmAny with the specified value.

public static RealmAny

Creates a new RealmAny with the specified value.

public static RealmAny
Short value
)

Creates a new RealmAny with the specified value.

public static RealmAny
Byte value
)

Creates a new RealmAny with the specified value.

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

Gets this value as a byte[] if it is one, otherwise throws exception.

Returns

a byte[].

Throws

public Boolean asBoolean ()

Gets this value as a Boolean if it is one, otherwise throws exception.

Returns

a Boolean.

Throws

public Byte asByte ()

Gets this value as a Byte if it is one, otherwise throws exception.

Returns

a Byte.

Throws

public Date asDate ()

Gets this value as a Date if it is one, otherwise throws exception.

Returns

a Date.

Throws

public Decimal128 asDecimal128 ()

Gets this value as a Decimal128 if it is one, otherwise throws exception.

Returns

a Decimal128.

Throws

public Double asDouble ()

Gets this value as a Double if it is one, otherwise throws exception.

Returns

a Double.

Throws

public Float asFloat ()

Gets this value as a Float if it is one, otherwise throws exception.

Returns

a Float.

Throws

public Integer asInteger ()

Gets this value as a Integer if it is one, otherwise throws exception.

Returns

an Integer.

Throws

public Long asLong ()

Gets this value as a Long if it is one, otherwise throws exception.

Returns

a Long.

Throws

public ObjectId asObjectId ()

Gets this value as a ObjectId if it is one, otherwise throws exception.

Returns

an ObjectId.

Throws

public T asRealmModel <T >(
)

Gets this value as a RealmModel if it is one, otherwise throws exception.

Type Parameters

  • T - the RealmModel type to cast the inner value to.

Returns

a RealmModel of the T type.

Throws

public Short asShort ()

Gets this value as a Short if it is one, otherwise throws exception.

Returns

a Short.

Throws

public String asString ()

Gets this value as a String if it is one, otherwise throws exception.

Returns

a String.

Throws

public UUID asUUID ()

Gets this value as a UUID if it is one, otherwise throws exception.

Returns

an UUID.

Throws

public final boolean coercedEquals (
RealmAny other
)
public final boolean equals (
Object other
)

Two RealmAny s are .equals if and only if their contents are equal.

Parameters

  • other - compare target

Returns

true if the target has the same value

Overrides

equals in class Object

Gets the inner type of this RealmAny object.

Returns

the inner RealmAny.Type

Returns the Java class that represents the inner value wrapped by this RealmAny value.

Returns

the class that represents the inner value wrapped by this RealmAny value.

public final int hashCode ()

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

Returns

true if the target has the same value

Throws

Overrides

hashCode in class Object

public boolean isNull ()

Returns true if the inner value is null, false otherwise.

Returns

true if the inner value is null, false otherwise.

public static RealmAny nullValue ()

Creates a new RealmAny of a null value.

Returns

a new RealmAny instance of a null value.

public String toString ()

Overrides

toString in class Object

public static RealmAny valueOf (
Decimal128 value
)

Creates a new RealmAny with the specified value. If the value is not null the type will be RealmAny.Type.DECIMAL128 , RealmAny.Type.NULL otherwise.

Parameters

  • value - the RealmAny value.

Returns

a new RealmAny of a Decimal128.

public static RealmAny valueOf (
ObjectId value
)

Creates a new RealmAny with the specified value. If the value is not null the type will be RealmAny.Type.OBJECT_ID , RealmAny.Type.NULL otherwise.

Parameters

  • value - the RealmAny value.

Returns

a new RealmAny of an ObjectId.

public static RealmAny valueOf (
Date value
)

Creates a new RealmAny with the specified value. If the value is not null the type will be RealmAny.Type.DATE , RealmAny.Type.NULL otherwise.

Parameters

  • value - the RealmAny value.

Returns

a new RealmAny of a Date.

public static RealmAny valueOf (
byte[] value
)

Creates a new RealmAny with the specified value. If the value is not null the type will be RealmAny.Type.BINARY , RealmAny.Type.NULL otherwise.

Parameters

  • value - the RealmAny value.

Returns

a new RealmAny of a byte[].

public static RealmAny valueOf (
String value
)

Creates a new RealmAny with the specified value. If the value is not null the type will be RealmAny.Type.STRING , RealmAny.Type.NULL otherwise.

Parameters

  • value - the RealmAny value.

Returns

a new RealmAny of a String.

public static RealmAny valueOf (
Double value
)

Creates a new RealmAny with the specified value. If the value is not null the type will be RealmAny.Type.DOUBLE , RealmAny.Type.NULL otherwise.

Parameters

  • value - the RealmAny value.

Returns

a new RealmAny of a Double.

public static RealmAny valueOf (
Float value
)

Creates a new RealmAny with the specified value. If the value is not null the type will be RealmAny.Type.FLOAT , RealmAny.Type.NULL otherwise.

Parameters

  • value - the RealmAny value.

Returns

a new RealmAny of a Float.

public static RealmAny valueOf (
Boolean value
)

Creates a new RealmAny with the specified value. If the value is not null the type will be RealmAny.Type.BOOLEAN , RealmAny.Type.NULL otherwise.

Parameters

  • value - the RealmAny value.

Returns

a new RealmAny of a Boolean.

public static RealmAny valueOf (
Long value
)

Creates a new RealmAny with the specified value. If the value is not null the type will be RealmAny.Type.INTEGER , RealmAny.Type.NULL otherwise.

Parameters

  • value - the RealmAny value.

Returns

a new RealmAny of a Long.

public static RealmAny valueOf (
Integer value
)

Creates a new RealmAny with the specified value. If the value is not null the type will be RealmAny.Type.INTEGER , RealmAny.Type.NULL otherwise.

Parameters

  • value - the RealmAny value.

Returns

a new RealmAny of a Integer.

public static RealmAny valueOf (
Short value
)

Creates a new RealmAny with the specified value. If the value is not null the type will be RealmAny.Type.INTEGER , RealmAny.Type.NULL otherwise.

Parameters

  • value - the RealmAny value.

Returns

a new RealmAny of a Short.

public static RealmAny valueOf (
Byte value
)

Creates a new RealmAny with the specified value. If the value is not null the type will be RealmAny.Type.INTEGER , RealmAny.Type.NULL otherwise.

Parameters

  • value - the RealmAny value.

Returns

a new RealmAny containing a Byte value.

←  Interface Realm.Transaction.OnSuccessEnum RealmAny.Type →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.