001 package org.objectweb.jac.util; 002 003 import java.lang.Cloneable; 004 import java.util.AbstractSet; 005 import java.util.Collection; 006 import java.util.Iterator; 007 import java.util.Set; 008 import java.util.WeakHashMap; 009 010 011 public class WeakHashSet extends AbstractSet 012 implements Set 013 { 014 private transient WeakHashMap map; 015 016 public WeakHashSet() { 017 map = new WeakHashMap(); 018 } 019 public WeakHashSet(Collection c) { 020 map = new WeakHashMap(Math.max((int) (c.size()/.75f) + 1, 16)); 021 addAll(c); 022 } 023 public WeakHashSet(int initialCapacity, float loadFactor) { 024 map = new WeakHashMap(initialCapacity, loadFactor); 025 } 026 public WeakHashSet(int initialCapacity) { 027 map = new WeakHashMap(initialCapacity); 028 } 029 030 public Iterator iterator() { 031 return map.keySet().iterator(); 032 } 033 034 public int size() { 035 return map.size(); 036 } 037 public boolean isEmpty() { 038 return map.isEmpty(); 039 } 040 041 public boolean contains(Object o) { 042 return map.containsKey(o); 043 } 044 045 public boolean add(Object o) { 046 return map.put(o, Boolean.TRUE)==null; 047 } 048 public boolean remove(Object o) { 049 return map.remove(o)==Boolean.TRUE; 050 } 051 public void clear() { 052 map.clear(); 053 } 054 }