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

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

package net.sf.javagimmicks.collections8.transformer;

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

import net.sf.javagimmicks.transform8.BidiFunction;
import net.sf.javagimmicks.transform8.BidiTransforming;

class ValueBidiTransformingMap
   extends ValueTransformingMap
   implements BidiTransforming
{
   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 ValueBidiTransformingEntryBidiFunction(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 ValueBidiTransformingEntryBidiFunction
      extends ValueTransformingEntryTransformer
      implements BidiFunction, Map.Entry>
   {
      public ValueBidiTransformingEntryBidiFunction(BidiFunction transformer)
      {
         super(transformer);
      }

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy