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

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

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

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

import net.sf.javagimmicks.transform.Transforming;
import net.sf.javagimmicks.util.Function;

class ValueTransformingMap
   extends AbstractMap
   implements Transforming
{
   protected final Map _internalMap;
   private final Function _transformer;
   
   /**
    * @deprecated Use TranformerUtils.decorateValueBased() instead
    */
   @Deprecated
   public ValueTransformingMap(Map map, Function valueTransformer)
   {
      _internalMap = map;
      _transformer = valueTransformer;
   }
   
   public Function getTransformerFunction()
   {
      return _transformer;
   }

   public void clear()
   {
      _internalMap.clear();
   }

   public boolean containsKey(Object key)
   {
      return _internalMap.containsKey(key);
   }

   public Set> entrySet()
   {
      return TransformerUtils.decorate(
         _internalMap.entrySet(),
         new ValueTransformingEntryTransformer(getTransformerFunction()));
   }
   
   public VT get(Object key)
   {
      VF result = _internalMap.get(key);
      
      if(result == null && !containsKey(key))
      {
         return null;
      }
      
      return transform(result);
   }

   public boolean isEmpty()
   {
      return _internalMap.isEmpty();
   }

   public Set keySet()
   {
      return _internalMap.keySet();
   }

   public VT remove(Object key)
   {
      if(!containsKey(key))
      {
         return null;
      }
      
      VF result = _internalMap.remove(key);
      return transform(result);
   }

   public int size()
   {
      return _internalMap.size();
   }

   public Collection values()
   {
      return TransformerUtils.decorate(
         _internalMap.values(),
         getTransformerFunction());
   }
   
   protected VT transform(VF element)
   {
      return getTransformerFunction().apply(element);
   }
   
   protected static class ValueTransformingEntry
      implements Entry, Transforming
   {
      protected final Function _transformer;
      protected final Entry _internalEntry;
      
      public ValueTransformingEntry(Entry entry, Function transformer)
      {
         _transformer = transformer;
         _internalEntry = entry;
      }
      
      public Function getTransformerFunction()
      {
         return _transformer;
      }

      public K getKey()
      {
         return _internalEntry.getKey();
      }

      public VT getValue()
      {
         return getTransformerFunction().apply(_internalEntry.getValue());
      }

      public VT setValue(VT value)
      {
         throw new UnsupportedOperationException();
      }
   }
   
   protected static class ValueTransformingEntryTransformer
      implements Function, Entry>
   {
      protected final Function _transformer;
      
      public ValueTransformingEntryTransformer(Function transformer)
      {
         _transformer = transformer;
      }

      public Entry apply(Entry source)
      {
         return new ValueTransformingEntry(source, _transformer);
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy