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);
Es crucial entender que el acto de extraer un valor de un tipo específico requiere un conocimiento definido sobre el tipo almacenado. Invocar un método getter para cualquier tipo en particular, que no sea del mismo tipo que el valor almacenado, puede provocar una excepción. Nuestra recomendación para gestionar el polimorfismo RealmAny es guardar un caso switch alrededor del tipo RealmAny y su clase de valor interno.
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() retorna la clase Java que representa el valor interno envuelto por la instancia RealmAny. Si la clase resultante es una realización de io.realm.RealmModel, se puede llamar asRealmModel() para convertir el valor de RealmAny en una referencia de objeto Realm.
RealmAny values can also be sorted. The sorting order used between different RealmAny types, from lowest to highest, is:
Booleano
Byte/Short/Integer/Long/Float/Double/Decimal128
byte[]/String
fecha
ObjectId
UUID
RealmObject
Esto tiene implicaciones en el funcionamiento de RealmQuery.sort(String), RealmQuery.minRealmAny(String) y RealmQuery.maxRealmAny(String). Especialmente min() y max() no solo tendrá en cuenta los campos numéricos, sino que utilizará el orden de clasificación para determinar el valor "más grande" o "más bajo".
Nested Class Summary
Modificador y Tipo | Clase y descripción |
|---|---|
|
Resumen del método
Modificador y Tipo | Método y descripción |
|---|---|
public byte | asBinary () Obtiene este valor como un byte[] si lo es, de lo contrario lanza una excepción. |
public Boolean | asBoolean () Gets this value as a Boolean if it is one, otherwise throws exception. |
public Byte | asByte () Obtiene este valor como un Byte si lo es; de lo contrario, lanza una excepción. |
public Date | asDate () Gets this value as a Date if it is one, otherwise throws exception. |
público Decimal128 | asDecimal128 () Obtiene este valor como un Decimal128 si lo es, de lo contrario lanza una excepción. |
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 () Obtiene este valor como Integer si es uno, de lo contrario lanza excepciones. |
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 () Obtiene este valor como Short si lo es, de lo contrario, lanza una excepción. |
public String | asString () Obtiene este valor como una cadena si es uno, de lo contrario arroja una excepción. |
public UUID | asUUID () Gets this value as a UUID if it is one, otherwise throws exception. |
public final boolean | |
public final boolean | |
público RealmAny.Type | getType () Obtiene el tipo interno de este objeto RealmAny. |
public Class | Returns the Java class that represents the inner value wrapped by this RealmAny value. |
public final int | hashCode () A |
public booleano | 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 | Crea un nuevo RealmAny con el valor especificado. |
public static RealmAny | Crea un nuevo RealmAny con el valor especificado. |
public static RealmAny | |
public static RealmAny | Crea un nuevo RealmAny con el valor especificado. |
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
Detalle del método
asBinary
public byte asBinary () |
|---|
Obtiene este valor como un byte[] si lo es, de lo contrario lanza una excepción. Devuelve a byte[]. Throws
|
asBoolean
Gets this value as a Boolean if it is one, otherwise throws exception. Devuelve a Boolean. Throws
|
asByte
Obtiene este valor como un Byte si lo es; de lo contrario, lanza una excepción. Devuelve a Byte. Throws
|
asDate
Gets this value as a Date if it is one, otherwise throws exception. Devuelve a Date. Throws
|
asDecimal128
public Decimal128 asDecimal128 () |
|---|
Obtiene este valor como un Decimal128 si lo es, de lo contrario lanza una excepción. Devuelve a Decimal128. Throws
|
comoDoble
Gets this value as a Double if it is one, otherwise throws exception. Devuelve un Doble. Throws
|
asFloat
Gets this value as a Float if it is one, otherwise throws exception. Devuelve un flotador. Throws
|
asInteger
Obtiene este valor como Integer si es uno, de lo contrario lanza excepciones. Devuelve an Integer. Throws
|
asLong
Gets this value as a Long if it is one, otherwise throws exception. Devuelve a Long. Throws
|
asObjectId
public ObjectId asObjectId () |
|---|
Gets this value as a ObjectId if it is one, otherwise throws exception. Devuelve an ObjectId. Throws
|
asRealmModel
Gets this value as a RealmModel if it is one, otherwise throws exception. Type Parameters
Devuelve a RealmModel of the T type. Throws
|
asShort
Obtiene este valor como Short si lo es, de lo contrario, lanza una excepción. Devuelve un Short. Throws
|
comoCadena
Obtiene este valor como una cadena si es uno, de lo contrario arroja una excepción. Devuelve Una string. Throws
|
asUUID
Gets this value as a UUID if it is one, otherwise throws exception. Devuelve un UUID. Throws
|
coercedEquals
equals
getType
public RealmAny.Type getType () |
|---|
Obtiene el tipo interno de este objeto RealmAny. Devuelve the inner RealmAny.Type |
getValueClass
public Class getValueClass () |
|---|
Returns the Java class that represents the inner value wrapped by this RealmAny value. Devuelve la clase que representa el valor interno envuelto por este valor RealmAny. |
hashCode
public final int hashCode () |
|---|
A Devuelve verdadero si el objetivo tiene el mismo valor Throws
Anulaciones
|
isNull
public boolean isNull () |
|---|
Returns true if the inner value is null, false otherwise. Devuelve Verdadero si el valor interno es nulo, falso en caso contrario. |
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. Parámetros
Devuelve 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. Parámetros
Devuelve un nuevo RealmAny de un 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. Parámetros
Devuelve a new RealmAny of a Date. |
Crea un nuevo RealmAny con el valor especificado. Si el valor no es nulo, el tipo será RealmAny.Type.BINARY; en caso contrario, RealmAny.Type.NULL. Parámetros
Devuelve a new RealmAny of a byte[]. |
Crea un nuevo RealmAny con el valor especificado. Si el valor no es nulo, el tipo será RealmAny.Type.STRING , RealmAny.Type.NULL en caso contrario. Parámetros
Devuelve 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. Parámetros
Devuelve a new RealmAny of a Double. |
Crea un nuevo RealmAny con el valor especificado. Si el valor no es nulo, el tipo será RealmAny.Type.FLOAT , si no, será RealmAny.Type.NULL . Parámetros
Devuelve 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. Parámetros
Devuelve a new RealmAny of a Boolean. |
Crea un nuevo RealmAny con el valor especificado. Si el valor no es nulo, el tipo será RealmAny.Type.INTEGER , en caso contrario, RealmAny.Type.NULL. Parámetros
Devuelve a new RealmAny of a Long. |
Crea un nuevo RealmAny con el valor especificado. Si el valor no es nulo, el tipo será RealmAny.Type.INTEGER , en caso contrario, RealmAny.Type.NULL. Parámetros
Devuelve un nuevo ReinoCualquiera de un entero. |
Crea un nuevo RealmAny con el valor especificado. Si el valor no es nulo, el tipo será RealmAny.Type.INTEGER , en caso contrario, RealmAny.Type.NULL. Parámetros
Devuelve a new RealmAny of a Short. |
Crea un nuevo RealmAny con el valor especificado. Si el valor no es nulo, el tipo será RealmAny.Type.INTEGER , en caso contrario, RealmAny.Type.NULL. Parámetros
Devuelve a new RealmAny containing a Byte value. |