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

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

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

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

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

class KeyBidiTransformingMap
   extends KeyTransformingMap
   implements BidiTransforming
{
   KeyBidiTransformingMap(Map map, BidiFunction transformer)
   {
      super(map, transformer);
   }
   
   public BidiFunction getTransformerBidiFunction()
   {
      return (BidiFunction)_transformer;
   }

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

   @Override
   public Set keySet()
   {
      return TransformerUtils.decorate(
         _internalMap.keySet(),
         getTransformerBidiFunction());
   }
   
   @Override
   public V put(KT key, V value)
   {
      return _internalMap.put(transformBack(key), value);
   }
   
   @SuppressWarnings("unchecked")
   @Override
   public V get(Object key)
   {
      try
      {
         return _internalMap.get(transformBack((KT)key));
      }
      catch(ClassCastException ex)
      {
         return super.get(key);
      }
   }

   @SuppressWarnings("unchecked")
   @Override
   public boolean containsKey(Object key)
   {
      try
      {
         return _internalMap.containsKey(transformBack((KT)key));
      }
      catch(ClassCastException ex)
      {
         return super.containsKey(key);
      }
   }

   @SuppressWarnings("unchecked")
   @Override
   public V remove(Object key)
   {
      try
      {
         return _internalMap.remove(transformBack((KT)key));
      }
      catch(ClassCastException ex)
      {
         return super.remove(key);
      }
   }
   
   protected KF transformBack(KT element)
   {
      return getTransformerBidiFunction().applyReverse(element);
   }
   
   protected static class KeyBidiTransformingEntry
      extends KeyTransformingEntry
      implements BidiTransforming
   {

      public KeyBidiTransformingEntry(Entry entry, BidiFunction transformer)
      {
         super(entry, transformer);
      }

      public BidiFunction getTransformerBidiFunction()
      {
         return (BidiFunction)getTransformerFunction();
      }
   }

   protected static class KeyBidiTransformingEntryBidiFunction
      extends KeyTransformingEntryTransformer
      implements BidiFunction, Entry>
   {
      public KeyBidiTransformingEntryBidiFunction(BidiFunction transformer)
      {
         super(transformer);
      }
   
      public Entry applyReverse(Entry source)
      {
         return new KeyBidiTransformingEntry(source, getTransformer().invert());
      }

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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy