Interface Converter<T,​R>

  • Type Parameters:
    T - the type of the input to the converter
    R - the type of the result of the converter
    All Superinterfaces:
    Function<T,​R>
    All Known Subinterfaces:
    BinarySerializer<T>, Serializer<T,​U>, StringSerializer<T>
    All Known Implementing Classes:
    AbstractBinarySerializer, AbstractStringSerializer

    @ParametersAreNonnullByDefault
    public interface Converter<T,​R>
    extends Function<T,​R>
    A specialized Function that converts an object of type T to another of type R.

    The reverse operation may be strict inverse, meaning that converter.revert(converter.convert(a)).equals(a) always true.

    See Also:
    Function
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default R apply​(T t)  
      static <T,​U,​R>
      Converter<T,​R>
      compose​(Converter<T,​U> before, Converter<U,​R> after)
      Returns a composed converter that first applies the before function to its input, and then applies the after function to the result.
      R convert​(T t)
      Returns a representation of t as an instance of type R.
      static <T,​R>
      Converter<T,​R>
      from​(Function<? super T,​? extends R> convertFunc, Function<? super R,​? extends T> revertFunc)
      Creates a converter based on separate forward and backward functions.
      static <T> Converter<T,​T> identity()
      Creates a converter that always converts or reverses an object to itself.
      default Converter<R,​T> reverse()
      Returns the reversed converter of this converter.
      T revert​(R r)
      Returns a representation of r as an instance of type T.
    • Method Detail

      • identity

        @Nonnull
        static <T> Converter<T,​T> identity()
        Creates a converter that always converts or reverses an object to itself.
        Type Parameters:
        T - the type of the converted instance and the result
        Returns:
        a new converter
      • from

        @Nonnull
        static <T,​R> Converter<T,​R> from​(Function<? super T,​? extends R> convertFunc,
                                                     Function<? super R,​? extends T> revertFunc)
        Creates a converter based on separate forward and backward functions.
        Type Parameters:
        T - the type of the input to the converter
        R - the type of the result of the converter
        Parameters:
        convertFunc - the function used for convert(Object)
        revertFunc - the function used for revert(Object)
        Returns:
        a new converter
      • compose

        @Nonnull
        static <T,​U,​R> Converter<T,​R> compose​(Converter<T,​U> before,
                                                                Converter<U,​R> after)
        Returns a composed converter that first applies the before function to its input, and then applies the after function to the result. This behavior is inverted when calling revert(Object).
        Type Parameters:
        T - the type of input to the before converter, and to the composed function
        U - the intermediate type
        R - the type of output of the after converter, and of the composed function
        Parameters:
        before - the first converter
        after - the second converter
        Returns:
        a composed converter
      • apply

        default R apply​(T t)
        Specified by:
        apply in interface Function<T,​R>
      • convert

        R convert​(T t)
        Returns a representation of t as an instance of type R.
        Parameters:
        t - the instance to convert
        Returns:
        the converted instance
      • revert

        T revert​(R r)
        Returns a representation of r as an instance of type T.
        Parameters:
        r - the instance to convert
        Returns:
        the converted instance
      • reverse

        @Nonnull
        default Converter<R,​T> reverse()
        Returns the reversed converter of this converter.
        Returns:
        the reversed converter