Class RealmAny
- java.lang.Object
-
- io.realm.RealmAny
-
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);
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:
- Boolean
- Byte/Short/Integer/Long/Float/Double/Decimal128
- byte[]/String
- Date
- ObjectId
- UUID
- RealmObject
RealmQuery.sort(String)
,RealmQuery.minRealmAny(String)
andRealmQuery.maxRealmAny(String)
work. Especiallymin()
andmax()
will not only take numeric fields into account, but will use the sorting order to determine the "largest" or "lowest" value.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RealmAny.Type
Enum describing all the types supported by RealmAny.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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>
TasRealmModel(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)
TwoRealmAny
s 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()
ARealmAny
'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(byte[] value)
Creates a new RealmAny with the specified value.static RealmAny
valueOf(RealmModel value)
Creates a new RealmAny with the specified value.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(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(Short value)
Creates a new RealmAny with the specified value.static RealmAny
valueOf(String 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(UUID 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(ObjectId value)
Creates a new RealmAny with the specified value.
-
-
-
Method Detail
-
getType
public RealmAny.Type getType()
Gets the inner type of this RealmAny object.- Returns:
- the inner RealmAny.Type
-
getValueClass
@Nullable 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.
-
valueOf
public static RealmAny valueOf(@Nullable Byte value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.INTEGER
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny containing a Byte value.
-
valueOf
public static RealmAny valueOf(@Nullable Short value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.INTEGER
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of a Short.
-
valueOf
public static RealmAny valueOf(@Nullable Integer value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.INTEGER
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of a Integer.
-
valueOf
public static RealmAny valueOf(@Nullable Long value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.INTEGER
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of a Long.
-
valueOf
public static RealmAny valueOf(@Nullable Boolean value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.BOOLEAN
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of a Boolean.
-
valueOf
public static RealmAny valueOf(@Nullable Float value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.FLOAT
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of a Float.
-
valueOf
public static RealmAny valueOf(@Nullable Double value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.DOUBLE
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of a Double.
-
valueOf
public static RealmAny valueOf(@Nullable String value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.STRING
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of a String.
-
valueOf
public static RealmAny valueOf(@Nullable byte[] value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.BINARY
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of a byte[].
-
valueOf
public static RealmAny valueOf(@Nullable Date value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.DATE
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of a Date.
-
valueOf
public static RealmAny valueOf(@Nullable ObjectId value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.OBJECT_ID
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of an ObjectId.
-
valueOf
public static RealmAny valueOf(@Nullable Decimal128 value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.DECIMAL128
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of a Decimal128.
-
valueOf
public static RealmAny valueOf(@Nullable UUID value)
Creates a new RealmAny with the specified value. If the value is not null the type will beRealmAny.Type.UUID
,RealmAny.Type.NULL
otherwise.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of an UUID.
-
nullValue
public static RealmAny nullValue()
Creates a new RealmAny of a null value.- Returns:
- a new RealmAny instance of a null value.
-
valueOf
public static RealmAny valueOf(@Nullable RealmModel value)
Creates a new RealmAny with the specified value.- Parameters:
value
- the RealmAny value.- Returns:
- a new RealmAny of a RealmModel.
-
isNull
public boolean isNull()
Returns true if the inner value is null, false otherwise.- Returns:
- true if the inner value is null, false otherwise.
-
asByte
public Byte asByte()
Gets this value as a Byte if it is one, otherwise throws exception.- Returns:
- a Byte.
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asShort
public Short asShort()
Gets this value as a Short if it is one, otherwise throws exception.- Returns:
- a Short.
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asInteger
public Integer asInteger()
Gets this value as a Integer if it is one, otherwise throws exception.- Returns:
- an Integer.
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asLong
public Long asLong()
Gets this value as a Long if it is one, otherwise throws exception.- Returns:
- a Long.
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asBoolean
public Boolean asBoolean()
Gets this value as a Boolean if it is one, otherwise throws exception.- Returns:
- a Boolean.
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asFloat
public Float asFloat()
Gets this value as a Float if it is one, otherwise throws exception.- Returns:
- a Float.
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asDouble
public Double asDouble()
Gets this value as a Double if it is one, otherwise throws exception.- Returns:
- a Double.
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asString
public String asString()
Gets this value as a String if it is one, otherwise throws exception.- Returns:
- a String.
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asBinary
public byte[] asBinary()
Gets this value as a byte[] if it is one, otherwise throws exception.- Returns:
- a byte[].
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asDate
public Date asDate()
Gets this value as a Date if it is one, otherwise throws exception.- Returns:
- a Date.
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asObjectId
public ObjectId asObjectId()
Gets this value as a ObjectId if it is one, otherwise throws exception.- Returns:
- an ObjectId.
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asUUID
public UUID asUUID()
Gets this value as a UUID if it is one, otherwise throws exception.- Returns:
- an UUID.
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asDecimal128
public Decimal128 asDecimal128()
Gets this value as a Decimal128 if it is one, otherwise throws exception.- Returns:
- a Decimal128.
- Throws:
ClassCastException
- if this value is not of the expected type.
-
asRealmModel
public <T extends RealmModel> T asRealmModel(Class<T> clazz)
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:
ClassCastException
- if this value is not of the expected type.
-
hashCode
public final int hashCode()
ARealmAny
's hash code is, exactly, the hash code of its value.- Overrides:
hashCode
in classObject
- Returns:
- true if the target has the same value
- Throws:
NullPointerException
- if the inner value is null
-
equals
public final boolean equals(@Nullable Object other)
TwoRealmAny
s are.equals
if and only if their contents are equal.
-
-