001 002 package railo.commons.collections; 003 004 import java.util.AbstractSet; 005 import java.util.ConcurrentModificationException; 006 import java.util.Iterator; 007 import java.util.Set; 008 009 010 public class HashSetNotSync 011 extends AbstractSet 012 implements Set, Cloneable, java.io.Serializable 013 { 014 static final long serialVersionUID = -5024744406713321676L; 015 016 private transient HashTableNotSync map; 017 018 // Dummy value to associate with an Object in the backing Map 019 private static final Object PRESENT = new Object(); 020 021 /** 022 * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has 023 * default initial capacity (16) and load factor (0.75). 024 */ 025 public HashSetNotSync() { 026 map = new HashTableNotSync(); 027 } 028 029 030 private HashSetNotSync(HashTableNotSync clone) { 031 // TODO Auto-generated constructor stub 032 } 033 034 035 /** 036 * Returns an iterator over the elements in this set. The elements 037 * are returned in no particular order. 038 * 039 * @return an Iterator over the elements in this set 040 * @see ConcurrentModificationException 041 */ 042 public Iterator iterator() { 043 return map.keySet().iterator(); 044 } 045 046 /** 047 * Returns the number of elements in this set (its cardinality). 048 * 049 * @return the number of elements in this set (its cardinality) 050 */ 051 public int size() { 052 return map.size(); 053 } 054 055 /** 056 * Returns <tt>true</tt> if this set contains no elements. 057 * 058 * @return <tt>true</tt> if this set contains no elements 059 */ 060 public boolean isEmpty() { 061 return map.isEmpty(); 062 } 063 064 /** 065 * Returns <tt>true</tt> if this set contains the specified element. 066 * More formally, returns <tt>true</tt> if and only if this set 067 * contains an element <tt>e</tt> such that 068 * <tt>(o==null ? e==null : o.equals(e))</tt>. 069 * 070 * @param o element whose presence in this set is to be tested 071 * @return <tt>true</tt> if this set contains the specified element 072 */ 073 public boolean contains(Object o) { 074 return map.containsKey(o); 075 } 076 077 /** 078 * Adds the specified element to this set if it is not already present. 079 * More formally, adds the specified element <tt>e</tt> to this set if 080 * this set contains no element <tt>e2</tt> such that 081 * <tt>(e==null ? e2==null : e.equals(e2))</tt>. 082 * If this set already contains the element, the call leaves the set 083 * unchanged and returns <tt>false</tt>. 084 * 085 * @param e element to be added to this set 086 * @return <tt>true</tt> if this set did not already contain the specified 087 * element 088 */ 089 public boolean add(Object e) { 090 return map.put(e, PRESENT)==null; 091 } 092 093 /** 094 * Removes the specified element from this set if it is present. 095 * More formally, removes an element <tt>e</tt> such that 096 * <tt>(o==null ? e==null : o.equals(e))</tt>, 097 * if this set contains such an element. Returns <tt>true</tt> if 098 * this set contained the element (or equivalently, if this set 099 * changed as a result of the call). (This set will not contain the 100 * element once the call returns.) 101 * 102 * @param o object to be removed from this set, if present 103 * @return <tt>true</tt> if the set contained the specified element 104 */ 105 public boolean remove(Object o) { 106 return map.remove(o)==PRESENT; 107 } 108 109 /** 110 * Removes all of the elements from this set. 111 * The set will be empty after this call returns. 112 */ 113 public void clear() { 114 map.clear(); 115 } 116 117 /** 118 * Returns a shallow copy of this <tt>HashSet</tt> instance: the elements 119 * themselves are not cloned. 120 * 121 * @return a shallow copy of this set 122 */ 123 public Object clone() { 124 HashSetNotSync newSet = new HashSetNotSync((HashTableNotSync) map.clone()); 125 return newSet; 126 } 127 128 /** 129 * Save the state of this <tt>HashSet</tt> instance to a stream (that is, 130 * serialize it). 131 * 132 * @serialData The capacity of the backing <tt>HashMap</tt> instance 133 * (int), and its load factor (float) are emitted, followed by 134 * the size of the set (the number of elements it contains) 135 * (int), followed by all of its elements (each an Object) in 136 * no particular order. 137 */ 138 private void writeObject(java.io.ObjectOutputStream s) 139 throws java.io.IOException { 140 // Write out any hidden serialization magic 141 s.defaultWriteObject(); 142 143 // Write out size 144 s.writeInt(map.size()); 145 146 // Write out all elements in the proper order. 147 for (Iterator i=map.keySet().iterator(); i.hasNext(); ) 148 s.writeObject(i.next()); 149 } 150 151 /** 152 * Reconstitute the <tt>HashSet</tt> instance from a stream (that is, 153 * deserialize it). 154 */ 155 private void readObject(java.io.ObjectInputStream s) 156 throws java.io.IOException, ClassNotFoundException { 157 // Read in any hidden serialization magic 158 s.defaultReadObject(); 159 160 161 map = new HashTableNotSync(); 162 163 // Read in size 164 int size = s.readInt(); 165 166 // Read in all elements in the proper order. 167 for (int i=0; i<size; i++) { 168 Object e = s.readObject(); 169 map.put(e, PRESENT); 170 } 171 } 172 }