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

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

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

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 KeyBidiTransformingMap
   extends KeyTransformingMap
   implements BidiTransforming
{
   /**
    * @deprecated Use TranformerUtils.decorateKeyBased() instead
    */
   @Deprecated
   public KeyBidiTransformingMap(Map map, BidiFunction transformer)
   {
      super(map, transformer);
   }
   
   public BidiFunction getTransformerBidiFunction()
   {
      return (BidiFunction)_transformer;
   }

   @Override
   public Set> entrySet()
   {
      return TransformerUtils.decorate(
         _internalMap.entrySet(),
         new KeyBidiTransformingEntryBidiTransformer(
            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 KeyBidiTransformingEntryBidiTransformer
      extends KeyTransformingEntryTransformer
      implements BidiFunction, Entry>
   {
      public KeyBidiTransformingEntryBidiTransformer(BidiFunction transformer)
      {
         super(transformer);
      }
   
      public BidiFunction, Entry> invert()
      {
         return Functions.invert(this);
      }

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

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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy