You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Morgan Delagrange <md...@yahoo.com> on 2002/02/21 22:31:30 UTC

[COLLECTIONS] DirtyMapFlas [was: Re: Collections 1.1?]

Hi all,

Would anyone like to sponsor this as a 2.0 collection?  I don't see myself
using this kind of collection, but maybe it's more useful than I'm giving it
credit for.

- Morgan

----- Original Message -----
From: "James House" <ja...@interobjective.com>
To: "Jakarta Commons Developers List" <co...@jakarta.apache.org>
Sent: Monday, February 18, 2002 3:06 PM
Subject: Re: Collections 1.1?


>
> >   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:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [COLLECTIONS] DirtyMapFlas [was: Re: Collections 1.1?]

Posted by "Michael A. Smith" <mi...@iammichael.org>.
On Thu, 21 Feb 2002, Morgan Delagrange wrote:
> Would anyone like to sponsor this as a 2.0 collection?  I don't see myself
> using this kind of collection, but maybe it's more useful than I'm giving it
> credit for.

I'm not sure what you mean by "sponsor" here, but if it just means that a 
committer needs to take responsibility for it, I'll do it.  Although I'm 
not going to be able to get to it for a little while.  Maybe someone else 
can before I have the chance...

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

Note:  There's actually some problems with the implementation that was 
provided.  Namely, modification to the map using the entrySet(), keySet(), 
or values() does not set the dirty flag.  Also, the equals and hashCode 
methods don't follow the Map contract.  These items will have to be fixed 
before I think it can be included.

regards,
michael





--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>