WeakHashSet.java

  1. /***** BEGIN LICENSE BLOCK *****
  2.  * Version: EPL 1.0/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Eclipse Public
  5.  * License Version 1.0 (the "License"); you may not use this file
  6.  * except in compliance with the License. You may obtain a copy of
  7.  * the License at http://www.eclipse.org/legal/epl-v10.html
  8.  *
  9.  * Software distributed under the License is distributed on an "AS
  10.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11.  * implied. See the License for the specific language governing
  12.  * rights and limitations under the License.
  13.  *
  14.  * Alternatively, the contents of this file may be used under the terms of
  15.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  16.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  17.  * in which case the provisions of the GPL or the LGPL are applicable instead
  18.  * of those above. If you wish to allow use of your version of this file only
  19.  * under the terms of either the GPL or the LGPL, and not to allow others to
  20.  * use your version of this file under the terms of the EPL, indicate your
  21.  * decision by deleting the provisions above and replace them with the notice
  22.  * and other provisions required by the GPL or the LGPL. If you do not delete
  23.  * the provisions above, a recipient may use your version of this file under
  24.  * the terms of any one of the EPL, the GPL or the LGPL.
  25.  ***** END LICENSE BLOCK *****/
  26. package org.jruby.util.collections;

  27. import java.util.*;

  28. /**
  29.  * A simple set that uses weak references to ensure that its elements can be garbage collected.
  30.  * See WeakHashMap.
  31.  *
  32.  * @author <a href="http://www.cs.auckland.ac.nz/~robert/">Robert Egglestone</a>
  33.  */
  34. public class WeakHashSet<T> implements Set<T> {
  35.     private WeakHashMap<T,Object> map;

  36.     public WeakHashSet() {
  37.         map = new WeakHashMap<T,Object>();
  38.     }

  39.     public WeakHashSet(int size) {
  40.         map = new WeakHashMap<T,Object>(size);
  41.     }

  42.     public boolean add(T o) {
  43.         Object previousValue = map.put(o, null);
  44.         return previousValue == null;
  45.     }

  46.     public Iterator<T> iterator() {
  47.         return map.keySet().iterator();
  48.     }

  49.     public int size() {
  50.         return map.size();
  51.     }

  52.     public boolean isEmpty() {
  53.         return map.isEmpty();
  54.     }

  55.     public boolean contains(Object o) {
  56.         return map.containsKey(o);
  57.     }

  58.     public boolean remove(Object o) {
  59.         boolean contains = contains(o);
  60.         map.remove(o);
  61.         return contains;
  62.     }

  63.     public boolean removeAll(Collection collection) {
  64.         return map.keySet().removeAll(collection);
  65.     }

  66.     public boolean retainAll(Collection collection) {
  67.         return map.keySet().retainAll(collection);
  68.     }

  69.     public void clear() {
  70.         map.clear();
  71.     }

  72.     public Object[] toArray() {
  73.         return map.keySet().toArray();
  74.     }

  75.     public boolean containsAll(Collection arg0) {
  76.         return map.keySet().containsAll(arg0);
  77.     }

  78.     public boolean addAll(Collection<? extends T> arg0) {
  79.         boolean added = false;
  80.         for (T i: arg0) {
  81.             add(i);
  82.             added = true;
  83.         }
  84.         return added;
  85.     }

  86.     public <T> T[] toArray(T[] arg0) {
  87.         return map.keySet().toArray(arg0);
  88.     }

  89. }