All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.javagimmicks.transform.Transformers Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
package net.sf.javagimmicks.transform;


/**
 * A basic helper for dealing with {@link Transformer}s, {@link BidiTransformer}
 * s and {@link Transforming} and {@link BidiTransforming} instances.
 */
public class Transformers
{
   private Transformers()
   {}

   /**
    * Creates a new pseudo-{@link Transformer} that always returns the original
    * value.
    * 
    * @return a new pseudo-{@link Transformer} that always returns the original
    *         value
    */
   public static  Transformer identityTransformer()
   {
      return identityBidiTransformer();
   }

   /**
    * Creates a new pseudo-{@link BidiTransformer} that always returns the
    * original value.
    * 
    * @return a new pseudo-{@link BidiTransformer} that always returns the
    *         original value
    */
   public static  BidiTransformer identityBidiTransformer()
   {
      return new BidiTransformer()
      {
         @Override
         public E transform(final E source)
         {
            return source;
         }

         @Override
         public BidiTransformer invert()
         {
            return this;
         }

         @Override
         public E transformBack(final E source)
         {
            return source;
         }
      };
   }

   /**
    * Creates a new inverted version of a given {@link BidiTransformer}
    * (exchanges {@link BidiTransformer#transform(Object)} and
    * {@link BidiTransformer#transformBack(Object)}).
    * 
    * @param transformer
    *           the {@link BidiTransformer} to invert
    * @return a inverted version of the given {@link BidiTransformer}
    */
   public static  BidiTransformer invert(final BidiTransformer transformer)
   {
      return new BidiTransformer()
      {
         @Override
         public T transformBack(final F source)
         {
            return transformer.transform(source);
         }

         @Override
         public F transform(final T source)
         {
            return transformer.transformBack(source);
         }

         @Override
         public BidiTransformer invert()
         {
            return transformer;
         }
      };
   }

   /**
    * Creates a new {@link BidiTransformer} out of two given {@link Transformer}
    * s - one for each direction.
    * 
    * @param forwardTransformer
    *           the forward {@link Transformer}
    * @param backTransformer
    *           the backward {@link Transformer}
    * @return a resulting {@link BidiTransformer} combined from the two given
    *         {@link Transformer}s
    */
   public static  BidiTransformer bidiTransformerFromTransformers(
         final Transformer forwardTransformer,
         final Transformer backTransformer)
   {
      return new DualTransformerBidiTransformer(forwardTransformer, backTransformer);
   }

   /**
    * Checks if a given object is transforming (if it is an instance of
    * {@link Transforming}).
    * 
    * @param o
    *           the object to check
    * @return if the object is {@link Transforming}
    */
   public static boolean isTransforming(final Object o)
   {
      return o instanceof Transforming;
   }

   /**
    * Checks if a given object is bidi-transforming (if it is an instance of
    * {@link BidiTransforming}).
    * 
    * @param o
    *           the object to check
    * @return if the object is {@link BidiTransforming}
    */
   public static boolean isBidiTransforming(final Object o)
   {
      return o instanceof BidiTransforming;
   }

   /**
    * Returns the {@link Transformer} of a given object if it is
    * {@link Transforming}.
    * 
    * @param transforming
    *           the object to drag the {@link Transformer} out from
    * @return the {@link Transformer} contained in the given object
    * @throws IllegalArgumentException
    *            if the given object is not a {@link Transforming} instance
    * @see #isTransforming(Object)
    */
   public static Transformer getTransformer(final Object transforming)
   {
      if (!isTransforming(transforming))
      {
         throw new IllegalArgumentException("Object is not transforming!");
      }

      return ((Transforming) transforming).getTransformer();
   }

   /**
    * Returns the {@link BidiTransformer} of a given object if it is
    * {@link BidiTransforming}.
    * 
    * @param transforming
    *           the object to drag the {@link BidiTransformer} out from
    * @return the {@link BidiTransformer} contained in the given object
    * @throws IllegalArgumentException
    *            if the given object is not a {@link BidiTransforming} instance
    * @see #isBidiTransforming(Object)
    */
   public static BidiTransformer getBidiTransformer(final Object transforming)
   {
      if (!isBidiTransforming(transforming))
      {
         throw new IllegalArgumentException("Object is not bidi-transforming!");
      }

      return ((BidiTransforming) transforming).getBidiTransformer();
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy