A java.util.Map implementation with soft values : Soft Map « Collections Data Structure « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Collections Data Structure » Soft MapScreenshots 
A java.util.Map implementation with soft values
 

/*
 * Copyright 2002-2006 (C) TJDO.
 * All rights reserved.
 *
 * This software is distributed under the terms of the TJDO License version 1.0.
 * See the terms of the TJDO License in the documentation provided with this software.
 *
 * $Id: ReferenceValueMap.java,v 1.8 2006/09/08 16:11:28 jackknifebarber Exp $
 */


import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*
 * Copyright 2002-2006 (C) TJDO.
 * All rights reserved.
 *
 * This software is distributed under the terms of the TJDO License version 1.0.
 * See the terms of the TJDO License in the documentation provided with this software.
 *
 * $Id: SoftValueMap.java,v 1.5 2006/09/08 16:11:28 jackknifebarber Exp $
 */


import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;


/**
 * A <code>java.util.Map</code> implementation with soft values.
 * <p>
 * The values are stored as soft references.
 * If map entry value object is not actively being used, i.e. no other object
 * has a strong reference to it, it may become garbage collected at the
 * discretion of the garbage collector (typically if the VM is low on memory).
 * If this happens, the entry in the <code>SoftValueMap</code> corresponding to
 * the value object will also be removed.
 *
 @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
 @version $Revision: 1.5 $
 *
 @see SoftReference
 */

public class SoftValueMap extends ReferenceValueMap
{
    public SoftValueMap()
    {
        super(new HashMap());
    }

    public SoftValueMap(int initialCapacity)
    {
        super(new HashMap(initialCapacity));
    }

    public SoftValueMap(int initialCapacity, float loadFactor)
    {
        super(new HashMap(initialCapacity, loadFactor));
    }

    public SoftValueMap(Map m)
    {
        super(new HashMap(m));
    }

    protected Reference newReference(Object value)
    {
        return new SoftReference(value, refQueue);
    }
}


/**
 * A <code>java.util.Map</code> implementation using reference values.
 * <p>
 * The values are stored in the map as references.
 * If the garbage collector clears the reference, the corresponding key is
 * automatically removed from the map.
 *
 @author  <a href="mailto:jackknifebarber@users.sourceforge.net">Mike Martin</a>
 @version $Revision: 1.8 $
 */

abstract class ReferenceValueMap extends AbstractMap
{
    protected final ReferenceQueue refQueue = new ReferenceQueue();
    /** Backing map. */
    private final Map backing;

    ReferenceValueMap(Map backing)
    {
        this.backing = backing;
    }

    /**
     * Returns a new <code>Reference</code> object to be inserted into the map.
     * Subclasses must implement this method to construct <code>Reference</code>
     * objects of the desired type (e.g. <code>SoftReference</code>, etc.).
     *
     @param value
     *      The associated value to be referenced.
     *
     @return
     *      A new <code>Reference</code> object to be inserted into the map.
     */
    protected abstract Reference newReference(Object value);

    private void reap()
    {
        Reference ref;

        while ((ref = refQueue.poll()) != null)
            backing.values().remove(ref);
    }

    public Object put(Object key, Object value)
    {
        reap();
        return backing.put(key, newReference(value));
    }

    public Object get(Object key)
    {
        reap();

        Object v = backing.get(key);

        return (instanceof Reference((Reference)v).get() : v;
    }

    public int size()
    {
        reap();
        return backing.size();
    }

    public boolean isEmpty()
    {
        reap();
        return backing.isEmpty();
    }

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

    public boolean containsValue(Object value)
    {
        reap();
        return super.containsValue(value);
    }

    public Set keySet()
    {
        reap();
        return backing.keySet();
    }

    public Collection values()
    {
        reap();
        return super.values();
    }

    public Set entrySet()
    {
        reap();
        return new EntrySet();
    }

    public Object remove(Object key)
    {
        reap();
        return backing.remove(key);
    }

    public int hashCode()
    {
        reap();
        return super.hashCode();
    }

    public boolean equals(Object o)
    {
        reap();
        return super.equals(o);
    }

    public String toString()
    {
        reap();
        return super.toString();
    }

    static boolean eq(Object o1, Object o2)
    {
        return o1 == null ? o2 == null : o1.equals(o2);
    }

    private class EntrySet extends AbstractSet
    {
        /** Backing set. */
        private final Set set = backing.entrySet();

        public Iterator iterator()
        {
            return new Iterator()
            {
                private Iterator i = set.iterator();

                public boolean hasNext()
                {
                    return i.hasNext();
                }

                public void remove()
                {
                    i.remove();
                }

                public Object next()
                {
                    final Map.Entry ent = (Map.Entry)i.next();

                    return new Map.Entry()
                    {
                        public Object getKey()
                        {
                            return ent.getKey();
                        }

                        public Object getValue()
                        {
                            Object v = ent.getValue();

                            return (instanceof Reference((Reference)v).get() : v;
                        }

                        public Object setValue(Object v)
                        {
                            Object oldVal = getValue();
                            ent.setValue(newReference(v));
                            return oldVal;
                        }

                        public boolean equals(Object o)
                        {
                            if (o == this)
                                return true;
                            if (!(instanceof Map.Entry))
                                return false;

                            Map.Entry e = (Map.Entry)o;
                            return eq(ent.getKey(), e.getKey())
                                && eq(ent.getValue(), e.getValue());
                        }

                        public int hashCode()
                        {
                            Object key = ent.getKey();
                            Object val = ent.getValue();

                            return (key == null : key.hashCode())
                                 (val == null : val.hashCode());
                        }

                        public String toString()
                        {
                            return ent.getKey() "=" + ent.getValue();
                        }
                    };
                }
            };
        }

        public int size()
        {
            reap();
            return set.size();
        }

        public boolean isEmpty()
        {
            reap();
            return set.isEmpty();
        }

        public boolean contains(Object o)
        {
            reap();
            return super.contains(o);
        }

        public Object[] toArray()
        {
            reap();
            return super.toArray();
        }

        public Object[] toArray(Object[] a)
        {
            reap();
            return super.toArray(a);
        }

        public boolean remove(Object o)
        {
            reap();
            return super.remove(o);
        }

        public boolean containsAll(Collection c)
        {
            reap();
            return super.containsAll(c);
        }

        public boolean removeAll(Collection c)
        {
            reap();
            return super.removeAll(c);
        }

        public boolean retainAll(Collection c)
        {
            reap();
            return super.retainAll(c);
        }

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

        public String toString()
        {
            reap();
            return super.toString();
        }
    }
}

   
  
Related examples in the same category
1. Soft HashMap
2. Soft Valued HashMap
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.