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

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

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

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

import net.sf.javagimmicks.transform8.Transforming;

class KeyTransformingMap
   extends AbstractMap
   implements Transforming
{
   protected final Map _internalMap;
   protected final Function _transformer;
   
   KeyTransformingMap(Map map, Function transformer)
   {
      _internalMap = map;
      _transformer = transformer;
   }
   
   public Function getTransformerFunction()
   {
      return _transformer;
   }

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

   @Override
   public boolean containsValue(Object value)
   {
      return _internalMap.containsValue(value);
   }

   @Override
   public Set> entrySet()
   {
      return TransformerUtils.decorate(
         _internalMap.entrySet(),
         new KeyTransformingEntryTransformer(getTransformerFunction()));
   }
   
   @Override
   public boolean isEmpty()
   {
      return _internalMap.isEmpty();
   }

   @Override
   public Set keySet()
   {
      return TransformerUtils.decorate(
         _internalMap.keySet(),
         getTransformerFunction());
   }

   @Override
   public int size()
   {
      return _internalMap.size();
   }
   
   @Override
   public Collection values()
   {
      return _internalMap.values();
   }
   
   protected KT transform(KF element)
   {
      return getTransformerFunction().apply(element);
   }
   
   protected static class KeyTransformingEntry
      implements Entry, Transforming
   {
      protected final Entry _internalEntry;
      protected final Function _transformer;
      
      public KeyTransformingEntry(Entry entry, Function transformer)
      {
         _internalEntry = entry;
         _transformer = transformer;
      }

      public Function getTransformerFunction()
      {
         return _transformer;
      }

      public KT getKey()
      {
         return _transformer.apply(_internalEntry.getKey());
      }

      public V getValue()
      {
         return _internalEntry.getValue();
      }

      public V setValue(V value)
      {
         return _internalEntry.setValue(value);
      }
   }
   
   protected static class KeyTransformingEntryTransformer
      implements Function, Entry>
   {
      protected final Function _transformer;
      
      public KeyTransformingEntryTransformer(Function transformer)
      {
         _transformer = transformer;
      }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy