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

public class BasicConverter extends CachingConverter
A concrete CachingConverter that supports common type conversions without requiring a MarshallingContext or MarshallingSession.

The following conversions are supported:

Input typeOutput typeNotes
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) true=1, false=0
CharSequence Number types Parsed via StringUtils.parseNumber(String, Class); empty string or "null" returns null
Number Boolean intValue() != 0
CharSequence Boolean Boolean.valueOf(String)
CharSequence (length 1) Character charAt(0)
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 args[0]
Map Map subtype Copies entries; key type from args[0], value type from args[1]
Collection or array Array type Element type from outType.getComponentType()
String TimeZone TimeZone.getTimeZone(String)
TimeZone String TimeZone.getID()
String Locale Locale.forLanguageTag(String) (underscores converted to hyphens)
Any Any with static T fromString/valueOf/of/from/parse/create/forName/fromValue/builder(X) or dynamic fromX/forX/parseX where X is the input class name Reflection-based static factory lookup
Any Any with public T(X) constructor Reflection-based constructor lookup
Any with toX() instance method Any type X Instance method where name matches to + output class name (e.g. toInteger())
Thread Safety:

This class is thread-safe. The singleton instance can be safely shared across multiple threads.

  • Field Details

  • Constructor Details

  • Method Details

    • findConversion

      protected <I, O> Conversion<I,O> findConversion(Class<I> inType, Class<O> outType)
      Description copied from class: CachingConverter
      Finds 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 cause CachingConverter.to(Object, Class) and CachingConverter.to(Object, Type, Type...) to throw InvalidConversionException for that type pair.

      Specified by:
      findConversion in class CachingConverter
      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 Conversion function, or null if no conversion is available.