Class TypeConverter

java.lang.Object
com.helger.commons.typeconvert.TypeConverter
All Implemented Interfaces:
IHasConditionalLogger

@Immutable public final class TypeConverter extends Object implements IHasConditionalLogger
Helper class for converting base types likes "boolean" to object types like "Boolean".
Uses TypeConverterRegistry.getFuzzyConverter(Class, Class) for retrieving a registered converter. If no converter is found, it is checked whether a mapping from a primitive type to an object type exists.
Author:
Philip Helger
  • Method Summary

    Modifier and Type
    Method
    Description
    static <DSTTYPE> DSTTYPE
    convert(boolean aSrcValue, Class<DSTTYPE> aDstClass)
     
    static <DSTTYPE> DSTTYPE
    convert(byte aSrcValue, Class<DSTTYPE> aDstClass)
     
    static <DSTTYPE> DSTTYPE
    convert(char aSrcValue, Class<DSTTYPE> aDstClass)
     
    static <DSTTYPE> DSTTYPE
    convert(double aSrcValue, Class<DSTTYPE> aDstClass)
     
    static <DSTTYPE> DSTTYPE
    convert(float aSrcValue, Class<DSTTYPE> aDstClass)
     
    static <DSTTYPE> DSTTYPE
    convert(int aSrcValue, Class<DSTTYPE> aDstClass)
     
    static <DSTTYPE> DSTTYPE
    convert(long aSrcValue, Class<DSTTYPE> aDstClass)
     
    static <DSTTYPE> DSTTYPE
    convert(short aSrcValue, Class<DSTTYPE> aDstClass)
     
    static <DSTTYPE> DSTTYPE
    convert(ITypeConverterProvider aTypeConverterProvider, Object aSrcValue, Class<DSTTYPE> aDstClass)
    Convert the passed source value to the destination class, if a conversion is necessary.
    static <DSTTYPE> DSTTYPE
    convert(ITypeConverterProvider aTypeConverterProvider, Object aSrcValue, Class<DSTTYPE> aDstClass, DSTTYPE aDefault)
    Convert the passed source value to the destination class, if a conversion is necessary.
    static <DSTTYPE> DSTTYPE
    convert(Object aSrcValue, Class<DSTTYPE> aDstClass)
    Convert the passed source value to the destination class using the best match type converter provider, if a conversion is necessary.
    static <DSTTYPE> DSTTYPE
    convert(Object aSrcValue, Class<DSTTYPE> aDstClass, DSTTYPE aDefault)
    Convert the passed source value to the destination class using the best match type converter provider, if a conversion is necessary.
    static boolean
    Convert the passed source value to boolean
    static boolean
    convertToBoolean(Object aSrcValue, boolean bDefault)
    Convert the passed source value to boolean
    static byte
    convertToByte(Object aSrcValue)
    Convert the passed source value to byte
    static byte
    convertToByte(Object aSrcValue, byte nDefault)
    Convert the passed source value to byte
    static char
    convertToChar(Object aSrcValue)
    Convert the passed source value to char
    static char
    convertToChar(Object aSrcValue, char cDefault)
    Convert the passed source value to char
    static double
    Convert the passed source value to double
    static double
    convertToDouble(Object aSrcValue, double dDefault)
    Convert the passed source value to double
    static float
    Convert the passed source value to float
    static float
    convertToFloat(Object aSrcValue, float fDefault)
    Convert the passed source value to float
    static int
    convertToInt(Object aSrcValue)
    Convert the passed source value to int
    static int
    convertToInt(Object aSrcValue, int nDefault)
    Convert the passed source value to int
    static long
    convertToLong(Object aSrcValue)
    Convert the passed source value to long
    static long
    convertToLong(Object aSrcValue, long nDefault)
    Convert the passed source value to long
    static short
    Convert the passed source value to short
    static short
    convertToShort(Object aSrcValue, short nDefault)
    Convert the passed source value to short
    static boolean
     
    static boolean
    setSilentMode(boolean bSilentMode)
    Enable or disable certain regular log messages.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isSilentMode

      public static boolean isSilentMode()
      Returns:
      true if logging is disabled, false if it is enabled.
      Since:
      9.4.0
    • setSilentMode

      public static boolean setSilentMode(boolean bSilentMode)
      Enable or disable certain regular log messages.
      Parameters:
      bSilentMode - true to disable logging, false to enable logging
      Returns:
      The previous value of the silent mode.
      Since:
      9.4.0
    • convert

      @Nullable public static <DSTTYPE> DSTTYPE convert(boolean aSrcValue, @Nonnull Class<DSTTYPE> aDstClass)
    • convert

      @Nullable public static <DSTTYPE> DSTTYPE convert(byte aSrcValue, @Nonnull Class<DSTTYPE> aDstClass)
    • convert

      @Nullable public static <DSTTYPE> DSTTYPE convert(char aSrcValue, @Nonnull Class<DSTTYPE> aDstClass)
    • convert

      @Nullable public static <DSTTYPE> DSTTYPE convert(double aSrcValue, @Nonnull Class<DSTTYPE> aDstClass)
    • convert

      @Nullable public static <DSTTYPE> DSTTYPE convert(float aSrcValue, @Nonnull Class<DSTTYPE> aDstClass)
    • convert

      @Nullable public static <DSTTYPE> DSTTYPE convert(int aSrcValue, @Nonnull Class<DSTTYPE> aDstClass)
    • convert

      @Nullable public static <DSTTYPE> DSTTYPE convert(long aSrcValue, @Nonnull Class<DSTTYPE> aDstClass)
    • convert

      @Nullable public static <DSTTYPE> DSTTYPE convert(short aSrcValue, @Nonnull Class<DSTTYPE> aDstClass)
    • convertToBoolean

      public static boolean convertToBoolean(@Nonnull Object aSrcValue)
      Convert the passed source value to boolean
      Parameters:
      aSrcValue - The source value. May not be null.
      Returns:
      The converted value.
      Throws:
      TypeConverterException - if the source value is null or if no converter was found or if the converter returned a null object.
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToBoolean

      public static boolean convertToBoolean(@Nullable Object aSrcValue, boolean bDefault)
      Convert the passed source value to boolean
      Parameters:
      aSrcValue - The source value. May be null.
      bDefault - The default value to be returned if an error occurs during type conversion.
      Returns:
      null if the source value was null.
      Throws:
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToByte

      public static byte convertToByte(@Nonnull Object aSrcValue)
      Convert the passed source value to byte
      Parameters:
      aSrcValue - The source value. May not be null.
      Returns:
      The converted value.
      Throws:
      TypeConverterException - if the source value is null or if no converter was found or if the converter returned a null object.
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToByte

      public static byte convertToByte(@Nullable Object aSrcValue, byte nDefault)
      Convert the passed source value to byte
      Parameters:
      aSrcValue - The source value. May be null.
      nDefault - The default value to be returned if an error occurs during type conversion.
      Returns:
      The converted value.
      Throws:
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToChar

      public static char convertToChar(@Nonnull Object aSrcValue)
      Convert the passed source value to char
      Parameters:
      aSrcValue - The source value. May not be null.
      Returns:
      The converted value.
      Throws:
      TypeConverterException - if the source value is null or if no converter was found or if the converter returned a null object.
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToChar

      public static char convertToChar(@Nullable Object aSrcValue, char cDefault)
      Convert the passed source value to char
      Parameters:
      aSrcValue - The source value. May be null.
      cDefault - The default value to be returned if an error occurs during type conversion.
      Returns:
      The converted value.
      Throws:
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToDouble

      public static double convertToDouble(@Nonnull Object aSrcValue)
      Convert the passed source value to double
      Parameters:
      aSrcValue - The source value. May not be null.
      Returns:
      The converted value.
      Throws:
      TypeConverterException - if the source value is null or if no converter was found or if the converter returned a null object.
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToDouble

      public static double convertToDouble(@Nullable Object aSrcValue, double dDefault)
      Convert the passed source value to double
      Parameters:
      aSrcValue - The source value. May be null.
      dDefault - The default value to be returned if an error occurs during type conversion.
      Returns:
      The converted value.
      Throws:
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToFloat

      public static float convertToFloat(@Nonnull Object aSrcValue)
      Convert the passed source value to float
      Parameters:
      aSrcValue - The source value. May not be null.
      Returns:
      The converted value.
      Throws:
      TypeConverterException - if the source value is null or if no converter was found or if the converter returned a null object.
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToFloat

      public static float convertToFloat(@Nullable Object aSrcValue, float fDefault)
      Convert the passed source value to float
      Parameters:
      aSrcValue - The source value. May be null.
      fDefault - The default value to be returned if an error occurs during type conversion.
      Returns:
      The converted value.
      Throws:
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToInt

      public static int convertToInt(@Nonnull Object aSrcValue)
      Convert the passed source value to int
      Parameters:
      aSrcValue - The source value. May not be null.
      Returns:
      The converted value.
      Throws:
      TypeConverterException - if the source value is null or if no converter was found or if the converter returned a null object.
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToInt

      public static int convertToInt(@Nullable Object aSrcValue, int nDefault)
      Convert the passed source value to int
      Parameters:
      aSrcValue - The source value. May be null.
      nDefault - The default value to be returned if an error occurs during type conversion.
      Returns:
      The converted value.
      Throws:
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToLong

      public static long convertToLong(@Nonnull Object aSrcValue)
      Convert the passed source value to long
      Parameters:
      aSrcValue - The source value. May not be null.
      Returns:
      The converted value.
      Throws:
      TypeConverterException - if the source value is null or if no converter was found or if the converter returned a null object.
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToLong

      public static long convertToLong(@Nullable Object aSrcValue, long nDefault)
      Convert the passed source value to long
      Parameters:
      aSrcValue - The source value. May be null.
      nDefault - The default value to be returned if an error occurs during type conversion.
      Returns:
      The converted value.
      Throws:
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToShort

      public static short convertToShort(@Nonnull Object aSrcValue)
      Convert the passed source value to short
      Parameters:
      aSrcValue - The source value. May not be null.
      Returns:
      The converted value.
      Throws:
      TypeConverterException - if the source value is null or if no converter was found or if the converter returned a null object.
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convertToShort

      public static short convertToShort(@Nullable Object aSrcValue, short nDefault)
      Convert the passed source value to short
      Parameters:
      aSrcValue - The source value. May be null.
      nDefault - The default value to be returned if an error occurs during type conversion.
      Returns:
      The converted value.
      Throws:
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convert

      @Nullable public static <DSTTYPE> DSTTYPE convert(@Nullable Object aSrcValue, @Nonnull Class<DSTTYPE> aDstClass)
      Convert the passed source value to the destination class using the best match type converter provider, if a conversion is necessary.
      Type Parameters:
      DSTTYPE - The destination type.
      Parameters:
      aSrcValue - The source value. May be null.
      aDstClass - The destination class to use.
      Returns:
      null if the source value was null.
      Throws:
      TypeConverterException - If no converter was found or if the converter returned a null object.
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convert

      @Nullable public static <DSTTYPE> DSTTYPE convert(@Nonnull ITypeConverterProvider aTypeConverterProvider, @Nullable Object aSrcValue, @Nonnull Class<DSTTYPE> aDstClass)
      Convert the passed source value to the destination class, if a conversion is necessary.
      Type Parameters:
      DSTTYPE - The destination type.
      Parameters:
      aTypeConverterProvider - The type converter provider. May not be null.
      aSrcValue - The source value. May be null.
      aDstClass - The destination class to use.
      Returns:
      null if the source value was null.
      Throws:
      TypeConverterException - If no converter was found or if the converter returned a null object.
      RuntimeException - If the converter itself throws an exception
    • convert

      @Nullable public static <DSTTYPE> DSTTYPE convert(@Nullable Object aSrcValue, @Nonnull Class<DSTTYPE> aDstClass, @Nullable DSTTYPE aDefault)
      Convert the passed source value to the destination class using the best match type converter provider, if a conversion is necessary.
      Type Parameters:
      DSTTYPE - The destination type.
      Parameters:
      aSrcValue - The source value. May be null.
      aDstClass - The destination class to use.
      aDefault - The default value to be returned, if an TypeConverterException occurs.
      Returns:
      null if the source value was null or the default value is null.
      Throws:
      RuntimeException - If the converter itself throws an exception
      See Also:
    • convert

      @Nullable public static <DSTTYPE> DSTTYPE convert(@Nonnull ITypeConverterProvider aTypeConverterProvider, @Nullable Object aSrcValue, @Nonnull Class<DSTTYPE> aDstClass, @Nullable DSTTYPE aDefault)
      Convert the passed source value to the destination class, if a conversion is necessary.
      Type Parameters:
      DSTTYPE - The destination type.
      Parameters:
      aTypeConverterProvider - The type converter provider. May not be null.
      aSrcValue - The source value. May be null.
      aDstClass - The destination class to use.
      aDefault - The default value to be returned, if an TypeConverterException occurs.
      Returns:
      null if the source value was null or the default value is null.
      Throws:
      RuntimeException - If the converter itself throws an exception