A bag of weakly referenced objects : Weak List « Collections Data Structure « Java

Home
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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Weak List 




A bag of weakly referenced objects
   

/*
 * Copyright (c) 1998 - 2005 Versant Corporation
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Versant Corporation - initial API and implementation
 */

import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.lang.ref.Reference;

/**
 * This maintains a bag of weakly referenced objects. The clean method
 * must be called from time to time to get rid of the objects that the
 * garbage collector wants to nuke. This class is not synchronized.
 */
public final class WeakBag {

    private HashSet set = new HashSet();
    private ReferenceQueue refQueue = new ReferenceQueue();

    public WeakBag() {
    }

    /**
     * Add o to the bag and return the WeakReference that can be used to
     * remove it.
     */
    public WeakReference add(Object o) {
        WeakReference ans = new WeakReference(o, refQueue);
        set.add(ans);
        return ans;
    }

    /**
     * Remove o from the bag.
     */
    public void remove(Reference o) {
        set.remove(o);
    }

    /**
     * Get the approximate number of objects in the bag.
     */
    public int size() {
        return set.size();
    }

    /**
     * Get all the objects still in the bag.
     */
    public List values() {
        ArrayList a = new ArrayList(set.size());
        for (Iterator i = set.iterator(); i.hasNext()) {
            WeakReference r = (WeakReference)i.next();
            Object o = r.get();
            if (o != nulla.add(o);
        }
        return a;
    }

    /**
     * Get rid of objects in the bag that the garbage collector wants to
     * nuke. This does not block.
     */
    public void clean() {
        for (;;) {
            Object r = refQueue.poll();
            if (r == nullreturn;
            set.remove(r);
        }
    }

}

   
    
    
  














Related examples in the same category
1.Weak List from objectweb jac
2.WeakReference list uses java.lang.ref.WeakReferences to store its contents.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.