Class Conversions


  • public class Conversions
    extends java.lang.Object
    This class provides default instances of common implementations if com.univocity.parsers.conversions.Conversion, as well as useful methods for obtaining new instances of these.
    Author:
    uniVocity Software Pty Ltd - parsers@univocity.com
    • Method Detail

      • trim

        public static TrimConversion trim​(int length)
        Returns a TrimConversion that limits the output to a fixed length
        Parameters:
        length - the maximum length a value can contain. Characters after this limit will be discarded.
        Returns:
        a trim-to-length conversion
      • replace

        public static RegexConversion replace​(java.lang.String replaceRegex,
                                              java.lang.String replacement)
        Returns a new instance of RegexConversion
        Parameters:
        replaceRegex - the regular expression used to match contents of a given input String
        replacement - the replacement content to replace any contents matched by the given regular expression
        Returns:
        the new instance of RegexConversion created with the given parameters.
      • toNull

        public static NullStringConversion toNull​(java.lang.String... nullRepresentations)
        Returns a new instance of NullStringConversion
        Parameters:
        nullRepresentations - the sequence of Strings that represent a null value.
        Returns:
        the new instance of NullStringConversion created with the given parameters.
      • toDate

        public static DateConversion toDate​(java.lang.String... dateFormats)
        Returns a new instance of DateConversion
        Parameters:
        dateFormats - list of acceptable date patterns. The first pattern in this sequence will be used to convert a Date into a String in DateConversion.revert(Date).
        Returns:
        the new instance of DateConversion created with the given parameters.
      • toDate

        public static DateConversion toDate​(java.util.Date dateIfNull,
                                            java.lang.String... dateFormats)
        Returns a new instance of DateConversion
        Parameters:
        dateIfNull - default Date value to be returned when the input String is null. Used when ObjectConversion.execute(String) is invoked.
        dateFormats - list of acceptable date patterns. The first pattern in this sequence will be used to convert a Date into a String in DateConversion.revert(Date).
        Returns:
        the new instance of DateConversion created with the given parameters.
      • toDate

        public static DateConversion toDate​(java.util.Date dateIfNull,
                                            java.lang.String stringIfNull,
                                            java.lang.String... dateFormats)
        Returns a new instance of DateConversion
        Parameters:
        dateIfNull - default Date value to be returned when the input String is null. Used when ObjectConversion.execute(String) is invoked.
        stringIfNull - default String value to be returned when a Date input is null. Used when DateConversion.revert(Date) is invoked.
        dateFormats - list of acceptable date patterns. The first pattern in this sequence will be used to convert a Date into a String in DateConversion.revert(Date).
        Returns:
        the new instance of DateConversion created with the given parameters.
      • formatToNumber

        public static NumericConversion<java.lang.Number> formatToNumber​(java.lang.String... numberFormats)
        Returns a new instance of NumericConversion
        Parameters:
        numberFormats - list of acceptable numeric patterns. The first pattern in this sequence will be used to convert a Number into a String in NumericConversion.revert(Number).
        Returns:
        a new instance of NumericConversion that supports the given number formats
      • formatToNumber

        public static <T extends java.lang.Number> NumericConversion<T> formatToNumber​(java.lang.Class<T> numberType,
                                                                                       java.lang.String... numberFormats)
        Returns a new instance of NumericConversion
        Type Parameters:
        T - type of number
        Parameters:
        numberType - type of number to be returned. The resulting instance of Number will be cast to the expected type.
        numberFormats - list of acceptable numeric patterns. The first pattern in this sequence will be used to convert a Number into a String in NumericConversion.revert(Number).
        Returns:
        a new instance of NumericConversion that supports the given number formats
      • formatToBigDecimal

        public static FormattedBigDecimalConversion formatToBigDecimal​(java.math.BigDecimal defaultValueForNullString,
                                                                       java.lang.String stringIfNull,
                                                                       java.lang.String... numberFormats)
        Returns a new instance of FormattedBigDecimalConversion
        Parameters:
        defaultValueForNullString - default BigDecimal to be returned when the input String is null. Used when ObjectConversion.execute(String) is invoked.
        stringIfNull - default String value to be returned when a BigDecimal input is null. Used when FormattedBigDecimalConversion#revert(BigDecimal) is invoked.
        numberFormats - list of acceptable numeric patterns. The first pattern in this sequence will be used to convert a BigDecimal into a String in NumericConversion.revert(Number).
        Returns:
        a new instance of FormattedBigDecimalConversion that supports the given number formats
      • toBoolean

        public static BooleanConversion toBoolean​(java.lang.Boolean defaultValueForNullString,
                                                  java.lang.String defaultValueForNullBoolean,
                                                  java.lang.String[] valuesForTrue,
                                                  java.lang.String[] valuesForFalse)
        Returns a new instance of BooleanConversion
        Parameters:
        defaultValueForNullString - default Boolean value to be returned when the input String is null. Used when ObjectConversion.execute(String) is invoked.
        defaultValueForNullBoolean - default String value to be returned when a Boolean input is null. Used when BooleanConversion.revert(Boolean) is invoked.
        valuesForTrue - Strings that identify the boolean value true. The first element will be returned when executing BooleanConversion.revert(true)
        valuesForFalse - Strings that identify the boolean value false. The first element will be returned when executing BooleanConversion.revert(false)
        Returns:
        a new instance of BooleanConversion with support for multiple representations of true and false
      • toBoolean

        public static BooleanConversion toBoolean​(java.lang.Boolean defaultValueForNullString,
                                                  java.lang.String defaultValueForNullBoolean,
                                                  java.lang.String valueForTrue,
                                                  java.lang.String valueForFalse)
        Returns a new instance of BooleanConversion
        Parameters:
        defaultValueForNullString - default Boolean value to be returned when the input String is null. Used when ObjectConversion.execute(String) is invoked.
        defaultValueForNullBoolean - default String value to be returned when a Boolean input is null. Used when BooleanConversion.revert(Boolean) is invoked.
        valueForTrue - String that identify the boolean value true.
        valueForFalse - String that identify the boolean value false.
        Returns:
        a new instance of BooleanConversion with support for multiple representations of true and false
      • toBoolean

        public static BooleanConversion toBoolean​(java.lang.String[] valuesForTrue,
                                                  java.lang.String[] valuesForFalse)
        Returns a new instance of BooleanConversion
        Parameters:
        valuesForTrue - Strings that identify the boolean value true. The first element will be returned when executing BooleanConversion.revert(true)
        valuesForFalse - Strings that identify the boolean value false. The first element will be returned when executing BooleanConversion.revert(false)
        Returns:
        a new instance of BooleanConversion with support for multiple representations of true and false
      • toBoolean

        public static BooleanConversion toBoolean()
        Returns a new instance of BooleanConversion that converts the string "true" to true, and the String "false" to false.
        Returns:
        a new instance of BooleanConversion with support for multiple representations of true and false
      • toBoolean

        public static BooleanConversion toBoolean​(java.lang.String valueForTrue,
                                                  java.lang.String valueForFalse)
        Returns a new instance of BooleanConversion
        Parameters:
        valueForTrue - String that identifies the boolean value true.
        valueForFalse - String that identifies the boolean value false.
        Returns:
        a new instance of BooleanConversion with support for multiple representations of true and false
      • toChar

        public static CharacterConversion toChar​(java.lang.Character defaultValueForNullString,
                                                 java.lang.String defaultValueForNullChar)
        Returns a new instance of CharacterConversion
        Parameters:
        defaultValueForNullString - default Character value to be returned when the input String is null. Used when ObjectConversion.execute(String) is invoked.
        defaultValueForNullChar - default String value to be returned when a Character input is null. Used when CharacterConversion#revert(Character) is invoked.
        Returns:
        a new instance of CharacterConversion
      • toEnum

        public static <T extends java.lang.Enum<T>> EnumConversion<T> toEnum​(java.lang.Class<T> enumType)
        Returns a new instance of EnumConversion
        Type Parameters:
        T - the enum type
        Parameters:
        enumType - the enumeration type to be converted from/to String
        Returns:
        new instance of EnumConversion
      • toEnum

        public static <T extends java.lang.Enum<T>> EnumConversion<T> toEnum​(java.lang.Class<T> enumType,
                                                                             EnumSelector... selectors)
        Returns a new instance of EnumConversion
        Type Parameters:
        T - the enum type
        Parameters:
        enumType - the enumeration type to be converted from/to String
        selectors - the selection elements of the enumeration to use for matching Strings.
        Returns:
        new instance of EnumConversion
      • toEnum

        public static <T extends java.lang.Enum<T>> EnumConversion<T> toEnum​(java.lang.Class<T> enumType,
                                                                             java.lang.String customEnumElement,
                                                                             EnumSelector... selectors)
        Returns a new instance of EnumConversion
        Type Parameters:
        T - the enum type
        Parameters:
        enumType - the enumeration type to be converted from/to String
        customEnumElement - name of custom element of the enumeration (attribute or method) whose values should be used to match equivalent Strings.
        selectors - the selection elements of the enumeration to use for matching Strings.
        Returns:
        new instance of EnumConversion
      • toEnum

        public static <T extends java.lang.Enum<T>> EnumConversion<T> toEnum​(java.lang.Class<T> enumType,
                                                                             T valueIfStringIsNull,
                                                                             java.lang.String valueIfEnumIsNull,
                                                                             java.lang.String customEnumElement,
                                                                             EnumSelector... selectors)
        Returns a new instance of EnumConversion
        Type Parameters:
        T - the enum type
        Parameters:
        enumType - the enumeration type to be converted from/to String
        valueIfStringIsNull - the default enumeration constant to use if the input String is null
        valueIfEnumIsNull - the default String value to use if the input enum constant is null
        customEnumElement - name of custom element of the enumeration (attribute or method) whose values should be used to match equivalent Strings.
        selectors - the selection elements of the enumeration to use for matching Strings.
        Returns:
        new instance of EnumConversion