Class DynamicRealmObject
- java.lang.Object
-
- io.realm.RealmObject
-
- io.realm.DynamicRealmObject
-
- All Implemented Interfaces:
io.realm.internal.ManageableObject,io.realm.internal.RealmObjectProxy,RealmModel
public class DynamicRealmObject extends RealmObject implements io.realm.internal.RealmObjectProxy
Class that wraps a normal RealmObject in order to allow dynamic access instead of a typed interface. Using a DynamicRealmObject is slower than using the regular RealmObject class.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.realm.internal.RealmObjectProxy
io.realm.internal.RealmObjectProxy.CacheData<E extends RealmModel>
-
-
Constructor Summary
Constructors Constructor Description DynamicRealmObject(RealmModel obj)Creates a dynamic Realm object based on an existing object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object o)<E> Eget(String fieldName)Returns the value for the given field.byte[]getBlob(String fieldName)Returns thebyte[]value for a given field.booleangetBoolean(String fieldName)Returns thebooleanvalue for a given field.bytegetByte(String fieldName)Returns thebytevalue for a given field.DategetDate(String fieldName)Returns theDatevalue for a given field.Decimal128getDecimal128(String fieldName)Returns theDecimal128value for a given field.RealmDictionary<DynamicRealmObject>getDictionary(String fieldName)Returns theRealmDictionaryofDynamicRealmObjects being linked from the given field.<E> RealmDictionary<E>getDictionary(String fieldName, Class<E> primitiveType)Returns theRealmDictionarycontaining only primitive values.doublegetDouble(String fieldName)Returns thedoublevalue for a given field.DynamicRealmgetDynamicRealm()ReturnsDynamicRealminstance where thisDynamicRealmObjectbelongs.String[]getFieldNames()Returns the list of field names on this object.RealmFieldTypegetFieldType(String fieldName)Returns the type used by the underlying storage engine to represent this field.floatgetFloat(String fieldName)Returns thefloatvalue for a given field.intgetInt(String fieldName)Returns theintvalue for a given field.RealmList<DynamicRealmObject>getList(String fieldName)Returns theRealmListofDynamicRealmObjects being linked from the given field.<E> RealmList<E>getList(String fieldName, Class<E> primitiveType)Returns theRealmListcontaining only primitive values.longgetLong(String fieldName)Returns thelongvalue for a given field.DynamicRealmObjectgetObject(String fieldName)Returns the object being linked to from this field.ObjectIdgetObjectId(String fieldName)Returns theObjectIdvalue for a given field.RealmAnygetRealmAny(String fieldName)Returns theRealmAnyvalue for a given field.RealmSet<DynamicRealmObject>getRealmSet(String fieldName)Returns theRealmSetofDynamicRealmObjects being linked from the given field.<E> RealmSet<E>getRealmSet(String fieldName, Class<E> primitiveType)Returns theRealmSetcontaining only primitive values.shortgetShort(String fieldName)Returns theshortvalue for a given field.StringgetString(String fieldName)Returns theStringvalue for a given field.StringgetType()Returns the type of object.UUIDgetUUID(String fieldName)Returns theUUIDvalue for a given field.booleanhasField(String fieldName)Checks whether an object has the given field or not.inthashCode()Returns a hash code value for theDynamicRealmObjectobject.booleanisNull(String fieldName)Checks if the value of a given field isnull.RealmResults<DynamicRealmObject>linkingObjects(String srcClassName, String srcFieldName)ReturnsRealmResultscontaining allsrcClassNameclass objects that have a relationship to this object fromsrcFieldNamefield.voidrealm$injectObjectContext()ProxyStaterealmGet$proxyState()voidset(String fieldName, Object value)Sets the value for the given field.voidsetBlob(String fieldName, byte[] value)Sets the binary value of the given field.voidsetBoolean(String fieldName, boolean value)Sets thebooleanvalue of the given field.voidsetByte(String fieldName, byte value)Sets thebytevalue of the given field.voidsetDate(String fieldName, Date value)Sets theDatevalue of the given field.voidsetDecimal128(String fieldName, Decimal128 value)Sets theDecimal128value of the given field.<E> voidsetDictionary(String fieldName, RealmDictionary<E> dictionary)Sets the reference to aRealmDictionaryon the given field.voidsetDouble(String fieldName, double value)Sets thedoublevalue of the given field.voidsetFloat(String fieldName, float value)Sets thefloatvalue of the given field.voidsetInt(String fieldName, int value)Sets theintvalue of the given field.<E> voidsetList(String fieldName, RealmList<E> list)Sets the reference to aRealmListon the given field.voidsetLong(String fieldName, long value)Sets thelongvalue of the given field.voidsetNull(String fieldName)Sets the value tonullfor the given field.voidsetObject(String fieldName, DynamicRealmObject value)Sets a reference to another object on the given field.voidsetObjectId(String fieldName, ObjectId value)Sets theObjectIdvalue of the given field.voidsetRealmAny(String fieldName, RealmAny value)Sets theRealmAnyvalue of the given field.<E> voidsetRealmSet(String fieldName, RealmSet<E> set)Sets the reference to aRealmSeton the given field.voidsetShort(String fieldName, short value)Sets theshortvalue of the given field.voidsetString(String fieldName, String value)Sets theStringvalue of the given field.voidsetUUID(String fieldName, UUID value)Sets theUUIDvalue of the given field.StringtoString()-
Methods inherited from class io.realm.RealmObject
addChangeListener, addChangeListener, addChangeListener, addChangeListener, asChangesetObservable, asChangesetObservable, asFlowable, asFlowable, deleteFromRealm, deleteFromRealm, freeze, freeze, getRealm, getRealm, isFrozen, isFrozen, isLoaded, isLoaded, isManaged, isManaged, isValid, isValid, load, load, removeAllChangeListeners, removeAllChangeListeners, removeChangeListener, removeChangeListener, removeChangeListener, removeChangeListener
-
-
-
-
Constructor Detail
-
DynamicRealmObject
public DynamicRealmObject(RealmModel obj)
Creates a dynamic Realm object based on an existing object.- Parameters:
obj- the Realm object to convert to a dynamic object. Only objects managed byRealmcan be used.- Throws:
IllegalArgumentException- if object isn't managed by Realm or is aDynamicRealmObjectalready.
-
-
Method Detail
-
get
public <E> E get(String fieldName)
Returns the value for the given field.- Parameters:
fieldName- name of the field.- Returns:
- the field value.
- Throws:
ClassCastException- if the field doesn't contain a field of the defined return type.
-
getBoolean
public boolean getBoolean(String fieldName)
Returns thebooleanvalue for a given field.If the field is nullable, use
isNull(String)to check fornullinstead of using this method.- Parameters:
fieldName- the name of the field.- Returns:
- the boolean value.
- Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain booleans.RealmException- if the return value would benull.
-
getInt
public int getInt(String fieldName)
Returns theintvalue for a given field.If the field is nullable, use
isNull(String)to check fornullinstead of using this method.- Parameters:
fieldName- the name of the field.- Returns:
- the int value. Integer values exceeding
Integer.MAX_VALUEwill wrap. - Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain integers.RealmException- if the return value would benull.
-
getShort
public short getShort(String fieldName)
Returns theshortvalue for a given field.If the field is nullable, use
isNull(String)to check fornullinstead of using this method.- Parameters:
fieldName- the name of the field.- Returns:
- the short value. Integer values exceeding
Short.MAX_VALUEwill wrap. - Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain integers.RealmException- if the return value would benull.
-
getLong
public long getLong(String fieldName)
Returns thelongvalue for a given field.If the field is nullable, use
isNull(String)to check fornullinstead of using this method.- Parameters:
fieldName- the name of the field.- Returns:
- the long value. Integer values exceeding
Long.MAX_VALUEwill wrap. - Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain integers.RealmException- if the return value would benull.
-
getByte
public byte getByte(String fieldName)
Returns thebytevalue for a given field.If the field is nullable, use
isNull(String)to check fornullinstead of using this method.- Parameters:
fieldName- the name of the field.- Returns:
- the byte value.
- Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain integers.RealmException- if the return value would benull.
-
getFloat
public float getFloat(String fieldName)
Returns thefloatvalue for a given field.If the field is nullable, use
isNull(String)to check fornullinstead of using this method.- Parameters:
fieldName- the name of the field.- Returns:
- the float value.
- Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain floats.RealmException- if the return value would benull.
-
getDouble
public double getDouble(String fieldName)
Returns thedoublevalue for a given field.If the field is nullable, use
isNull(String)to check fornullinstead of using this method.- Parameters:
fieldName- the name of the field.- Returns:
- the double value.
- Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain doubles.RealmException- if the return value would benull.
-
getBlob
public byte[] getBlob(String fieldName)
Returns thebyte[]value for a given field.- Parameters:
fieldName- the name of the field.- Returns:
- the byte[] value.
- Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain binary data.
-
getString
public String getString(String fieldName)
Returns theStringvalue for a given field.- Parameters:
fieldName- the name of the field.- Returns:
- the String value.
- Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain Strings.
-
getDate
public Date getDate(String fieldName)
Returns theDatevalue for a given field.- Parameters:
fieldName- the name of the field.- Returns:
- the Date value.
- Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain Dates.
-
getDecimal128
public Decimal128 getDecimal128(String fieldName)
Returns theDecimal128value for a given field.- Parameters:
fieldName- the name of the field.- Returns:
- the Decimal128 value.
- Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain Decimal128.
-
getObjectId
public ObjectId getObjectId(String fieldName)
Returns theObjectIdvalue for a given field.- Parameters:
fieldName- the name of the field.- Returns:
- the ObjectId value.
- Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain ObjectId.
-
getRealmAny
public RealmAny getRealmAny(String fieldName)
Returns theRealmAnyvalue for a given field.- Parameters:
fieldName- the name of the field.- Returns:
- the RealmAny value.
- Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain RealmAny.
-
getUUID
public UUID getUUID(String fieldName)
Returns theUUIDvalue for a given field.- Parameters:
fieldName- the name of the field.- Returns:
- the UUID value.
- Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain UUID.
-
getObject
@Nullable public DynamicRealmObject getObject(String fieldName)
Returns the object being linked to from this field.- Parameters:
fieldName- the name of the field.- Returns:
- the
DynamicRealmObjectrepresentation of the linked object ornullif no object is linked. - Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain links to other objects.
-
getList
public RealmList<DynamicRealmObject> getList(String fieldName)
Returns theRealmListofDynamicRealmObjects being linked from the given field.If the list contains primitive types, use
getList(String, Class)instead.- Parameters:
fieldName- the name of the field.- Returns:
- the
RealmListdata for this field. - Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain a list of objects.
-
getList
public <E> RealmList<E> getList(String fieldName, Class<E> primitiveType)
Returns theRealmListcontaining only primitive values.If the list contains references to other Realm objects, use
getList(String)instead.- Parameters:
fieldName- the name of the field.primitiveType- the type of elements in the list. Only primitive types are supported.- Returns:
- the
RealmListdata for this field. - Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain a list of primitive objects.
-
getDictionary
public RealmDictionary<DynamicRealmObject> getDictionary(String fieldName)
Returns theRealmDictionaryofDynamicRealmObjects being linked from the given field.If the dictionary contains primitive types, use
getDictionary(String, Class)instead.- Parameters:
fieldName- the name of the field.- Returns:
- the
RealmDictionarydata for this field. - Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain a dictionary of objects.
-
getDictionary
public <E> RealmDictionary<E> getDictionary(String fieldName, Class<E> primitiveType)
Returns theRealmDictionarycontaining only primitive values.If the dictionary contains references to other Realm objects, use
getDictionary(String)instead.- Parameters:
fieldName- the name of the field.primitiveType- the type of elements in the dictionary. Only primitive types are supported.- Returns:
- the
RealmDictionarydata for this field. - Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain a dictionary of primitive objects.
-
getRealmSet
public RealmSet<DynamicRealmObject> getRealmSet(String fieldName)
Returns theRealmSetofDynamicRealmObjects being linked from the given field.If the set contains primitive types, use
getRealmSet(String, Class)instead.- Parameters:
fieldName- the name of the field.- Returns:
- the
RealmSetdata for this field. - Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain a set of objects.
-
getRealmSet
public <E> RealmSet<E> getRealmSet(String fieldName, Class<E> primitiveType)
Returns theRealmSetcontaining only primitive values.If the set contains references to other Realm objects, use
getRealmSet(String)instead.- Parameters:
fieldName- the name of the field.primitiveType- the type of elements in the set. Only primitive types are supported.- Returns:
- the
RealmSetdata for this field. - Throws:
IllegalArgumentException- if field name doesn't exist or it doesn't contain a set of primitive objects.
-
isNull
public boolean isNull(String fieldName)
Checks if the value of a given field isnull.- Parameters:
fieldName- the name of the field.- Returns:
trueif field value is null,falseotherwise.- Throws:
IllegalArgumentException- if field name doesn't exist.
-
hasField
public boolean hasField(String fieldName)
Checks whether an object has the given field or not.- Parameters:
fieldName- field name to check.- Returns:
trueif the object has a field with the given name,falseotherwise.
-
getFieldNames
public String[] getFieldNames()
Returns the list of field names on this object.- Returns:
- list of field names on this objects or the empty list if the object doesn't have any fields.
-
set
public void set(String fieldName, Object value)
Sets the value for the given field. This method will automatically try to convert numbers and booleans that are given asStringto their appropriate type. For example"10"will be converted to10if the field type isint.Using the typed setters will be faster than using this method.
- Throws:
IllegalArgumentException- if field name doesn't exist or if the input value cannot be converted to the appropriate input type.NumberFormatException- if a String based number cannot be converted properly.RealmException- if the field is aPrimaryKeyfield.
-
setBoolean
public void setBoolean(String fieldName, boolean value)
Sets thebooleanvalue of the given field.- Parameters:
fieldName- field name to update.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't a boolean field.
-
setShort
public void setShort(String fieldName, short value)
Sets theshortvalue of the given field.- Parameters:
fieldName- field name.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't an integer field.RealmException- if the field is aPrimaryKeyfield.
-
setInt
public void setInt(String fieldName, int value)
Sets theintvalue of the given field.- Parameters:
fieldName- field name to update.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't an integer field.RealmException- if the field is aPrimaryKeyfield.
-
setLong
public void setLong(String fieldName, long value)
Sets thelongvalue of the given field.- Parameters:
fieldName- field name.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't an integer field.RealmException- if the field is aPrimaryKeyfield.
-
setByte
public void setByte(String fieldName, byte value)
Sets thebytevalue of the given field.- Parameters:
fieldName- field name.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't an integer field.RealmException- if the field is aPrimaryKeyfield.
-
setFloat
public void setFloat(String fieldName, float value)
Sets thefloatvalue of the given field.- Parameters:
fieldName- field name.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't a float field.
-
setDouble
public void setDouble(String fieldName, double value)
Sets thedoublevalue of the given field.- Parameters:
fieldName- field name.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't a double field.
-
setString
public void setString(String fieldName, @Nullable String value)
Sets theStringvalue of the given field.- Parameters:
fieldName- field name.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't a String field.RealmException- if the field is aPrimaryKeyfield.
-
setBlob
public void setBlob(String fieldName, @Nullable byte[] value)
Sets the binary value of the given field.- Parameters:
fieldName- field name.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't a binary field.
-
setDate
public void setDate(String fieldName, @Nullable Date value)
Sets theDatevalue of the given field.- Parameters:
fieldName- field name.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't a Date field.
-
setDecimal128
public void setDecimal128(String fieldName, @Nullable Decimal128 value)
Sets theDecimal128value of the given field.- Parameters:
fieldName- field name.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't a Decimal128 field.
-
setObjectId
public void setObjectId(String fieldName, @Nullable ObjectId value)
Sets theObjectIdvalue of the given field.- Parameters:
fieldName- field name.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't a ObjectId field.
-
setRealmAny
public void setRealmAny(String fieldName, @Nullable RealmAny value)
Sets theRealmAnyvalue of the given field.- Parameters:
fieldName- field name.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't a RealmAny field.
-
setUUID
public void setUUID(String fieldName, @Nullable UUID value)
Sets theUUIDvalue of the given field.- Parameters:
fieldName- field name.value- value to insert.- Throws:
IllegalArgumentException- if field name doesn't exist or field isn't a UUID field.
-
setObject
public void setObject(String fieldName, @Nullable DynamicRealmObject value)
Sets a reference to another object on the given field.- Parameters:
fieldName- field name.value- object to link to.- Throws:
IllegalArgumentException- if field name doesn't exist, it doesn't link to other Realm objects, the type of DynamicRealmObject doesn't match or it belongs to a different Realm.
-
setList
public <E> void setList(String fieldName, RealmList<E> list)
Sets the reference to aRealmListon the given field.This will copy all the elements in the list into Realm, but any further changes to the list will not be reflected in the Realm. Use
getList(String)in order to get a reference to the managed list.- Parameters:
fieldName- field name.list- list of objects. Must either be primitive types orDynamicRealmObjects.- Throws:
IllegalArgumentException- if field name doesn't exist, it is not a list field, the objects in the list doesn't match the expected type or any Realm object in the list belongs to a different Realm.
-
setDictionary
public <E> void setDictionary(String fieldName, RealmDictionary<E> dictionary)
Sets the reference to aRealmDictionaryon the given field.This will copy all the elements in the dictionary into Realm, but any further changes to the dictionary will not be reflected in the Realm. Use
getDictionary(String)in order to get a reference to the managed dictionary.- Parameters:
fieldName- field name.dictionary- dictionary of objects. Must either be primitive types orDynamicRealmObjects.- Throws:
IllegalArgumentException- if field name doesn't exist, it is not a dictionary field, the objects in the dictionary doesn't match the expected type or any Realm object in the dictionary belongs to a different Realm.
-
setRealmSet
public <E> void setRealmSet(String fieldName, RealmSet<E> set)
Sets the reference to aRealmSeton the given field.This will copy all the elements in the set into Realm, but any further changes to the set will not be reflected in the Realm. Use
getRealmSet(String)in order to get a reference to the managed set.- Parameters:
fieldName- field name.set- set of objects. Must either be primitive types orDynamicRealmObjects.- Throws:
IllegalArgumentException- if field name doesn't exist, it is not a set field, the objects in the set doesn't match the expected type or any Realm object in the set belongs to a different Realm.
-
setNull
public void setNull(String fieldName)
Sets the value tonullfor the given field.- Parameters:
fieldName- field name.- Throws:
IllegalArgumentException- if field name doesn't exist, or the field isn't nullable.RealmException- if the field is aPrimaryKeyfield.
-
getType
public String getType()
Returns the type of object. This will normally correspond to the name of a class that is extendingRealmObject.- Returns:
- this objects type.
-
getFieldType
public RealmFieldType getFieldType(String fieldName)
Returns the type used by the underlying storage engine to represent this field.- Returns:
- the underlying type used by Realm to represent this field.
-
hashCode
public int hashCode()
Returns a hash code value for theDynamicRealmObjectobject.By the general contract of
Object.hashCode(), any two objects for whichequals(java.lang.Object)returnstruemust return the same hash code value.Note that a
RealmObjectis a live object, and it might be updated by changes from other threads. This means that a hash code value of the object is not stable, and the value should be neither used as a key in HashMap nor saved in HashSet.- Overrides:
hashCodein classObject- Returns:
- a hash code value for the object.
- See Also:
equals(java.lang.Object)
-
linkingObjects
public RealmResults<DynamicRealmObject> linkingObjects(String srcClassName, String srcFieldName)
ReturnsRealmResultscontaining allsrcClassNameclass objects that have a relationship to this object fromsrcFieldNamefield.An entry is added for each reference, e.g. if the same reference is in a list multiple times, the src object will show up here multiple times.
- Parameters:
srcClassName- name of the class returned objects belong to.srcFieldName- name of the field in the source class that holds a reference to this object. Field type must be eitherio.realm.RealmFieldType.OBJECTorio.realm.RealmFieldType.LIST.- Returns:
- the result.
- Throws:
IllegalArgumentException- if thesrcClassNameisnullor does not exist, thesrcFieldNameisnullor does not exist, type of the source field is not supported.
-
getDynamicRealm
public DynamicRealm getDynamicRealm()
ReturnsDynamicRealminstance where thisDynamicRealmObjectbelongs.You must not call
Closeable.close()against returned instance.- Returns:
DynamicRealminstance where this object belongs.- Throws:
IllegalStateException- if this object was deleted or the correspondingDynamicRealmwas already closed.
-
realm$injectObjectContext
public void realm$injectObjectContext()
- Specified by:
realm$injectObjectContextin interfaceio.realm.internal.RealmObjectProxy
-
realmGet$proxyState
public ProxyState realmGet$proxyState()
- Specified by:
realmGet$proxyStatein interfaceio.realm.internal.RealmObjectProxy
-
-