Class PropertyConverter
java.lang.Object
org.apache.commons.configuration2.convert.PropertyConverter
A utility class to convert the configuration properties into any type.
- Since:
- 2.8.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
Constant for the prefix of binary numbers.private static final int
Constant for the radix of binary numbers.private static final Class<?>[]
Constant for the argument classes of the Number constructor that takes a String.private static final String
Constant for the prefix of hex numbers.private static final int
Constant for the radix of hex numbers.private static final String
The fully qualified name ofjavax.mail.internet.InternetAddress
, as used in the javamail-2.0+ API.private static final String
The fully qualified name ofjavax.mail.internet.InternetAddress
, as used in the javamail-1.* API. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Private constructor prevents instances from being created. -
Method Summary
Modifier and TypeMethodDescriptionprivate static Object
convertToEnum
(Class<?> enumClass, Object value) Helper method for converting a value to a constant of an enumeration class.(package private) static boolean
Calls Class.isEnum() on Java 5, returns false on older JRE.static Object
to
(Class<?> cls, Object value, DefaultConversionHandler convHandler) Performs a data type conversion from the specified value object to the given target data class.static BigDecimal
toBigDecimal
(Object value) Convert the specified object into a BigDecimal.static BigInteger
toBigInteger
(Object value) Convert the specified object into a BigInteger.static Boolean
Convert the specified object into a Boolean.static Byte
Convert the specified object into a Byte.static Calendar
toCalendar
(Object value, String format) Convert the specified object into a Calendar.static Character
toCharacter
(Object value) Converts the specified value object to aCharacter
.static Color
Convert the specified object into a Color.static Date
Convert the specified object into a Date.static Double
Convert the specified object into a Double.static Duration
toDuration
(Object value) Convert the specified object into a Duration.(package private) static <E extends Enum<E>>
EConvert the specified value into a Java 5 enum.static File
Convert the specified object into a File.static Float
Convert the specified object into a Float.(package private) static InetAddress
toInetAddress
(Object value) Convert the specified value into an internet address.static Integer
Convert the specified object into an Integer.(package private) static Object
toInternetAddress
(Object value, String targetClassName) Convert the specified value into an email address with the given class name.static Locale
Convert the specified object into a Locale.static Long
Convert the specified object into a Long.(package private) static Number
Tries to convert the specified object into a number object.static Path
Convert the specified object into a Path.static Pattern
Convert the specified object into a Pattern.static Short
Convert the specified object into a Short.static URI
Convert the specified object into an URI.static URL
Convert the specified object into an URL.
-
Field Details
-
HEX_PREFIX
Constant for the prefix of hex numbers.- See Also:
-
HEX_RADIX
private static final int HEX_RADIXConstant for the radix of hex numbers.- See Also:
-
BIN_PREFIX
Constant for the prefix of binary numbers.- See Also:
-
BIN_RADIX
private static final int BIN_RADIXConstant for the radix of binary numbers.- See Also:
-
CONSTR_ARGS
Constant for the argument classes of the Number constructor that takes a String. -
INTERNET_ADDRESS_CLASSNAME_JAVAX
The fully qualified name ofjavax.mail.internet.InternetAddress
, as used in the javamail-1.* API.- See Also:
-
INTERNET_ADDRESS_CLASSNAME_JAKARTA
The fully qualified name ofjavax.mail.internet.InternetAddress
, as used in the javamail-2.0+ API.- See Also:
-
-
Constructor Details
-
PropertyConverter
private PropertyConverter()Private constructor prevents instances from being created.
-
-
Method Details
-
to
public static Object to(Class<?> cls, Object value, DefaultConversionHandler convHandler) throws ConversionException Performs a data type conversion from the specified value object to the given target data class. If additional information is required for this conversion, it is obtained from the passed inDefaultConversionHandler
object. If the class is a primitive type (Integer.TYPE, Boolean.TYPE, etc), the value returned will use the wrapper type (Integer.class, Boolean.class, etc).- Parameters:
cls
- the target class of the converted valuevalue
- the value to convertconvHandler
- the conversion handler object- Returns:
- the converted value
- Throws:
ConversionException
- if the value is not compatible with the requested type
-
toBoolean
Convert the specified object into a Boolean. Internally theorg.apache.commons.lang.BooleanUtils
class from the Commons Lang project is used to perform this conversion. This class accepts some more tokens for the boolean value of true, e.g.yes
andon
. Please refer to the documentation of this class for more details.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a boolean
-
toCharacter
Converts the specified value object to aCharacter
. This method converts the passed in object to a string. If the string has exactly one character, this character is returned as result. Otherwise, conversion fails.- Parameters:
value
- the value to be converted- Returns:
- the resulting
Character
object - Throws:
ConversionException
- if the conversion is not possible
-
toByte
Convert the specified object into a Byte.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a byte
-
toShort
Convert the specified object into a Short.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a short
-
toInteger
Convert the specified object into an Integer.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to an integer
-
toLong
Convert the specified object into a Long.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a Long
-
toFloat
Convert the specified object into a Float.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a Float
-
toDouble
Convert the specified object into a Double.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a Double
-
toDuration
Convert the specified object into a Duration.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a Duration- Since:
- 2.8.0
-
toBigInteger
Convert the specified object into a BigInteger.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a BigInteger
-
toBigDecimal
Convert the specified object into a BigDecimal.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a BigDecimal
-
toNumber
Tries to convert the specified object into a number object. This method is used by the conversion methods for number types. Note that the return value is not in always of the specified target class, but only if a new object has to be created.- Parameters:
value
- the value to be converted (must not be null)targetClass
- the target class of the conversion (must be derived fromNumber
)- Returns:
- the converted number
- Throws:
ConversionException
- if the object cannot be converted
-
toFile
Convert the specified object into a File.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a File- Since:
- 2.3
-
toPath
Convert the specified object into a Path.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a Path- Since:
- 2.3
-
toURI
Convert the specified object into an URI.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to an URI
-
toURL
Convert the specified object into an URL.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to an URL
-
toPattern
Convert the specified object into a Pattern.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a Pattern
-
toLocale
Convert the specified object into a Locale.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a Locale
-
toColor
Convert the specified object into a Color. If the value is a String, the format allowed is (#)?[0-9A-F]{6}([0-9A-F]{2})?. Examples:- FF0000 (red)
- 0000FFA0 (semi transparent blue)
- #CCCCCC (gray)
- #00FF00A0 (semi transparent green)
- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a Color
-
toInetAddress
Convert the specified value into an internet address.- Parameters:
value
- the value to convert- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a InetAddress- Since:
- 1.5
-
toInternetAddress
Convert the specified value into an email address with the given class name.- Parameters:
value
- the value to converttargetClassName
- the fully qualified name of theInternetAddress
class to convert to, e.g., "javax.mail.internet.InternetAddress" or "javax.mail.internet.InternetAddress"- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to an email address- Since:
- 1.5
-
isEnum
Calls Class.isEnum() on Java 5, returns false on older JRE. -
toEnum
Convert the specified value into a Java 5 enum.- Parameters:
value
- the value to convertcls
- the type of the enumeration- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to an enumeration- Since:
- 1.5
-
toDate
Convert the specified object into a Date.- Parameters:
value
- the value to convertformat
- the DateFormat pattern to parse String values- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a Calendar
-
toCalendar
Convert the specified object into a Calendar.- Parameters:
value
- the value to convertformat
- the DateFormat pattern to parse String values- Returns:
- the converted value
- Throws:
ConversionException
- thrown if the value cannot be converted to a Calendar
-
convertToEnum
Helper method for converting a value to a constant of an enumeration class.- Parameters:
enumClass
- the enumeration classvalue
- the value to be converted- Returns:
- the converted value
-