Class StringConverter<T>

All Implemented Interfaces:
Serializable, Function<String,T>, ObjectConverter<String,T>
Direct Known Subclasses:
StringConverter.Angle, StringConverter.BigDecimal, StringConverter.BigInteger, StringConverter.Boolean, StringConverter.Byte, StringConverter.Charset, StringConverter.CodeList, StringConverter.Double, StringConverter.Enum, StringConverter.File, StringConverter.Float, StringConverter.Integer, StringConverter.InternationalString, StringConverter.Locale, StringConverter.Long, StringConverter.Number, StringConverter.Path, StringConverter.Short, StringConverter.Unit, StringConverter.URI, StringConverter.URL

abstract class StringConverter<T> extends SystemConverter<String,T>
Handles conversions between String and various kinds of objects. Each inner class in this StringConverter class defines both the forward and the inverse converters.

Most converters are pretty close to bijective functions, but not exactly. For example, conversions from String to File is not completely bijective because various path separators ('/' and '\') produce the same File object.

Special cases

Conversion table from String to Boolean:
Conversion table from strings
source target
"true" Boolean.TRUE
"false" Boolean.FALSE
"yes" Boolean.TRUE
"no" Boolean.FALSE
"on" Boolean.TRUE
"off" Boolean.FALSE
"1" Boolean.TRUE
"0" Boolean.FALSE

Immutability and thread safety

This base class and all inner classes are immutable, and thus inherently thread-safe.
Since:
0.3
Version:
0.5
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      For cross-version compatibility.
      See Also:
    • inverse

      private final ObjectConverter<T,String> inverse
      The inverse converter from the target to the source class.
  • Constructor Details

    • StringConverter

      StringConverter(Class<T> targetClass)
      Creates a new converter for the given target class.
      Parameters:
      targetClass - the target class.
  • Method Details

    • createInverse

      ObjectConverter<T,String> createInverse()
      Invoked by the constructor for creating the inverse converter. To be overridden by classes which need a specialized instance.
    • properties

      public Set<FunctionProperty> properties()
      While this is not a general rule for surjective functions, all converters defined in this class are invertibles.
      Returns:
      the manners in which source values are mapped to target values. May be an empty set, but never null.
    • inverse

      public final ObjectConverter<T,String> inverse()
      Returns the inverse converter.
      Specified by:
      inverse in interface ObjectConverter<String,T>
      Overrides:
      inverse in class SystemConverter<String,T>
      Returns:
      a converter for converting instances of T back to instances of S.
      See Also:
    • apply

      public final T apply(String source) throws UnconvertibleObjectException
      Converts the given string to the target type of this converter. This method verifies that the given string is non-null and non-empty, then delegates to doConvert(String).
      Parameters:
      source - the string to convert, or null.
      Returns:
      the converted value, or null if the given string was null or empty.
      Throws:
      UnconvertibleObjectException - if an error occurred during the conversion.
    • doConvert

      abstract T doConvert(String source) throws Exception
      Invoked by apply(String) for converting the given string to the target type of this converter.
      Parameters:
      source - the string to convert, guaranteed to be non-null and non-empty.
      Returns:
      the converted value.
      Throws:
      Exception - if an error occurred during the conversion.