Interface Convert<S,T>

Type Parameters:
S - the "source" type of the conversion
T - the "target" type of the conversion

public interface Convert<S,T>
An infallible two-way conversion between types S and T.

When round-tripping a value of type T through S and back, the result must be equal to the original value. That is, Objects.equals(from(to(t)), t) must always be true.

  • Method Summary

    Modifier and Type
    Method
    Description
    static <S, T> Convert<S,T>
    between(Function<S,? extends T> from, Function<? super T,S> to)
    Constructs a conversion from two individual transformations.
    default <X> Convert<S,X>
    compose(Convert<T,X> other)
    Extends this two-way conversion to a new target type by chaining with another conversion.
    from(S source)
    Converts a value forward from the source type to the target type.
    static <T> Convert<T,T>
    Constructs a trivial conversion between a type and itself.
    default Convert<T,S>
    Inverts this two-way conversion, swapping its source and target types.
    to(T target)
    Converts a value backward to the source type from the target type.
  • Method Details

    • from

      T from(S source)
      Converts a value forward from the source type to the target type.
      Parameters:
      source - the value to convert
      Returns:
      the converted value
    • to

      S to(T target)
      Converts a value backward to the source type from the target type.
      Parameters:
      target - the value to convert
      Returns:
      the converted value
    • between

      static <S, T> Convert<S,T> between(Function<S,? extends T> from, Function<? super T,S> to)
      Constructs a conversion from two individual transformations. The first function must be a left inverse of the second, i.e. Objects.equals(from(to(t)), t) must always be true.
      Type Parameters:
      S - the "source" type of the conversion
      T - the "target" type of the conversion
      Parameters:
      from - an infallible transformation from the source type to the target type
      to - an infallible transformation to the source type from the target type
    • identity

      static <T> Convert<T,T> identity()
      Constructs a trivial conversion between a type and itself.

      This is most useful as an initial value when accumulating multiple conversions together.

      Type Parameters:
      T - the type to trivially convert into itself
      Returns:
      a trivial two-way conversion from a type to itself
    • compose

      default <X> Convert<S,X> compose(Convert<T,X> other)
      Extends this two-way conversion to a new target type by chaining with another conversion.
      Type Parameters:
      X - the new "target" type
      Parameters:
      other - a conversion from this target type to the new target type
      Returns:
      a combined conversion from this source type to the new target type
    • invert

      default Convert<T,S> invert()
      Inverts this two-way conversion, swapping its source and target types.
      Returns:
      a two-way conversion with the same logic as this one, but with its source and target types swapped