All Implemented Interfaces:
DataExceptionContributor, JsonSerializable, RecordSerializable, XmlSerializable, SchemaProblemsSupplier, JavaCodeGenerator, Serializable, Cloneable
Direct Known Subclasses:
BinaryFieldDef, BooleanFieldDef, NumericFieldDef, RecordFieldDef, TemporalFieldDef, TextFieldDef, UuidFieldDef

public abstract class FieldDef extends SchemaPart implements Cloneable, SchemaProblemsSupplier
The metadata structure of one column or property in a file, JSON object, database table, or class for validation and mapping.
See Also:
  • Constructor Details

    • FieldDef

      public FieldDef()
    • FieldDef

      public FieldDef(String name, FieldType type)
  • Method Details

    • getSchemaProblems

      public void getSchemaProblems(List<SchemaProblem> problems, boolean includeChildren)
      Description copied from interface: SchemaProblemsSupplier
      Adds this element's problems to the list, plus those of its children when includeChildren is true; the default adds none.
      Specified by:
      getSchemaProblems in interface SchemaProblemsSupplier
    • clone

      public FieldDef clone()
      Overrides:
      clone in class FoundationObject
    • validateField

      public ValidationResult validateField(Record record)
      Validates this field in the given record, locating it by getPosition() when set and by name otherwise.
    • validateField

      public ValidationResult validateField(Record record, ValidationResult result)
      Validates this field in the record like validateField(Record), adding errors to the given result (or a new one if null) and returning it.
    • validateField

      public final ValidationResult validateField(Field field)
      Validates the given field against this definition, treating null as a missing field, and returns the errors found.
    • validateField

      public final ValidationResult validateField(Field field, ValidationResult result)
      Validates the field like validateField(Field), adding errors to the given result (or a new one if null) and returning it.
    • assertValid

      public FieldDef assertValid()
      Description copied from class: FoundationObject
      Throws a DataException if this object is invalid, otherwise returns it; the base implementation checks nothing.
      Overrides:
      assertValid in class FoundationObject
    • validateValue

      public final ValidationResult validateValue(Object value)
      Validates a Java value against this definition, treating arrays and collections as array values, and returns the errors found.
    • validateValue

      public final ValidationResult validateValue(Object value, ValidationResult result)
      Validates the value like validateValue(Object), adding errors to the given result (or a new one if null) and returning it.
    • validateAllowedValues

      protected void validateAllowedValues(Object value, ValidationResult result)
      Adds an error if isLimitToAllowedValues() is true and the value is not one of the allowed values.
    • validateValueImpl

      protected abstract void validateValueImpl(Object value, ValidationResult result) throws Throwable
      Applies the subclass-specific checks to a single (non-array) value, adding any errors to the result; called after the common type, array and allowed-value checks.
      Throws:
      Throwable
    • mapValue

      public final Object mapValue(Object sourceValue)
      Converts a value to this field's type, using the default value expression when the value is null. Array fields return a List of converted elements; other fields convert only the first element of an array or collection.
    • mapValueImpl

      protected Object mapValueImpl(Object value) throws Throwable
      Converts a single (non-array) value to this field's type; subclasses override it to parse or adjust values first.
      Throws:
      Throwable
    • generateConstructor

      protected void generateConstructor(JavaCodeBuilder code, CodeWriter writer)
      Writes the constructor call that starts this field's generated Java code; subclasses override it to match their constructors.
    • generateJavaCode

      public void generateJavaCode(JavaCodeBuilder code)
      Description copied from interface: JavaCodeGenerator
      Appends Java source code representing this object to the given builder.
      Specified by:
      generateJavaCode in interface JavaCodeGenerator
    • getEntityDef

      public EntityDef getEntityDef()
      The parent entity containing this field.
    • setEntityDef

      protected FieldDef setEntityDef(EntityDef entityDef)
      Sets the parent entity; called by EntityDef when this field is added to it.
    • getSchemaDef

      public SchemaDef getSchemaDef(boolean throwException)
      Returns the schema of the entity containing this field; if either is missing, throws a DataException when throwException is true and returns null otherwise.
    • getSchemaDef

      public SchemaDef getSchemaDef()
      The schema of the parent entity containing this field.
      Specified by:
      getSchemaDef in class SchemaPart
    • getSchemaPartContainer

      public SchemaPart getSchemaPartContainer()
      Description copied from class: SchemaPart
      Returns the schema part containing this one or null if there is none.
      Specified by:
      getSchemaPartContainer in class SchemaPart
    • getSchemaPartType

      public SchemaPartType getSchemaPartType()
      Description copied from class: SchemaPart
      Returns the kind of part this is, such as SchemaPartType.ENTITY.
      Specified by:
      getSchemaPartType in class SchemaPart
    • getSchemaPartName

      public String getSchemaPartName()
      Description copied from class: SchemaPart
      Returns the name identifying this part, such as an entity's name or an index field's field name.
      Specified by:
      getSchemaPartName in class SchemaPart
    • getName

      public String getName()
      The human-readable text identifier for this field.
    • setName

      public FieldDef setName(String name)
    • getQualifiedName

      public String getQualifiedName()
      Returns the fully qualified name that includes the name of this field definition and all names in its hierarchy separated by period.
    • getEntityQualifiedName

      public String getEntityQualifiedName()
      Returns the qualified name that includes the name of this field and the EntityDef above it separated by period.
    • getDisplayName

      public String getDisplayName()
      The user-friendly name for this field.
    • setDisplayName

      public FieldDef setDisplayName(String displayName)
      Sets the user-friendly name for this field, used instead of the name in validation messages.
    • getDisplayNameOrName

      public String getDisplayNameOrName()
      The user-friendly name for this field if one is set or the getName() value otherwise.
    • getNameForErrorMessage

      public String getNameForErrorMessage()
      The fully-qualified name for use in error and validation messages using getDisplayNameOrName() for each name segment.
    • getDescription

      public String getDescription()
      The human-readable summary of this field.
    • setDescription

      public FieldDef setDescription(String description)
    • getType

      public FieldType getType()
      The data type of this field.
    • setType

      public FieldDef setType(FieldType type)
      Sets the data type of this field; each subclass accepts only its own kind of type, such as numeric types for NumericFieldDef.
    • isRequired

      public boolean isRequired()
      Indicates if this field can be null or missing.
    • setRequired

      public FieldDef setRequired(boolean required)
      Indicates if this field must be present and not null to pass validation (defaults to false).
    • getMinimumElements

      public Integer getMinimumElements()
      The minimum numbers of array elements allowed by this array field. This field is treated as an array if either this property of the getMaximumElements() property are not null. Negative values will be set to zero (0).
    • setMinimumElements

      public FieldDef setMinimumElements(Integer minimumElements)
      Sets the minimum number of array elements allowed; a non-null value makes this field an array. Negative values are set to zero (0).
    • getMaximumElements

      public Integer getMaximumElements()
      The maximum numbers of array elements allowed by this array field. This field is treated as an array if either this property of the getMinimumElements() property are not null. Negative values will be set to the max integer (2147483647). Zero (0) is allowed, it means the array must always be empty.
    • setMaximumElements

      public FieldDef setMaximumElements(Integer maximumElements)
      Sets the maximum number of array elements allowed; a non-null value makes this field an array. Negative values are set to the max integer and zero (0) means the array must be empty.
    • isArray

      public boolean isArray()
      Indicates if either getMinimumElements() or getMaximumElements() are not null.
    • setArray

      public FieldDef setArray(boolean array)
      Forces this field to be an array by setting the getMinimumElements() to zero (0) if both getMinimumElements() and getMaximumElements() are null, otherwise, does nothing.
    • isStrictArrays

      public boolean isStrictArrays()
      Indicates if arrays should be strictly handled as arrays (default/true) or if an array with one value can be treated and a non-array and vice-versa (false). Most developers will want to leave this set to the default (true).
    • setStrictArrays

      public FieldDef setStrictArrays(boolean strictArrays)
      Indicates if values must match this field's array setting exactly (defaults to true); when false, a single value and a one-element array are interchangeable.
    • isLimitToAllowedValues

      public boolean isLimitToAllowedValues()
      Indicates if values not it getAllowedValues() are allowed.
    • setLimitToAllowedValues

      public FieldDef setLimitToAllowedValues(boolean limitToAllowedValues)
      Indicates if values must be one of getAllowedValues() to pass validation (defaults to false).
    • getAllowedValues

      public ValueMatch<?> getAllowedValues()
      The optional set of values this field allows. The isLimitToAllowedValues() property indicates if other values can be set.
    • setAllowedValues

      public FieldDef setAllowedValues(ValueMatch<?> allowedValues)
      Sets the optional set of values this field allows; they are enforced only when isLimitToAllowedValues() is true.
    • getPosition

      public Integer getPosition()
      The zero-based location of this field in the data (if applicable). This can be used to name previously unnamed fields based on their position (or override the names of already named fields).
    • setPosition

      public FieldDef setPosition(Integer position)
      Sets the zero-based position of this field in the data or null to locate it by name; mapping renames the field found at this position to this field's name.
    • getDefaultValueExpression

      public Expression getDefaultValueExpression()
      The calculated value to set in the data if the field is null or missing.
    • getDefaultValueExpressionAsString

      public String getDefaultValueExpressionAsString()
      The calculated value to set in the data if the field is null or missing.
    • setDefaultValueExpression

      public FieldDef setDefaultValueExpression(Expression defaultValueExpression)
      The calculated value to set in the data if the field is null or missing.
    • getExample

      public String getExample()
      A human-readable example of values supported by this field. A "country" field might have "Japan" as an example.
    • setExample

      public FieldDef setExample(String example)
    • getAttributes

      public Attributes getAttributes()
      User-defined key-value pairs to attached arbitrary metadata to this field.
    • setAttributes

      public FieldDef setAttributes(Attributes attributes)
      Sets the user-defined key-value pairs attached to this field (null is treated as empty).
    • getTags

      public Tags getTags()
      The set of labels used to organize, filter, and classify this field.
    • setTags

      public FieldDef setTags(Tags tags)
      Sets the labels used to organize, filter, and classify this field (null is treated as empty).
    • getPrimaryKeyPosition

      public Integer getPrimaryKeyPosition()
      Returns the zero-based position of this field in its entity's primary key or null if it is not part of the key.
    • setPrimaryKeyPosition

      public FieldDef setPrimaryKeyPosition(Integer primaryKeyPosition)
      Sets the zero-based position of this field in its entity's primary key or null if it is not part of the key.
    • isPrimaryKey

      public boolean isPrimaryKey()
      Returns true if primaryKeyPosition is not null.
    • getPrimaryKeyType

      public PrimaryKeyType getPrimaryKeyType()
      Returns the strategy used to generate this primary key field's values or null if none is set.
    • setPrimaryKeyType

      public FieldDef setPrimaryKeyType(PrimaryKeyType primaryKeyType)
      Sets the strategy used to generate this primary key field's values; a primary key position must also be set.
    • getIndexInEntityDef

      public int getIndexInEntityDef()
      Returns the zero-based index of this field in EntityDef.
    • toJson

      public String toJson()
      Specified by:
      toJson in interface JsonSerializable
      Specified by:
      toJson in interface RecordSerializable
    • toRecord

      public Record toRecord()
      Description copied from interface: RecordSerializable
      Converts this object's state to a record that RecordSerializable.fromRecord(Record) can load.
      Specified by:
      toRecord in interface RecordSerializable
      Overrides:
      toRecord in class Bean
    • fromRecord

      public FieldDef fromRecord(Record source)
      Description copied from interface: RecordSerializable
      Loads this instance's state from a record and returns this (for fluid API call chaining). For fluid API call chaining, the overridden method should change the declared return type to its class.
      Specified by:
      fromRecord in interface RecordSerializable
      Overrides:
      fromRecord in class Bean
      Parameters:
      source -
      Returns:
      this instance.
    • fromJson

      public FieldDef fromJson(String jsonString)
      Description copied from interface: JsonSerializable
      Loads this object's state from the given JSON text.
      Specified by:
      fromJson in interface JsonSerializable
      Specified by:
      fromJson in interface RecordSerializable
    • toXmlElement

      public Element toXmlElement(Document document)
      Description copied from interface: XmlSerializable
      Returns an element, created with document, describing this object; the default implementation throws a DataException.
      Specified by:
      toXmlElement in interface XmlSerializable
    • fromXmlElement

      public FieldDef fromXmlElement(Element fieldDefElement)
      Specified by:
      fromXmlElement in interface XmlSerializable
    • newInstanceFromType

      public static FieldDef newInstanceFromType(String type)
      Creates the field definition subclass for the given FieldType name (ignoring case) or returns null if the name is empty or the type has no matching subclass.
    • newInstanceFromXml

      public static FieldDef newInstanceFromXml(Element fieldDefElement)
      Creates a field definition from a field element, picking the subclass by its type attribute; returns null if the type is missing or has no matching subclass.
    • newInstanceFromRecord

      public static FieldDef newInstanceFromRecord(Record fieldDefRecord)
      Creates a field definition from its record form, picking the subclass by the type field; returns null if the type is missing or has no matching subclass.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object