Class Field

  • All Implemented Interfaces:
    java.lang.Cloneable, java.lang.Iterable<Component>, Parser, Property, Replicateable

    public class Field
    extends ParserComponent
    implements Property
    A parser for marshalling and unmarshalling a single field in a record. A field is usually, but optionally, bound to a simple property value.

    A field component does not have any children.

    Since:
    2.0
    • Constructor Detail

      • Field

        public Field()
        Constructs a new Field.
    • Method Detail

      • hasContent

        public boolean hasContent​(ParsingContext context)
        Description copied from interface: Parser
        Returns whether this parser or any of its descendant have content for marshalling.
        Specified by:
        hasContent in interface Parser
        Parameters:
        context - the ParsingContext
        Returns:
        true if there is content for marshalling, false otherwise
      • isOptional

        public boolean isOptional()
        Description copied from interface: Parser
        Returns whether this node must exist during unmarshalling.
        Specified by:
        isOptional in interface Parser
        Returns:
        true if this node is optional during unmarshalling, false otherwise
      • defines

        public boolean defines​(java.lang.Object value)
        Specified by:
        defines in interface Property
      • matches

        public boolean matches​(UnmarshallingContext context)
        Tests if the field text in the record matches this field.
        Specified by:
        matches in interface Parser
        Parameters:
        context - the UnmarshallingContext containing the record to match
        Returns:
        true if the field text is a match or this field is not used to identify the record
      • isMatch

        protected boolean isMatch​(java.lang.String text)
        Returns true if the provided field text is a match for this field definition based on the configured literal value or regular expression.
        Parameters:
        text - the field text to test
        Returns:
        true if the field text matches this field definitions constraints, or false if the field text is null or does not match
      • unmarshal

        public boolean unmarshal​(UnmarshallingContext context)
        Description copied from interface: Parser
        Unmarshals a record.
        Specified by:
        unmarshal in interface Parser
        Parameters:
        context - the UnmarshallingContext
        Returns:
        true if this component was present in the unmarshalled record, or false otherwise
      • parseValue

        protected java.lang.Object parseValue​(UnmarshallingContext context,
                                              java.lang.String fieldText)
        Parses and validates a field property value from the given field text.
        Parameters:
        context - the UnmarshallingContext to report field errors to
        fieldText - the field text to parse
        Returns:
        the parsed field value, or Value.INVALID if the field was invalid, or Value.MISSING if the field was not present in the record
      • formatValue

        protected java.lang.String formatValue​(java.lang.Object value)
        Formats a field/property value.
        Parameters:
        value - the property value to format
        Returns:
        the formatted field text
      • createValue

        public java.lang.Object createValue​(ParsingContext context)
        Description copied from interface: Property
        Creates the property value and returns it.
        Specified by:
        createValue in interface Property
        Parameters:
        context - the ParsingContext
        Returns:
        the property value
      • getValue

        public java.lang.Object getValue​(ParsingContext context)
        Description copied from interface: Property
        Returns the value of this property.

        When unmarshalling, this method should return Value.MISSING if the field was not present in the stream. Or if present, but has no value, null should be returned.

        When marshalling, this method should return Value.MISSING for any optional segment bound to a bean object, or null if required. Null field properties should always return Value.MISSING.

        Specified by:
        getValue in interface Parser
        Specified by:
        getValue in interface Property
        Parameters:
        context - the ParsingContext
        Returns:
        the property value, or Value.MISSING if not present in the stream, or Value.INVALID if the field was invalid
      • setValue

        public void setValue​(ParsingContext context,
                             java.lang.Object value)
        Description copied from interface: Property
        Sets the property value (before marshalling).
        Specified by:
        setValue in interface Parser
        Specified by:
        setValue in interface Property
        Parameters:
        context - the ParsingContext
        value - the property value
      • getRegex

        public java.lang.String getRegex()
        Returns the regular expression pattern the field text parsed by this field definition must match.
        Returns:
        the regular expression pattern
      • setRegex

        public void setRegex​(java.lang.String pattern)
                      throws java.util.regex.PatternSyntaxException
        Sets the regular expression pattern the field text parsed by this field definition must match.
        Parameters:
        pattern - the regular expression pattern
        Throws:
        java.util.regex.PatternSyntaxException - if the pattern is invalid
      • getRegexPattern

        protected java.util.regex.Pattern getRegexPattern()
        Returns the regular expression the field text parsed by this field definition must match.
        Returns:
        the regular expression
      • registerLocals

        public void registerLocals​(java.util.Set<ParserLocal<? extends java.lang.Object>> locals)
        Description copied from class: Component
        Called by a stream to register variables stored in the parsing context. This method should be overridden by subclasses that need to register one or more parser context variables.
        Overrides:
        registerLocals in class Component
        Parameters:
        locals - set of local variables
      • setPropertyType

        public void setPropertyType​(java.lang.Class<?> type)
      • getPropertyType

        public java.lang.Class<?> getPropertyType()
      • isIdentifier

        public boolean isIdentifier()
        Description copied from interface: Property
        Returns whether this property or any of its descendants are used to identify a bean object.
        Specified by:
        isIdentifier in interface Parser
        Specified by:
        isIdentifier in interface Property
        Returns:
        true if this property identifies a bean
      • setIdentifier

        public void setIdentifier​(boolean recordIdentifier)
        Specified by:
        setIdentifier in interface Property
      • setFormat

        public void setFormat​(FieldFormat format)
      • getLiteral

        public java.lang.String getLiteral()
      • setLiteral

        public void setLiteral​(java.lang.String literal)
      • getType

        public java.lang.Class<?> getType()
        Specified by:
        getType in interface Property
      • isTrim

        public boolean isTrim()
      • setTrim

        public void setTrim​(boolean trim)
      • isRequired

        public boolean isRequired()
      • setRequired

        public void setRequired​(boolean required)
      • isLazy

        public boolean isLazy()
      • setLazy

        public void setLazy​(boolean lazy)
      • getMinLength

        public int getMinLength()
      • setMinLength

        public void setMinLength​(int minLength)
      • getMaxLength

        public int getMaxLength()
      • setMaxLength

        public void setMaxLength​(int maxLength)
      • setRegex

        public void setRegex​(java.util.regex.Pattern regex)
      • setType

        public void setType​(java.lang.Class<?> type)
        Specified by:
        setType in interface Property
      • getDefaultValue

        public java.lang.Object getDefaultValue()
        Returns the default value for a field parsed by this field definition when the field text is null or the empty string (after trimming).
        Returns:
        default value
      • setDefaultValue

        public void setDefaultValue​(java.lang.Object defaultValue)
        Sets the default value for a field parsed by this field definition when the field text is null or the empty string (after trimming).
        Parameters:
        defaultValue - the default value
      • setHandler

        public void setHandler​(TypeHandler handler)
      • getSize

        public int getSize()
        Description copied from interface: Parser
        Returns the size of a single occurrence of this element, which is used to offset field positions for repeating segments and fields.

        The concept of size is dependent on the stream format. The size of an element in a fixed length stream format is determined by the length of the element in characters, while other stream formats calculate size based on the number of fields. Some stream formats, such as XML, may ignore size settings.

        Specified by:
        getSize in interface Parser
        Returns:
        the size of this parser element
      • isBound

        public boolean isBound()
      • setBound

        public void setBound​(boolean property)