Class BaseConverter<From,To>

Type Parameters:
From - The type to convert from
To - The type to convert to
All Implemented Interfaces:
Converter<From,To>, Broadcaster, Listener, Repeater, Transceiver, Receiver, Transmitter, NamedObject
Direct Known Subclasses:
BaseStringConverter, BaseTwoWayConverter, ObjectConverter

public abstract class BaseConverter<From,To> extends BaseRepeater implements Converter<From,To>
Base class for implementing converters. The inherited Converter.convert(Object) method converts from the 'From' type to the To type. Whether the conversion allows null values or not can be specified with allowNull(boolean). Converters are used extensively in KivaKit for tasks as diverse as switch parsing and populating objects from properties files.

Since this class extends BaseRepeater and implements the Repeater interface, it can both hear messages broadcast by sub-converters and broadcast messages about the conversion itself.

For example, if there is a problem with a conversion, Transceiver.problem(String, Object...) (which Repeater indirectly extends) can broadcast a Problem message to any clients of the converter. Similarly Warning and Glitch messages can be broadcast with Transceiver.warning(String, Object...) and Transceiver.glitch(String, Object...).

Author:
jonathanl (shibo)
See Also:
  • Constructor Details

    • BaseConverter

      protected BaseConverter(Listener listener)
  • Method Details

    • allowNull

      public BaseConverter<From,To> allowNull(boolean allowNull)
      Parameters:
      allowNull - True if null values should be converted to null
    • allowsNull

      public boolean allowsNull()
      Returns:
      True if this converter allows null values, false if a problem will be broadcast when a null value is encountered.
    • convert

      public final To convert(From from)
      Converts from the <From> type to the <To> type. If the 'from' value is null and the converter allows null values, null will be returned. If the value is null and the converter does not allow null values a problem will be broadcast. Any exceptions that occur during conversion are caught and broadcast as problems.
      Specified by:
      convert in interface Converter<From,To>
      Parameters:
      from - The value to convert
      Returns:
      The converted value
    • onConvert

      protected abstract To onConvert(From value)
      The method to override to provide the conversion
    • problemBroadcastFrequency

      protected Frequency problemBroadcastFrequency()
      Returns:
      The maximum Frequency to broadcast problems at, for example, every minute or once an hour
    • subclass

      protected Class<?> subclass()