Class Convert


  • public final class Convert
    extends java.lang.Object
    Utility methods for type conversions

    This class provides less type-safety than the general jOOQ API methods. For instance, it accepts arbitrary Converter objects in methods like convert(Collection, Converter) and convert(Object, Class), trying to retrofit the Object argument to the type provided in Converter.fromType() before performing the actual conversion.

    Author:
    Lukas Eder
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.Set<java.lang.String> FALSE_VALUES
      All string values that can be transformed into a boolean false value.
      static java.util.Set<java.lang.String> TRUE_VALUES
      All string values that can be transformed into a boolean true value.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object[] convert​(java.lang.Object[] values, java.lang.Class<?>[] types)
      Convert an array of values to a matching data type
      static java.lang.Object[] convert​(java.lang.Object[] values, Field<?>[] fields)
      Convert an array of values to a matching data type
      static <T> T convert​(java.lang.Object from, java.lang.Class<? extends T> toClass)
      Convert an object to a type.
      static <U> U convert​(java.lang.Object from, Converter<?,​? extends U> converter)
      Convert an object to a type.
      static <T> java.util.List<T> convert​(java.util.Collection<?> collection, java.lang.Class<? extends T> type)
      Convert a collection of objects to a list of T, using convert(Object, Class)
      static <U> java.util.List<U> convert​(java.util.Collection<?> collection, Converter<?,​? extends U> converter)
      Convert a collection of objects to a list of T, using convert(Object, Converter)
      static java.lang.Object[] convertArray​(java.lang.Object[] from, java.lang.Class<?> toClass)
      Convert an array into another one by these rules
      static <U> U[] convertArray​(java.lang.Object[] from, Converter<?,​? extends U> converter)
      Convert an array into another one using a converter
      static <U> U[] convertCollection​(java.util.Collection from, java.lang.Class<? extends U[]> to)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • TRUE_VALUES

        public static final java.util.Set<java.lang.String> TRUE_VALUES
        All string values that can be transformed into a boolean true value.
      • FALSE_VALUES

        public static final java.util.Set<java.lang.String> FALSE_VALUES
        All string values that can be transformed into a boolean false value.
    • Method Detail

      • convert

        public static final java.lang.Object[] convert​(java.lang.Object[] values,
                                                       Field<?>[] fields)
        Convert an array of values to a matching data type

        This converts values[i] to fields[i].getType()

      • convert

        public static final java.lang.Object[] convert​(java.lang.Object[] values,
                                                       java.lang.Class<?>[] types)
        Convert an array of values to a matching data type

        This converts values[i] to types[i]

      • convertArray

        public static final java.lang.Object[] convertArray​(java.lang.Object[] from,
                                                            java.lang.Class<?> toClass)
                                                     throws DataTypeException
        Convert an array into another one by these rules

        • If toClass is not an array class, then make it an array class first
        • If toClass is an array class, then create an instance from it, and convert all elements in the from array one by one, using convert(Object, Class)
        Parameters:
        from - The array to convert
        toClass - The target array type
        Returns:
        A converted array
        Throws:
        DataTypeException - - When the conversion is not possible
      • convertCollection

        public static final <U> U[] convertCollection​(java.util.Collection from,
                                                      java.lang.Class<? extends U[]> to)
      • convert

        public static final <U> U convert​(java.lang.Object from,
                                          Converter<?,​? extends U> converter)
                                   throws DataTypeException
        Convert an object to a type.
        Parameters:
        from - The source object
        converter - The data type converter
        Returns:
        The target type object
        Throws:
        DataTypeException - - When the conversion is not possible
      • convert

        public static final <T> T convert​(java.lang.Object from,
                                          java.lang.Class<? extends T> toClass)
                                   throws DataTypeException
        Convert an object to a type. These are the conversion rules:
        • null is always converted to null, or the primitive default value, or Optional.empty(), regardless of the target type.
        • Identity conversion (converting a value to its own type) is always possible.
        • Primitive types can be converted to their wrapper types and vice versa
        • All types can be converted to String
        • All types can be converted to Object
        • All Number types can be converted to other Number types
        • All Number or String types can be converted to Boolean. Possible (case-insensitive) values for true:
          • 1
          • 1.0
          • y
          • yes
          • true
          • on
          • enabled

          Possible (case-insensitive) values for false:

          • 0
          • 0.0
          • n
          • no
          • false
          • off
          • disabled

          All other values evaluate to null

        • All Date subtypes (Date, Time, Timestamp), as well as most Temporal subtypes ( LocalDate, LocalTime, LocalDateTime, OffsetTime, OffsetDateTime, as well as Instant) can be converted into each other.
        • All String types can be converted into URI, URL and File
        • byte[] can be converted into String, using the platform's default charset
        • Object[] can be converted into any other array type, if array elements can be converted, too
        • All other combinations that are not listed above will result in a DataTypeException
        Parameters:
        from - The object to convert
        toClass - The target type
        Returns:
        The converted object
        Throws:
        DataTypeException - - When the conversion is not possible
      • convert

        public static final <T> java.util.List<T> convert​(java.util.Collection<?> collection,
                                                          java.lang.Class<? extends T> type)
                                                   throws DataTypeException
        Convert a collection of objects to a list of T, using convert(Object, Class)
        Parameters:
        collection - The list of objects
        type - The target type
        Returns:
        The list of converted objects
        Throws:
        DataTypeException - - When the conversion is not possible
        See Also:
        convert(Object, Class)
      • convert

        public static final <U> java.util.List<U> convert​(java.util.Collection<?> collection,
                                                          Converter<?,​? extends U> converter)
                                                   throws DataTypeException
        Convert a collection of objects to a list of T, using convert(Object, Converter)
        Parameters:
        collection - The collection of objects
        converter - The data type converter
        Returns:
        The list of converted objects
        Throws:
        DataTypeException - - When the conversion is not possible
        See Also:
        convert(Object, Converter)