public class RealmAny extends Object
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 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 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:
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 |
|---|---|
static class |
RealmAny.Type
Enum describing all the types supported by RealmAny.
|
| Modifier and Type | Method and Description |
|---|---|
byte[] |
asBinary()
Gets this value as a byte[] if it is one, otherwise throws exception.
|
Boolean |
asBoolean()
Gets this value as a Boolean if it is one, otherwise throws exception.
|
Byte |
asByte()
Gets this value as a Byte if it is one, otherwise throws exception.
|
Date |
asDate()
Gets this value as a Date if it is one, otherwise throws exception.
|
Decimal128 |
asDecimal128()
Gets this value as a Decimal128 if it is one, otherwise throws exception.
|
Double |
asDouble()
Gets this value as a Double if it is one, otherwise throws exception.
|
Float |
asFloat()
Gets this value as a Float if it is one, otherwise throws exception.
|
Integer |
asInteger()
Gets this value as a Integer if it is one, otherwise throws exception.
|
Long |
asLong()
Gets this value as a Long if it is one, otherwise throws exception.
|
ObjectId |
asObjectId()
Gets this value as a ObjectId if it is one, otherwise throws exception.
|
<T extends RealmModel> |
asRealmModel(Class<T> clazz)
Gets this value as a RealmModel if it is one, otherwise throws exception.
|
Short |
asShort()
Gets this value as a Short if it is one, otherwise throws exception.
|
String |
asString()
Gets this value as a String if it is one, otherwise throws exception.
|
UUID |
asUUID()
Gets this value as a UUID if it is one, otherwise throws exception.
|
boolean |
coercedEquals(RealmAny other) |
boolean |
equals(Object other)
Two
RealmAnys are .equals if and only if their contents are equal. |
RealmAny.Type |
getType()
Gets the inner type of this RealmAny object.
|
Class<?> |
getValueClass()
Returns the Java class that represents the inner value wrapped by this RealmAny value.
|
int |
hashCode()
A
RealmAny's hash code is, exactly, the hash code of its value. |
boolean |
isNull()
Returns true if the inner value is null, false otherwise.
|
static RealmAny |
nullValue()
Creates a new RealmAny of a null value.
|
String |
toString() |
static RealmAny |
valueOf(Boolean value)
Creates a new RealmAny with the specified value.
|
static RealmAny |
valueOf(Byte value)
Creates a new RealmAny with the specified value.
|
static RealmAny |
valueOf(byte[] value)
Creates a new RealmAny with the specified value.
|
static RealmAny |
valueOf(Date value)
Creates a new RealmAny with the specified value.
|
static RealmAny |
valueOf(Decimal128 value)
Creates a new RealmAny with the specified value.
|
static RealmAny |
valueOf(Double value)
Creates a new RealmAny with the specified value.
|
static RealmAny |
valueOf(Float value)
Creates a new RealmAny with the specified value.
|
static RealmAny |
valueOf(Integer value)
Creates a new RealmAny with the specified value.
|
static RealmAny |
valueOf(Long value)
Creates a new RealmAny with the specified value.
|
static RealmAny |
valueOf(ObjectId value)
Creates a new RealmAny with the specified value.
|
static RealmAny |
valueOf(Short value)
Creates a new RealmAny with the specified value.
|
static RealmAny |
valueOf(String value)
Creates a new RealmAny with the specified value.
|
public RealmAny.Type getType()
public Class<?> getValueClass()
public static RealmAny valueOf(Byte value)
RealmAny.Type.INTEGER, RealmAny.Type.NULL otherwise.value - the RealmAny value.public static RealmAny valueOf(Short value)
RealmAny.Type.INTEGER, RealmAny.Type.NULL otherwise.value - the RealmAny value.public static RealmAny valueOf(Integer value)
RealmAny.Type.INTEGER, RealmAny.Type.NULL otherwise.value - the RealmAny value.public static RealmAny valueOf(Long value)
RealmAny.Type.INTEGER, RealmAny.Type.NULL otherwise.value - the RealmAny value.public static RealmAny valueOf(Boolean value)
RealmAny.Type.BOOLEAN, RealmAny.Type.NULL otherwise.value - the RealmAny value.public static RealmAny valueOf(Float value)
RealmAny.Type.FLOAT, RealmAny.Type.NULL otherwise.value - the RealmAny value.public static RealmAny valueOf(Double value)
RealmAny.Type.DOUBLE, RealmAny.Type.NULL otherwise.value - the RealmAny value.public static RealmAny valueOf(String value)
RealmAny.Type.STRING, RealmAny.Type.NULL otherwise.value - the RealmAny value.public static RealmAny valueOf(byte[] value)
RealmAny.Type.BINARY, RealmAny.Type.NULL otherwise.value - the RealmAny value.public static RealmAny valueOf(Date value)
RealmAny.Type.DATE, RealmAny.Type.NULL otherwise.value - the RealmAny value.public static RealmAny valueOf(ObjectId value)
RealmAny.Type.OBJECT_ID, RealmAny.Type.NULL otherwise.value - the RealmAny value.public static RealmAny valueOf(Decimal128 value)
RealmAny.Type.DECIMAL128, RealmAny.Type.NULL otherwise.value - the RealmAny value.public static RealmAny nullValue()
public boolean isNull()
public Byte asByte()
ClassCastException - if this value is not of the expected type.public Short asShort()
ClassCastException - if this value is not of the expected type.public Integer asInteger()
ClassCastException - if this value is not of the expected type.public Long asLong()
ClassCastException - if this value is not of the expected type.public Boolean asBoolean()
ClassCastException - if this value is not of the expected type.public Float asFloat()
ClassCastException - if this value is not of the expected type.public Double asDouble()
ClassCastException - if this value is not of the expected type.public String asString()
ClassCastException - if this value is not of the expected type.public byte[] asBinary()
ClassCastException - if this value is not of the expected type.public Date asDate()
ClassCastException - if this value is not of the expected type.public ObjectId asObjectId()
ClassCastException - if this value is not of the expected type.public UUID asUUID()
ClassCastException - if this value is not of the expected type.public Decimal128 asDecimal128()
ClassCastException - if this value is not of the expected type.public <T extends RealmModel> T asRealmModel(Class<T> clazz)
T - the RealmModel type to cast the inner value to.ClassCastException - if this value is not of the expected type.public final int hashCode()
RealmAny's hash code is, exactly, the hash code of its value.hashCode in class ObjectNullPointerException - if the inner value is nullpublic final boolean equals(Object other)
RealmAnys are .equals if and only if their contents are equal.public final boolean coercedEquals(RealmAny other)