Package io.realm

Class 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.
    • 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 by Realm can be used.
        Throws:
        IllegalArgumentException - if object isn't managed by Realm or is a DynamicRealmObject already.
    • 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 the boolean value for a given field.

        If the field is nullable, use isNull(String) to check for null instead 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 be null.
      • getInt

        public int getInt​(String fieldName)
        Returns the int value for a given field.

        If the field is nullable, use isNull(String) to check for null instead of using this method.

        Parameters:
        fieldName - the name of the field.
        Returns:
        the int value. Integer values exceeding Integer.MAX_VALUE will wrap.
        Throws:
        IllegalArgumentException - if field name doesn't exist or it doesn't contain integers.
        RealmException - if the return value would be null.
      • getShort

        public short getShort​(String fieldName)
        Returns the short value for a given field.

        If the field is nullable, use isNull(String) to check for null instead of using this method.

        Parameters:
        fieldName - the name of the field.
        Returns:
        the short value. Integer values exceeding Short.MAX_VALUE will wrap.
        Throws:
        IllegalArgumentException - if field name doesn't exist or it doesn't contain integers.
        RealmException - if the return value would be null.
      • getLong

        public long getLong​(String fieldName)
        Returns the long value for a given field.

        If the field is nullable, use isNull(String) to check for null instead of using this method.

        Parameters:
        fieldName - the name of the field.
        Returns:
        the long value. Integer values exceeding Long.MAX_VALUE will wrap.
        Throws:
        IllegalArgumentException - if field name doesn't exist or it doesn't contain integers.
        RealmException - if the return value would be null.
      • getByte

        public byte getByte​(String fieldName)
        Returns the byte value for a given field.

        If the field is nullable, use isNull(String) to check for null instead 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 be null.
      • getFloat

        public float getFloat​(String fieldName)
        Returns the float value for a given field.

        If the field is nullable, use isNull(String) to check for null instead 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 be null.
      • getDouble

        public double getDouble​(String fieldName)
        Returns the double value for a given field.

        If the field is nullable, use isNull(String) to check for null instead 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 be null.
      • getBlob

        public byte[] getBlob​(String fieldName)
        Returns the byte[] 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 the String value 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 the Date value 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 the Decimal128 value 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 the ObjectId value 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 the RealmAny value 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 the UUID value 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 DynamicRealmObject representation of the linked object or null if no object is linked.
        Throws:
        IllegalArgumentException - if field name doesn't exist or it doesn't contain links to other objects.
      • getList

        public <E> RealmList<E> getList​(String fieldName,
                                        Class<E> primitiveType)
        Returns the RealmList containing 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 RealmList data for this field.
        Throws:
        IllegalArgumentException - if field name doesn't exist or it doesn't contain a list of primitive objects.
      • getDictionary

        public <E> RealmDictionary<E> getDictionary​(String fieldName,
                                                    Class<E> primitiveType)
        Returns the RealmDictionary containing 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 RealmDictionary data for this field.
        Throws:
        IllegalArgumentException - if field name doesn't exist or it doesn't contain a dictionary of primitive objects.
      • getRealmSet

        public <E> RealmSet<E> getRealmSet​(String fieldName,
                                           Class<E> primitiveType)
        Returns the RealmSet containing 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 RealmSet data 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 is null.
        Parameters:
        fieldName - the name of the field.
        Returns:
        true if field value is null, false otherwise.
        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:
        true if the object has a field with the given name, false otherwise.
      • 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 as String to their appropriate type. For example "10" will be converted to 10 if the field type is int.

        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 a PrimaryKey field.
      • setBoolean

        public void setBoolean​(String fieldName,
                               boolean value)
        Sets the boolean value 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 the short 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 an integer field.
        RealmException - if the field is a PrimaryKey field.
      • setInt

        public void setInt​(String fieldName,
                           int value)
        Sets the int value 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 a PrimaryKey field.
      • setLong

        public void setLong​(String fieldName,
                            long value)
        Sets the long 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 an integer field.
        RealmException - if the field is a PrimaryKey field.
      • setByte

        public void setByte​(String fieldName,
                            byte value)
        Sets the byte 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 an integer field.
        RealmException - if the field is a PrimaryKey field.
      • setFloat

        public void setFloat​(String fieldName,
                             float value)
        Sets the float 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 float field.
      • setDouble

        public void setDouble​(String fieldName,
                              double value)
        Sets the double 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 double field.
      • 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 the Date 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 Date field.
      • setDecimal128

        public void setDecimal128​(String fieldName,
                                  @Nullable
                                  Decimal128 value)
        Sets the Decimal128 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 Decimal128 field.
      • setObjectId

        public void setObjectId​(String fieldName,
                                @Nullable
                                ObjectId value)
        Sets the ObjectId 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 ObjectId field.
      • setRealmAny

        public void setRealmAny​(String fieldName,
                                @Nullable
                                RealmAny value)
        Sets the RealmAny 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 RealmAny field.
      • setUUID

        public void setUUID​(String fieldName,
                            @Nullable
                            UUID value)
        Sets the UUID 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 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 a RealmList on 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 or DynamicRealmObjects.
        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 a RealmDictionary on 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 or DynamicRealmObjects.
        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 a RealmSet on 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 or DynamicRealmObjects.
        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 to null for 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 a PrimaryKey field.
      • getType

        public String getType()
        Returns the type of object. This will normally correspond to the name of a class that is extending RealmObject.
        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 the DynamicRealmObject object.

        By the general contract of Object.hashCode(), any two objects for which equals(java.lang.Object) returns true must return the same hash code value.

        Note that a RealmObject is 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:
        hashCode in class Object
        Returns:
        a hash code value for the object.
        See Also:
        equals(java.lang.Object)
      • linkingObjects

        public RealmResults<DynamicRealmObject> linkingObjects​(String srcClassName,
                                                               String srcFieldName)
        Returns RealmResults containing all srcClassName class objects that have a relationship to this object from srcFieldName field.

        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 either io.realm.RealmFieldType.OBJECT or io.realm.RealmFieldType.LIST.
        Returns:
        the result.
        Throws:
        IllegalArgumentException - if the srcClassName is null or does not exist, the srcFieldName is null or does not exist, type of the source field is not supported.
      • realm$injectObjectContext

        public void realm$injectObjectContext()
        Specified by:
        realm$injectObjectContext in interface io.realm.internal.RealmObjectProxy
      • realmGet$proxyState

        public ProxyState realmGet$proxyState()
        Specified by:
        realmGet$proxyState in interface io.realm.internal.RealmObjectProxy