Class FromBasedConverter<T>

  • Type Parameters:
    T - the built type.
    All Implemented Interfaces:
    Converter<T>

    public final class FromBasedConverter<T>
    extends Object
    implements Converter<T>
    This 'default' converter tries to create objects using a static 'from' method taking a single String argument. This converter is particularly convenient for builders.
    Author:
    Clement Escoffier
    • Method Detail

      • getIfEligible

        public static <T> FromBasedConverter<T> getIfEligible​(Class<T> clazz)
        Checks whether the given class can be used by the FromBasedConverter (i.e. has a static 'from' method taking a single String as argument). If so, creates a new instance of converter for this type.
        Parameters:
        clazz - the class
        Returns:
        a FromBasedConverter if the given class is eligible, null otherwise.
      • fromString

        public T fromString​(String input)
                     throws IllegalArgumentException
        Converts the given input to an object by using the 'from' method. Notice that the method may receive a null value.
        Specified by:
        fromString in interface Converter<T>
        Parameters:
        input - the input, can be null
        Returns:
        the instance of T
        Throws:
        IllegalArgumentException - if the instance of T cannot be created from the input.