Class DefaultConverter

java.lang.Object
org.jhotdraw8.base.converter.DefaultConverter
All Implemented Interfaces:
Converter<Object>

public class DefaultConverter extends Object implements Converter<Object>
Converts an Object to a String but can not a String back to an Object.

This converter is not bijective, and thus only useful for one-way conversions to a String. For example for generating a message text.

  • The conversion to string is performed by invoking the toString method on the value object.
  • The fromString method always returns a String object regardless of the type that was converted toString.
  • If a null value is converted to string, the string contains the text "null". On the conversion from string, a String object with the value "null" is returned.
Author:
Werner Randelshofer
  • Constructor Details

    • DefaultConverter

      public DefaultConverter()
  • Method Details

    • fromString

      public @Nullable Object fromString(@NonNull CharBuffer buf, @Nullable IdResolver idResolver)
      Description copied from interface: Converter
      Constructs a value from a string.

      The converter should try to create the value greedily, by consuming as many characters as possible for the value.

      This method does not change the state of the converter.

      Specified by:
      fromString in interface Converter<Object>
      Parameters:
      buf - A char buffer which holds the string. The char buffer must be treated as read only! The position of the char buffer denotes the beginning of the string when this method is invoked. After completion of this method, the position is set after the last consumed character.
      idResolver - The factory for looking up object ids. Nullable for non-resolving converters.
      Returns:
      The value. Nullable.
    • toString

      public void toString(@NonNull Appendable out, @Nullable IdSupplier idSupplier, @Nullable Object value) throws IOException
      Description copied from interface: Converter
      Converts a value to a string and appends it to the provided Appendable.

      This method does not change the state of the converter.

      Specified by:
      toString in interface Converter<Object>
      Parameters:
      out - The appendable
      idSupplier - The factory for creating object ids. Nullable for non-resolving converters.
      value - The value. Nullable.
      Throws:
      IOException - thrown by Appendable
    • getDefaultValue

      public @Nullable String getDefaultValue()
      Description copied from interface: Converter
      Provides a default value for APIs which always require a value even if conversion from String failed.
      Specified by:
      getDefaultValue in interface Converter<Object>
      Returns:
      The default value to use when conversion from String failed.