Class FunctionalSwap<T,S>

java.lang.Object
org.apache.juneau.swap.ObjectSwap<T,S>
org.apache.juneau.swap.FunctionalSwap<T,S>
Type Parameters:
T - The normal form of the class.
S - The swapped form of the class.

public class FunctionalSwap<T,S> extends ObjectSwap<T,S>
A subclass of ObjectSwap that allows swap and unswap methods to be defined as functions.

// Example public class MyBeanSwap extends FunctionalSwap<MyBean,String> { public MyBeanSwap() { super(MyBean.class, String.class, x -> myStringifyier(x), x -> myDeStringifier(x)); } }

See Also:
  • Constructor Details

    • FunctionalSwap

      public FunctionalSwap(Class<T> normalClass, Class<S> swappedClass, ThrowingFunction<T,S> swapFunction)
      Constructor.
      Parameters:
      normalClass - The normal class.
      swappedClass - The swapped class.
      swapFunction - The function for converting from normal to swapped.
    • FunctionalSwap

      public FunctionalSwap(Class<T> normalClass, Class<S> swappedClass, ThrowingFunction<T,S> swapFunction, ThrowingFunction<S,T> unswapFunction)
      Constructor.
      Parameters:
      normalClass - The normal class.
      swappedClass - The swapped class.
      swapFunction - The function for converting from normal to swapped.
      unswapFunction - The function for converting swapped to normal.
  • Method Details

    • swap

      public S swap(BeanSession session, T o, String template) throws Exception
      Description copied from class: ObjectSwap
      Same as ObjectSwap.swap(BeanSession, Object), but can be used if your swap has a template associated with it.
      Overrides:
      swap in class ObjectSwap<T,S>
      Parameters:
      session - The bean session to use to get the class meta. This is always going to be the same bean context that created this swap.
      o - The object to be transformed.
      template - The template string associated with this swap.
      Returns:
      The transformed object.
      Throws:
      Exception - If a problem occurred trying to convert the output.
    • unswap

      public T unswap(BeanSession session, S f, ClassMeta<?> hint, String template) throws Exception
      Description copied from class: ObjectSwap
      Same as ObjectSwap.unswap(BeanSession, Object, ClassMeta), but can be used if your swap has a template associated with it.
      Overrides:
      unswap in class ObjectSwap<T,S>
      Parameters:
      session - The bean session to use to get the class meta. This is always going to be the same bean context that created this swap.
      f - The transformed object.
      hint - If possible, the parser will try to tell you the object type being created. For example, on a serialized date, this may tell you that the object being created must be of type GregorianCalendar.
      This may be null if the parser cannot make this determination.
      template - The template string associated with this swap.
      Returns:
      The transformed object.
      Throws:
      Exception - If a problem occurred trying to convert the output.