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

net.sf.javagimmicks.collections.transformer.ValueBidiTransformingMap Maven / Gradle / Ivy

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

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import net.sf.javagimmicks.transform.BidiFunction;
import net.sf.javagimmicks.transform.BidiTransforming;
import net.sf.javagimmicks.transform.Functions;

class ValueBidiTransformingMap
   extends ValueTransformingMap
   implements BidiTransforming
{
   /**
    * @deprecated Use TranformerUtils.decorateValueBased() instead
    */
   @Deprecated
   public ValueBidiTransformingMap(Map map, BidiFunction valueTransformer)
   {
      super(map, valueTransformer);
   }
   
   public BidiFunction getTransformerBidiFunction()
   {
      return (BidiFunction)getTransformerFunction();
   }
   
   @SuppressWarnings("unchecked")
   @Override
   public boolean containsValue(Object value)
   {
      try
      {
         return _internalMap.containsValue(getTransformerBidiFunction().applyReverse((VT)value));
      }
      catch (ClassCastException e)
      {
         return super.containsValue(value);
      }
   }

   @Override
   public Set> entrySet()
   {
      return TransformerUtils.decorate(
         _internalMap.entrySet(),
         new ValueBidiTransformingEntryBidiTransformer(getTransformerBidiFunction()));
   }

   @Override
   public VT put(K key, VT value)
   {
      boolean keyContained = containsKey(key);

      VF result = _internalMap.put(key, getTransformerBidiFunction().applyReverse(value));
      return !keyContained ? null : getTransformerFunction().apply(result);
   }
   
   @Override
   public Collection values()
   {
      return TransformerUtils.decorate(_internalMap.values(), getTransformerBidiFunction());
   }

   protected static class ValueBidiTransformingEntry
      extends ValueTransformingEntry
      implements BidiTransforming
   {
      public ValueBidiTransformingEntry(Entry entry, BidiFunction transformer)
      {
         super(entry, transformer);
      }
      
      public BidiFunction getTransformerBidiFunction()
      {
         return (BidiFunction)_transformer;
      }

      @Override
      public VT setValue(VT value)
      {
         VF result = _internalEntry.setValue(getTransformerBidiFunction().applyReverse(value));
         return getTransformerFunction().apply(result);
      }
   }

   protected static class ValueBidiTransformingEntryBidiTransformer
      extends ValueTransformingEntryTransformer
      implements BidiFunction, Map.Entry>
   {
      public ValueBidiTransformingEntryBidiTransformer(BidiFunction transformer)
      {
         super(transformer);
      }

      public Entry applyReverse(Entry source)
      {
         return new ValueBidiTransformingEntry(source, getTransformer().invert());
      }

      public BidiFunction, Entry> invert()
      {
         return Functions.invert(this);
      }

      protected BidiFunction getTransformer()
      {
         return (BidiFunction)_transformer;
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy