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