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

net.sf.javagimmicks.collections.AbstractMap2 Maven / Gradle / Ivy

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

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * An extension of {@link AbstractMap} that allows an alternate way to specify
 * the entry set - i.e. by specifying the key set and the logic to derive the
 * value from the key.
 */
public abstract class AbstractMap2 extends AbstractMap
{
   @Override
   abstract public Set keySet();

   /**
    * Derives the value for a given key.
    * 
    * @param key
    *           the given key
    * @return the derived value for the given key
    */
   abstract protected V getValue(K key);

   @Override
   public Set> entrySet()
   {
      final Set keySet = keySet();
      return new EntrySetProxy(keySet);
   }

   protected class EntrySetProxy extends AbstractSet>
   {
      protected final Set _keySet;

      protected EntrySetProxy(final Set keySet)
      {
         _keySet = keySet;
      }

      @Override
      public Iterator> iterator()
      {
         final Iterator keyIterator = _keySet.iterator();
         return new EntryIteratorProxy(keyIterator);
      }

      @Override
      public int size()
      {
         return _keySet.size();
      }
   }

   protected class EntryIteratorProxy implements Iterator>
   {
      protected final Iterator _keyIterator;

      protected EntryIteratorProxy(final Iterator keyIterator)
      {
         _keyIterator = keyIterator;
      }

      @Override
      public boolean hasNext()
      {
         return _keyIterator.hasNext();
      }

      @Override
      public Entry next()
      {
         final K key = _keyIterator.next();
         final V value = getValue(key);

         return new EntryProxy(key, value);
      }

      @Override
      public void remove()
      {
         _keyIterator.remove();
      }
   }

   protected final class EntryProxy implements Entry
   {
      protected final K _key;
      protected final V _value;

      private EntryProxy(final K key, final V value)
      {
         _key = key;
         _value = value;
      }

      @Override
      public K getKey()
      {
         return _key;
      }

      @Override
      public V getValue()
      {
         return _value;
      }

      @Override
      public V setValue(final V value)
      {
         // !! DANGEROUS: May cause a ConcurrentModificationException when
         // continuing to iterate over the map's entries
         put(_key, value);

         return _value;
      }
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy