Class RealmAny
On this page
io.realm
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:
Boolean
Byte/Short/Integer/Long/Float/Double/Decimal128
byte[]/String
Date
ObjectId
UUID
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.
Nested Class Summary
Modifier and Type | Class and Description |
---|---|
public static final |
Method Summary
Modifier and Type | Method and Description |
---|---|
public byte | asBinary () Gets this value as a byte[] if it is one, otherwise throws exception. |
public Boolean | asBoolean () Gets this value as a Boolean if it is one, otherwise throws exception. |
public Byte | asByte () Gets this value as a Byte if it is one, otherwise throws exception. |
public Date | asDate () Gets this value as a Date if it is one, otherwise throws exception. |
public Decimal128 | asDecimal128 () Gets this value as a Decimal128 if it is one, otherwise throws exception. |
public Double | asDouble () Gets this value as a Double if it is one, otherwise throws exception. |
public Float | asFloat () Gets this value as a Float if it is one, otherwise throws exception. |
public Integer | asInteger () Gets this value as a Integer if it is one, otherwise throws exception. |
public Long | asLong () Gets this value as a Long if it is one, otherwise throws exception. |
public ObjectId | asObjectId () 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 | asShort () Gets this value as a Short if it is one, otherwise throws exception. |
public String | asString () Gets this value as a String if it is one, otherwise throws exception. |
public UUID | asUUID () Gets this value as a UUID if it is one, otherwise throws exception. |
public final boolean | |
public final boolean | |
public RealmAny.Type | getType () 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 | hashCode () A |
public boolean | isNull () Returns true if the inner value is null, false otherwise. |
public static RealmAny | nullValue () Creates a new RealmAny of a null value. |
public String | toString () |
public static RealmAny | Creates a new RealmAny with the specified value. |
public static RealmAny | Creates a new RealmAny with the specified value. |
public static RealmAny | |
public static RealmAny | Creates a new RealmAny with the specified value. |
public static RealmAny | |
public static RealmAny | |
public static RealmAny | |
public static RealmAny | |
public static RealmAny | |
public static RealmAny | |
public static RealmAny | |
public static RealmAny |
Inherited Methods
Methods inherited from class java.lang.Object :
getClass
,hashCode
,equals
,clone
,toString
,notify
,notifyAll
,wait
,wait
,wait
,finalize
Method Detail
asBinary
public byte asBinary () |
---|
Gets this value as a byte[] if it is one, otherwise throws exception. Returns a byte[]. Throws
|
asBoolean
Gets this value as a Boolean if it is one, otherwise throws exception. Returns a Boolean. Throws
|
asByte
Gets this value as a Byte if it is one, otherwise throws exception. Returns a Byte. Throws
|
asDate
Gets this value as a Date if it is one, otherwise throws exception. Returns a Date. Throws
|
asDecimal128
public Decimal128 asDecimal128 () |
---|
Gets this value as a Decimal128 if it is one, otherwise throws exception. Returns a Decimal128. Throws
|
asDouble
Gets this value as a Double if it is one, otherwise throws exception. Returns a Double. Throws
|
asFloat
Gets this value as a Float if it is one, otherwise throws exception. Returns a Float. Throws
|
asInteger
Gets this value as a Integer if it is one, otherwise throws exception. Returns an Integer. Throws
|
asLong
Gets this value as a Long if it is one, otherwise throws exception. Returns a Long. Throws
|
asObjectId
public ObjectId asObjectId () |
---|
Gets this value as a ObjectId if it is one, otherwise throws exception. Returns an ObjectId. Throws
|
asRealmModel
Gets this value as a RealmModel if it is one, otherwise throws exception. Type Parameters
Returns a RealmModel of the T type. Throws
|
asShort
Gets this value as a Short if it is one, otherwise throws exception. Returns a Short. Throws
|
asString
Gets this value as a String if it is one, otherwise throws exception. Returns a String. Throws
|
asUUID
Gets this value as a UUID if it is one, otherwise throws exception. Returns an UUID. Throws
|
coercedEquals
equals
getType
public RealmAny.Type getType () |
---|
Gets the inner type of this RealmAny object. Returns the inner RealmAny.Type |
getValueClass
public Class getValueClass () |
---|
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. |
hashCode
public final int hashCode () |
---|
A Returns true if the target has the same value Throws
Overrides
|
isNull
public boolean isNull () |
---|
Returns true if the inner value is null, false otherwise. Returns true if the inner value is null, false otherwise. |
nullValue
toString
valueOf
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
Returns a new RealmAny of a Decimal128. |
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
Returns a new RealmAny of an ObjectId. |
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
Returns a new RealmAny of a Date. |
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
Returns a new RealmAny of a byte[]. |
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
Returns a new RealmAny of a String. |
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
Returns a new RealmAny of a Double. |
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
Returns a new RealmAny of a Float. |
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
Returns a new RealmAny of a Boolean. |
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
Returns a new RealmAny of a Long. |
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
Returns a new RealmAny of a Integer. |
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
Returns a new RealmAny of a Short. |
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
Returns a new RealmAny containing a Byte value. |