Class RealmObjectSchema
- java.lang.Object
-
- io.realm.RealmObjectSchema
-
public abstract class RealmObjectSchema extends Object
Class for interacting with the schema for a given RealmObject class. This makes it possible to inspect, add, delete or change the fields for given class.If this
RealmObjectSchema
is retrieved from an immutableRealmSchema
, thisRealmObjectSchema
will be immutable as well.- See Also:
RealmMigration
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
RealmObjectSchema.Function
Function interface, used when traversing all objects of the current class and apply a function on each.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract RealmObjectSchema
addField(String fieldName, Class<?> fieldType, FieldAttribute... attributes)
Adds a new simple field to the RealmObject class.abstract RealmObjectSchema
addIndex(String fieldName)
Adds an index to a given field.abstract RealmObjectSchema
addPrimaryKey(String fieldName)
Adds a primary key to a given field.abstract RealmObjectSchema
addRealmDictionaryField(String fieldName, RealmObjectSchema objectSchema)
Adds a new field that contains aRealmDictionary
with references to other Realm model classes.abstract RealmObjectSchema
addRealmDictionaryField(String fieldName, Class<?> primitiveType)
Adds a new field that references aRealmDictionary
with primitive values.abstract RealmObjectSchema
addRealmListField(String fieldName, RealmObjectSchema objectSchema)
Adds a new field that contains aRealmList
with references to other Realm model classes.abstract RealmObjectSchema
addRealmListField(String fieldName, Class<?> primitiveType)
Adds a new field that references aRealmList
with primitive values.abstract RealmObjectSchema
addRealmObjectField(String fieldName, RealmObjectSchema objectSchema)
Adds a new field that references anotherRealmObject
.abstract RealmObjectSchema
addRealmSetField(String fieldName, RealmObjectSchema objectSchema)
Adds a new field that contains aRealmSet
with references to other Realm model classes.abstract RealmObjectSchema
addRealmSetField(String fieldName, Class<?> primitiveType)
Adds a new field that references aRealmSet
with primitive values.String
getClassName()
Returns the name of the RealmObject class being represented by this schema.Set<String>
getFieldNames()
Returns all fields in this class.RealmFieldType
getFieldType(String fieldName)
Returns the type used by the underlying storage engine to represent this field.String
getPrimaryKey()
Returns the name of the primary key field.boolean
hasField(String fieldName)
Tests if the class has field defined with the given name.boolean
hasIndex(String fieldName)
Checks if a given field has an index defined.boolean
hasPrimaryKey()
Checks if the class has a primary key defined.boolean
isEmbedded()
Returnstrue
if objects of this type are considered "embedded".boolean
isNullable(String fieldName)
Checks if a given field is nullable i.e., it is allowed to containnull
values.boolean
isPrimaryKey(String fieldName)
Checks if a given field is the primary key field.boolean
isRequired(String fieldName)
Checks if a given field is required i.e., it is not allowed to containnull
values.abstract RealmObjectSchema
removeField(String fieldName)
Removes a field from the class.abstract RealmObjectSchema
removeIndex(String fieldName)
Removes an index from a given field.abstract RealmObjectSchema
removePrimaryKey()
Removes the primary key from this class.abstract RealmObjectSchema
renameField(String currentFieldName, String newFieldName)
Renames a field from one name to another.abstract RealmObjectSchema
setClassName(String className)
Sets a new name for this RealmObject class.void
setEmbedded(boolean embedded)
Converts the class to be embedded or not.abstract RealmObjectSchema
setNullable(String fieldName, boolean nullable)
Sets a field to be nullable i.e., it should be able to holdnull
values.abstract RealmObjectSchema
setRequired(String fieldName, boolean required)
Sets a field to be required i.e., it is not allowed to holdnull
values.abstract RealmObjectSchema
transform(RealmObjectSchema.Function function)
Runs a transformation function on each RealmObject instance of the current class.
-
-
-
Method Detail
-
getClassName
public String getClassName()
Returns the name of the RealmObject class being represented by this schema.- When using a normal
Realm
this name is the same as theRealmObject
class. - When using a
DynamicRealm
this is the name used in all API methods requiring a class name.
- Returns:
- the name of the RealmObject class represented by this schema.
- Throws:
IllegalStateException
- if this schema defintion is no longer part of the Realm.
- When using a normal
-
setClassName
public abstract RealmObjectSchema setClassName(String className)
Sets a new name for this RealmObject class. This is equivalent to renaming it.- Parameters:
className
- the new name for this class.- Throws:
IllegalArgumentException
- if className isnull
or an empty string, or its length exceeds 56 characters.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable or from a synced Realm.- See Also:
RealmSchema.rename(String, String)
-
addField
public abstract RealmObjectSchema addField(String fieldName, Class<?> fieldType, FieldAttribute... attributes)
Adds a new simple field to the RealmObject class. The type must be one supported by Realm. SeeRealmObject
for the list of supported types. If the field should allownull
values use the boxed type instead e.g.,Integer.class
instead ofint.class
.To add fields that reference other RealmObjects or RealmLists use
addRealmObjectField(String, RealmObjectSchema)
oraddRealmListField(String, RealmObjectSchema)
instead.- Parameters:
fieldName
- name of the field to add.fieldType
- type of field to add. SeeRealmObject
for the full list.attributes
- set of attributes for this field.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if the type isn't supported, field name is illegal or a field with that name already exists.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable or if adding a a field withFieldAttribute.PRIMARY_KEY
attribute to a schema of a synced Realm.
-
addRealmObjectField
public abstract RealmObjectSchema addRealmObjectField(String fieldName, RealmObjectSchema objectSchema)
Adds a new field that references anotherRealmObject
.- Parameters:
fieldName
- name of the field to add.objectSchema
- schema for the Realm type being referenced.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if field name is illegal or a field with that name already exists.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable.
-
addRealmListField
public abstract RealmObjectSchema addRealmListField(String fieldName, RealmObjectSchema objectSchema)
Adds a new field that contains aRealmList
with references to other Realm model classes.If the list contains primitive types, use
addRealmListField(String, Class)
instead.- Parameters:
fieldName
- name of the field to add.objectSchema
- schema for the Realm type being referenced.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if the field name is illegal or a field with that name already exists.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable.
-
addRealmListField
public abstract RealmObjectSchema addRealmListField(String fieldName, Class<?> primitiveType)
Adds a new field that references aRealmList
with primitive values. SeeRealmObject
for the list of supported types.Nullability of elements are defined by using the correct class e.g.,
Integer.class
instead ofint.class
. AlternativelysetRequired(String, boolean)
can be used.Example:
// Defines the list of Strings as being non null. RealmObjectSchema schema = schema.create("Person") .addRealmListField("children", String.class) .setRequired("children", true)
addRealmListField(String, RealmObjectSchema)
instead.- Parameters:
fieldName
- name of the field to add.primitiveType
- simple type of elements in the array.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if the field name is illegal, a field with that name already exists or the element type isn't supported.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable.
-
addRealmDictionaryField
public abstract RealmObjectSchema addRealmDictionaryField(String fieldName, RealmObjectSchema objectSchema)
Adds a new field that contains aRealmDictionary
with references to other Realm model classes.If the dictionary contains primitive types, use
addRealmDictionaryField(String, Class)
instead.- Parameters:
fieldName
- name of the field to add.objectSchema
- schema for the Realm type being referenced.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if the field name is illegal or a field with that name already exists.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable.
-
addRealmDictionaryField
public abstract RealmObjectSchema addRealmDictionaryField(String fieldName, Class<?> primitiveType)
Adds a new field that references aRealmDictionary
with primitive values. SeeRealmObject
for the list of supported types.Nullability of elements are defined by using the correct class e.g.,
Integer.class
instead ofint.class
. AlternativelysetRequired(String, boolean)
can be used.Example:
// Defines the dictionary of Strings as being non null. RealmObjectSchema schema = schema.create("Person") .addRealmDictionaryField("parentAndChild", String.class) .setRequired("parentAndChild", true)
addRealmDictionaryField(String, RealmObjectSchema)
instead.- Parameters:
fieldName
- name of the field to add.primitiveType
- simple type of elements in the array.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if the field name is illegal, a field with that name already exists or the element type isn't supported.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable.
-
addRealmSetField
public abstract RealmObjectSchema addRealmSetField(String fieldName, RealmObjectSchema objectSchema)
Adds a new field that contains aRealmSet
with references to other Realm model classes.If the set contains primitive types, use
addRealmSetField(String, Class)
instead.- Parameters:
fieldName
- name of the field to add.objectSchema
- schema for the Realm type being referenced.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if the field name is illegal or a field with that name already exists.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable.
-
addRealmSetField
public abstract RealmObjectSchema addRealmSetField(String fieldName, Class<?> primitiveType)
Adds a new field that references aRealmSet
with primitive values. SeeRealmObject
for the list of supported types.Nullability of elements are defined by using the correct class e.g.,
Integer.class
instead ofint.class
. AlternativelysetRequired(String, boolean)
can be used.Example:
// Defines the set of Strings as being non null. RealmObjectSchema schema = schema.create("Person") .addRealmSetField("children", String.class) .setRequired("children", true)
addRealmSetField(String, RealmObjectSchema)
instead.- Parameters:
fieldName
- name of the field to add.primitiveType
- simple type of elements in the array.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if the field name is illegal, a field with that name already exists or the element type isn't supported.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable.
-
removeField
public abstract RealmObjectSchema removeField(String fieldName)
Removes a field from the class.- Parameters:
fieldName
- field name to remove.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if field name doesn't exist.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable or for a synced Realm.
-
renameField
public abstract RealmObjectSchema renameField(String currentFieldName, String newFieldName)
Renames a field from one name to another.- Parameters:
currentFieldName
- field name to rename.newFieldName
- the new field name.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if field name doesn't exist or if the new field name already exists.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable or for a synced Realm.
-
hasField
public boolean hasField(String fieldName)
Tests if the class has field defined with the given name.- Parameters:
fieldName
- field name to test.- Returns:
true
if the field exists,false
otherwise.
-
addIndex
public abstract RealmObjectSchema addIndex(String fieldName)
Adds an index to a given field. This is the equivalent of adding theIndex
annotation on the field.- Parameters:
fieldName
- field to add index to.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if field name doesn't exist, the field cannot be indexed or it already has a index defined.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable.
-
hasIndex
public boolean hasIndex(String fieldName)
Checks if a given field has an index defined.- Parameters:
fieldName
- existing field name to check.- Returns:
true
if field is indexed,false
otherwise.- Throws:
IllegalArgumentException
- if field name doesn't exist.- See Also:
Index
-
removeIndex
public abstract RealmObjectSchema removeIndex(String fieldName)
Removes an index from a given field. This is the same as removing the@Index
annotation on the field.- Parameters:
fieldName
- field to remove index from.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if field name doesn't exist or the field doesn't have an index.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable or of a synced Realm.
-
addPrimaryKey
public abstract RealmObjectSchema addPrimaryKey(String fieldName)
Adds a primary key to a given field. This is the same as adding thePrimaryKey
annotation on the field. Further, this implicitly addsIndex
annotation to the field as well.- Parameters:
fieldName
- field to set as primary key.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if field name doesn't exist, the field cannot be a primary key or it already has a primary key defined.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable or of a synced Realm.
-
removePrimaryKey
public abstract RealmObjectSchema removePrimaryKey()
Removes the primary key from this class. This is the same as removing thePrimaryKey
annotation from the class. Further, this implicitly removesIndex
annotation from the field as well.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if the class doesn't have a primary key defined.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable or of a synced Realm.
-
setRequired
public abstract RealmObjectSchema setRequired(String fieldName, boolean required)
Sets a field to be required i.e., it is not allowed to holdnull
values. This is equivalent to switching between boxed types and their primitive variant e.g.,Integer
toint
.If the type of designated field is a list of values (not
RealmObject
s , specified nullability only affects its elements, not the field itself. Value list itself is always non-nullable.- Parameters:
fieldName
- name of field in the class.required
-true
if field should be required,false
otherwise.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if the field name doesn't exist, cannot have theRequired
annotation or the field already have been set as required.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable.- See Also:
Required
-
setNullable
public abstract RealmObjectSchema setNullable(String fieldName, boolean nullable)
Sets a field to be nullable i.e., it should be able to holdnull
values. This is equivalent to switching between primitive types and their boxed variant e.g.,int
toInteger
.If the type of designated field is a list of values (not
RealmObject
s , specified nullability only affects its elements, not the field itself. Value list itself is always non-nullable.- Parameters:
fieldName
- name of field in the class.nullable
-true
if field should be nullable,false
otherwise.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException
- if the field name doesn't exist, or cannot be set as nullable.UnsupportedOperationException
- if thisRealmObjectSchema
is immutable.
-
isRequired
public boolean isRequired(String fieldName)
Checks if a given field is required i.e., it is not allowed to containnull
values.- Parameters:
fieldName
- field to check.- Returns:
true
if it is required,false
otherwise.- Throws:
IllegalArgumentException
- if field name doesn't exist.- See Also:
setRequired(String, boolean)
-
isNullable
public boolean isNullable(String fieldName)
Checks if a given field is nullable i.e., it is allowed to containnull
values.- Parameters:
fieldName
- field to check.- Returns:
true
if it is required,false
otherwise.- Throws:
IllegalArgumentException
- if field name doesn't exist.- See Also:
setNullable(String, boolean)
-
isPrimaryKey
public boolean isPrimaryKey(String fieldName)
Checks if a given field is the primary key field.- Parameters:
fieldName
- field to check.- Returns:
true
if it is the primary key field,false
otherwise.- Throws:
IllegalArgumentException
- if field name doesn't exist.- See Also:
addPrimaryKey(String)
-
hasPrimaryKey
public boolean hasPrimaryKey()
Checks if the class has a primary key defined.- Returns:
true
if a primary key is defined,false
otherwise.- See Also:
PrimaryKey
-
getPrimaryKey
public String getPrimaryKey()
Returns the name of the primary key field.- Returns:
- the name of the primary key field.
- Throws:
IllegalStateException
- if the class doesn't have a primary key defined.
-
getFieldNames
public Set<String> getFieldNames()
Returns all fields in this class.- Returns:
- a list of all the fields in this class.
-
transform
public abstract RealmObjectSchema transform(RealmObjectSchema.Function function)
Runs a transformation function on each RealmObject instance of the current class. The object will be represented as aDynamicRealmObject
.There is no guarantees in which order the objects are returned.
- Parameters:
function
- transformation function.- Returns:
- this schema.
- Throws:
UnsupportedOperationException
- if thisRealmObjectSchema
is immutable.
-
getFieldType
public RealmFieldType getFieldType(String fieldName)
Returns the type used by the underlying storage engine to represent this field.- Parameters:
fieldName
- name of the target field.- Returns:
- the underlying type used by Realm to represent this field.
-
isEmbedded
public boolean isEmbedded()
Returnstrue
if objects of this type are considered "embedded". SeeRealmClass.embedded()
for further details.- Returns:
true
if objects of this type are embedded.false
if not.
-
setEmbedded
public void setEmbedded(boolean embedded)
Converts the class to be embedded or not.A class can only be marked as embedded if the following invariants are satisfied:
- The class is not allowed to have a primary key defined.
-
All existing objects of this type, must have one and exactly one parent object
already pointing to it. If 0 or more than 1 object has a reference to an object
about to be marked embedded an
IllegalStateException
will be thrown.
- Throws:
IllegalStateException
- if the class could not be converted because it broke some of the Embedded Objects invariants.- See Also:
RealmClass.embedded()
-
-