Class FieldReplacer


  • public class FieldReplacer
    extends Object
    POJO to store a filename, its match pattern and its replacement string. A checkAndReplace method is provided where you can simultaneously check if the field matches this replacer and if the pattern matches your field value.
    Author:
    Peter Ciuffetti
    • Constructor Detail

      • FieldReplacer

        public FieldReplacer​(String fieldName,
                             String toFieldName,
                             String pattern,
                             String replacement,
                             Integer flags)
        Create a FieldReplacer for a field. Any pattern exceptions are caught within this constructor and the object is marked inValid. The error will be logged. This prevents this caller from attempting invalid replacements.
        Parameters:
        fieldName - the name of the source field to operate on. Required.
        toFieldName - the name of the target field. Required.
        pattern - the pattern the field must match. Required.
        replacement - the replacement string
        flags - the Pattern flags value, or null if no flags are needed
      • FieldReplacer

        public FieldReplacer​(String fieldName,
                             String pattern,
                             String replacement,
                             Integer flags)
        Field replacer with the input and output field the same.
        Parameters:
        fieldName - the name of the source field to operate on. Required.
        pattern - the pattern the field must match. Required.
        replacement - the replacement string
        flags - the Integer flags value, or null if no flags are needed
    • Method Detail

      • getFieldName

        public String getFieldName()
      • getToFieldName

        public String getToFieldName()
      • getPattern

        public Pattern getPattern()
      • getReplacement

        public String getReplacement()
      • isValid

        public boolean isValid()
        Does this FieldReplacer have a valid fieldname and pattern?
        Returns:
        true if fieldname and pattern are valid, false otherwise
      • replace

        public String replace​(String value)
        Return the replacement value for a field value. This does not check for a matching field; the caller must decide if this FieldReplacer should operate on this value by checking getFieldName(). The method returns the value with the replacement. If the value returned is not different then eiher the pattern didn't match or the replacement was a no-op.
        Parameters:
        value - the value to replace
        Returns:
        the replaced value
      • checkAndReplace

        public String checkAndReplace​(String fieldName,
                                      String value)
        Return a replacement value for a field. This is designed to fail fast and trigger a replacement only when necessary. If this method returns null, either the field does not match or the value does not match the pattern (or possibly the pattern is invalid). So only if the method returns a non-null value will you need to replace the value for the field.
        Parameters:
        fieldName - the name of the field you are checking
        value - the value of the field you are checking
        Returns:
        a replacement value. If null, either the field does not match or the value does not match.