Class BasicConverter
java.lang.Object
org.apache.juneau.commons.conversion.CachingConverter
org.apache.juneau.commons.conversion.BasicConverter
- All Implemented Interfaces:
Converter
- Direct Known Subclasses:
ConfigurableConverter
A concrete
CachingConverter that supports common type conversions without
requiring a MarshallingContext or MarshallingSession.
The following conversions are supported:
| Input type | Output type | Notes |
|---|---|---|
| Any type | Same or supertype | Identity / widening (no-op cast) |
Number |
Integer, Long, Short, Float, Double, Byte,
AtomicInteger, AtomicLong, BigDecimal, BigInteger,
and primitive equivalents
|
Narrowing/widening numeric conversion |
Boolean |
Number types (including BigDecimal, BigInteger) |
|
CharSequence |
Number types |
Parsed via StringUtils.parseNumber(String, Class);
empty string or "null" returns |
Number |
Boolean |
|
CharSequence |
Boolean |
Boolean.valueOf(String) |
CharSequence (length 1) |
Character |
|
Number |
Character |
First character of toString() (e.g. 65 → '6') |
| Any | String |
Object.toString(), with array support via Arrays.toString(Object[]) |
CharSequence |
byte[] |
UTF-8 encoding via String.getBytes(Charset) |
CharSequence |
Any Enum |
Enum.valueOf(Class, String) |
Collection or array |
Collection subtype |
Copies elements; element type from |
Map |
Map subtype |
Copies entries; key type from |
Collection or array |
Array type | Element type from |
String |
TimeZone |
TimeZone.getTimeZone(String) |
TimeZone |
String |
TimeZone.getID() |
String |
Locale |
Locale.forLanguageTag(String) (underscores converted to hyphens) |
| Any | Any with |
Reflection-based static factory lookup |
| Any | Any with |
Reflection-based constructor lookup |
| Any with |
Any type X | Instance method where name matches |
Thread Safety:
This class is thread-safe. The singleton instance can be safely shared across multiple threads.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected <I,O> Conversion<I, O> findConversion(Class<I> inType, Class<O> outType) Finds a conversion function from the specified input type to the specified output type.Methods inherited from class org.apache.juneau.commons.conversion.CachingConverter
canConvert, to, to, to, to
-
Field Details
-
INSTANCE
Singleton instance.
-
-
Constructor Details
-
BasicConverter
protected BasicConverter()Constructor.
-
-
Method Details
-
findConversion
Description copied from class:CachingConverterFinds a conversion function from the specified input type to the specified output type.This method is called lazily the first time a conversion between a given type pair is requested. The result is cached so subsequent calls for the same type pair bypass this method entirely. When this method returns
null , a sentinel is stored so that unconvertable type pairs are also only evaluated once.Implementations should return
null if no conversion is possible for the given types, which will causeCachingConverter.to(Object, Class)andCachingConverter.to(Object, Type, Type...)to throwInvalidConversionExceptionfor that type pair.- Specified by:
findConversionin classCachingConverter- Type Parameters:
I- The input type.O- The output type.- Parameters:
inType- The runtime class of the input object.outType- The target output class.- Returns:
- A
Conversionfunction, ornull if no conversion is available.
-