commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James House <ja...@interobjective.com>
Subject Re: Collections 1.1?
Date Mon, 18 Feb 2002 21:06:48 GMT

>   Does anyone have other
>work they would like to include in this release?  (I know Michael has some
>pending patches, if anyone is feeling ambitious.)
>
>- Morgan


I've got a "DirtyFlagMap" that may be of interest.  It's a Map that wraps 
another Map, and adds a "dirty" flag - so you can tell if the Map has been 
modified since the flag was last cleared.  This is very useful for 
environments where you pass a Map to a component (such as a plug-in) and 
you need to be able to tell if it was modified.

Here's the very simple implementation, in case anyone's interested, and/or 
you think it good for inclusion in Collections 1.1

James

============================

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

/**
  * <p>An implementation of <code>Map</code> that wraps another 
<code>Map</code>
  * and flags itself 'dirty' when it is modified.</p>
  *
  * @author James House
  */
public class DirtyFlagMap implements Map {


   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    *
    * Data Members.
    *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


   private boolean dirty = false;
   private Map map;

   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    *
    * Constructors.
    *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

   /**
    * <p>Create a DirtyFlagMap that 'wraps' the given <code>Map</code>.</p>
    */
   public DirtyFlagMap(Map mapToWrap)
   {
     if(mapToWrap == null)
       throw new IllegalArgumentException("mapToWrap cannot be null!");

     map = mapToWrap;
   }

   /**
    * <p>Create a DirtyFlagMap that 'wraps' a <code>HashMap</code>.</p>
    *
    * @see java.util.HashMap
    */
   public DirtyFlagMap()
   {
     map = new HashMap();
   }

   /**
    * <p>Create a DirtyFlagMap that 'wraps' a <code>HashMap</code> that
has
    * the given initial capacity.</p>
    *
    * @see java.util.HashMap
    */
   public DirtyFlagMap(int initialCapacity)
   {
     map = new HashMap(initialCapacity);
   }

   /**
    * <p>Create a DirtyFlagMap that 'wraps' a <code>HashMap</code> that
has
    * the given initial capacity and load factor.</p>
    *
    * @see java.util.HashMap
    */
   public DirtyFlagMap(int initialCapacity, float loadFactor)
   {
     map = new HashMap(initialCapacity, loadFactor);
   }

   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    *
    * Interface.
    *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

   /**
    * <p>Clear the 'dirty' flag (set dirty flag to <code>false</code>).</p>
    */
   public void clearDirtyFlag()
   {
     dirty = false;
   }

   /**
    * <p>Determine whether the <code>Map</code> is flagged dirty.</p>
    */
   public boolean isDirty()
   {
     return dirty;
   }

   public Map getWrappedMap()
   {
     return map;
   }



   public void clear()
   {
     dirty = true;

     map.clear();
   }

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

   public boolean containsValue(Object val)
   {
     return map.containsValue(val);
   }

   public Set entrySet()
   {
     return map.entrySet();
   }

   public boolean equals(Object obj)
   {
     if(obj == null || !(obj instanceof DirtyFlagMap))
       return false;

     return map.equals(((DirtyFlagMap)obj).getWrappedMap());
   }

   public Object get(Object key)
   {
     return map.get(key);
   }

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

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

   public Object put(Object key, Object val)
   {
     dirty = true;

     return map.put(key, val);
   }

   public void putAll(Map t)
   {
     if(!t.isEmpty())
       dirty = true;

     map.putAll(t);
   }

   public Object remove(Object key)
   {
     Object obj = map.remove(key);

     if(obj != null)
       dirty = true;

     return obj;
   }

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

   public Collection values()
   {
     return map.values();
   }
}

============================




--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message