Class FieldParser

java.lang.Object
org.apache.pulsar.common.util.FieldParser

public final class FieldParser extends Object
Generic value converter.

Use examples

 String o1 = String.valueOf(1);
 ;
 Integer i = FieldParser.convert(o1, Integer.class);
 System.out.println(i); // 1

 
  • Constructor Details

    • FieldParser

      public FieldParser()
  • Method Details

    • convert

      public static <T> T convert(Object from, Class<T> to)
      Convert the given object value to the given class.
      Parameters:
      from - The object value to be converted.
      to - The type class which the given object should be converted to.
      Returns:
      The converted object value.
      Throws:
      UnsupportedOperationException - If no suitable converter can be found.
      RuntimeException - If conversion failed somehow. This can be caused by at least an ExceptionInInitializerError, IllegalAccessException or InvocationTargetException.
    • update

      public static <T> void update(Map<String,String> properties, T obj) throws IllegalArgumentException
      Update given Object attribute by reading it from provided map properties.
      Parameters:
      properties - which key-value pair of properties to assign those values to given object
      obj - object which needs to be updated
      Throws:
      IllegalArgumentException - if the properties key-value contains incorrect value type
    • value

      public static Object value(String strValue, Field field)
      Converts value as per appropriate DataType of the field.
      Parameters:
      strValue - : string value of the object
      field - : field of the attribute
      Returns:
    • setEmptyValue

      public static <T> void setEmptyValue(String strValue, Field field, T obj) throws IllegalArgumentException, IllegalAccessException
      Sets the empty/null value if field is allowed to be set empty.
      Parameters:
      strValue -
      field -
      obj -
      Throws:
      IllegalArgumentException
      IllegalAccessException
    • stringToInteger

      public static Integer stringToInteger(String val)
      Converts String to Integer.
      Parameters:
      val - The String to be converted.
      Returns:
      The converted Integer value.
    • stringToLong

      public static Long stringToLong(String val)
      Converts String to Long.
      Parameters:
      val - The String to be converted.
      Returns:
      The converted Long value.
    • stringToDouble

      public static Double stringToDouble(String val)
      Converts String to Double.
      Parameters:
      val - The String to be converted.
      Returns:
      The converted Double value.
    • stringToFloat

      public static Float stringToFloat(String val)
      Converts String to float.
      Parameters:
      val - The String to be converted.
      Returns:
      The converted Double value.
    • stringToList

      public static <T> List<T> stringToList(String val, Class<T> type)
      Converts comma separated string to List.
      Type Parameters:
      T - type of list
      Parameters:
      val - comma separated values.
      Returns:
      The converted list with type <T>.
    • stringToSet

      public static <T> Set<T> stringToSet(String val, Class<T> type)
      Converts comma separated string to Set.
      Type Parameters:
      T - type of set
      Parameters:
      val - comma separated values.
      Returns:
      The converted set with type <T>.
    • integerToString

      public static String integerToString(Integer value)
      Converts Integer to String.
      Parameters:
      value - The Integer to be converted.
      Returns:
      The converted String value.
    • booleanToString

      public static String booleanToString(Boolean value)
      Converts Boolean to String.
      Parameters:
      value - The Boolean to be converted.
      Returns:
      The converted String value.
    • stringToBoolean

      public static Boolean stringToBoolean(String value)
      Converts String to Boolean.
      Parameters:
      value - The String to be converted.
      Returns:
      The converted Boolean value.