Package org.apache.pulsar.common.util
Class FieldParser
- java.lang.Object
-
- org.apache.pulsar.common.util.FieldParser
-
public final class FieldParser extends java.lang.Object
Generic value converter.Use examples
String o1 = String.valueOf(1); ; Integer i = FieldParser.convert(o1, Integer.class); System.out.println(i); // 1
-
-
Constructor Summary
Constructors Constructor Description FieldParser()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
booleanToString(java.lang.Boolean value)
Converts Boolean to String.static <T> T
convert(java.lang.Object from, java.lang.Class<T> to)
Convert the given object value to the given class.static java.lang.String
integerToString(java.lang.Integer value)
Converts Integer to String.static <T> void
setEmptyValue(java.lang.String strValue, java.lang.reflect.Field field, T obj)
Sets the empty/null value if field is allowed to be set empty.static java.lang.Boolean
stringToBoolean(java.lang.String value)
Converts String to Boolean.static java.lang.Double
stringToDouble(java.lang.String val)
Converts String to Double.static java.lang.Float
stringToFloat(java.lang.String val)
Converts String to float.static java.lang.Integer
stringToInteger(java.lang.String val)
Converts String to Integer.static <T> java.util.List<T>
stringToList(java.lang.String val, java.lang.Class<T> type)
Converts comma separated string to List.static java.lang.Long
stringToLong(java.lang.String val)
Converts String to Long.static <T> java.util.Set<T>
stringToSet(java.lang.String val, java.lang.Class<T> type)
Converts comma separated string to Set.static <T> void
update(java.util.Map<java.lang.String,java.lang.String> properties, T obj)
Update given Object attribute by reading it from provided map properties.static java.lang.Object
value(java.lang.String strValue, java.lang.reflect.Field field)
Converts value as per appropriate DataType of the field.
-
-
-
Method Detail
-
convert
public static <T> T convert(java.lang.Object from, java.lang.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:
java.lang.UnsupportedOperationException
- If no suitable converter can be found.java.lang.RuntimeException
- If conversion failed somehow. This can be caused by at least an ExceptionInInitializerError, IllegalAccessException or InvocationTargetException.
-
update
public static <T> void update(java.util.Map<java.lang.String,java.lang.String> properties, T obj) throws java.lang.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 objectobj
- object which needs to be updated- Throws:
java.lang.IllegalArgumentException
- if the properties key-value contains incorrect value type
-
value
public static java.lang.Object value(java.lang.String strValue, java.lang.reflect.Field field)
Converts value as per appropriate DataType of the field.- Parameters:
strValue
- : string value of the objectfield
- : field of the attribute- Returns:
-
setEmptyValue
public static <T> void setEmptyValue(java.lang.String strValue, java.lang.reflect.Field field, T obj) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException
Sets the empty/null value if field is allowed to be set empty.- Parameters:
strValue
-field
-obj
-- Throws:
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
-
stringToInteger
public static java.lang.Integer stringToInteger(java.lang.String val)
Converts String to Integer.- Parameters:
val
- The String to be converted.- Returns:
- The converted Integer value.
-
stringToLong
public static java.lang.Long stringToLong(java.lang.String val)
Converts String to Long.- Parameters:
val
- The String to be converted.- Returns:
- The converted Long value.
-
stringToDouble
public static java.lang.Double stringToDouble(java.lang.String val)
Converts String to Double.- Parameters:
val
- The String to be converted.- Returns:
- The converted Double value.
-
stringToFloat
public static java.lang.Float stringToFloat(java.lang.String val)
Converts String to float.- Parameters:
val
- The String to be converted.- Returns:
- The converted Double value.
-
stringToList
public static <T> java.util.List<T> stringToList(java.lang.String val, java.lang.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> java.util.Set<T> stringToSet(java.lang.String val, java.lang.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 java.lang.String integerToString(java.lang.Integer value)
Converts Integer to String.- Parameters:
value
- The Integer to be converted.- Returns:
- The converted String value.
-
booleanToString
public static java.lang.String booleanToString(java.lang.Boolean value)
Converts Boolean to String.- Parameters:
value
- The Boolean to be converted.- Returns:
- The converted String value.
-
stringToBoolean
public static java.lang.Boolean stringToBoolean(java.lang.String value)
Converts String to Boolean.- Parameters:
value
- The String to be converted.- Returns:
- The converted Boolean value.
-
-