| 1 30 31 32 33 package jbet; 34 import java.util.*; 35 36 public class InternSet implements Collection 37 { 38 Hashtable h; 39 40 public InternSet() { h = new Hashtable(); } 41 public InternSet (InternSet in) { h = new Hashtable (in.h); } 42 public InternSet (Object o) { this(); add (o); } 43 44 public Iterator iterator () { return h.values().iterator(); } 45 public int size() { return h.size(); } 46 public boolean isEmpty() { return h.isEmpty(); } 47 public boolean contains (Object o) { return h.get (o) != null; } 48 public boolean add (Object o) { if (o == null) throw new IllegalStateException ("add(null)"); return null == h.put (o, o); } 49 public boolean remove (Object o) { return null != h.remove (o); } 50 public void clear() { h.clear (); } 51 public Object clone () throws CloneNotSupportedException { 52 throw new CloneNotSupportedException ("no InternSet.clone"); 53 } 54 55 public boolean addAll (Collection c) { 56 boolean change = false; 57 for (Iterator i = c.iterator(); i.hasNext(); ) 58 if (add (i.next())) 59 change = true; 60 return change; 61 } 62 63 public boolean removeAll (Collection c) { 64 boolean change = false; 65 for (Iterator i = c.iterator(); i.hasNext(); ) 66 if (remove (i.next())) 67 change = true; 68 return change; 69 } 70 71 public boolean retainAll (Collection c) { 72 boolean change = false; 73 for (Iterator i = h.values().iterator(); i.hasNext(); ) { 74 Object o = i.next(); 75 if (!c.contains (o)) { 76 remove (o); 77 change = true; 78 } 79 } 80 return change; 81 } 82 83 public boolean containsAll (Collection c) { 84 boolean all = true; 85 for (Iterator i = c.iterator(); i.hasNext(); ) 86 all &= contains (i.next()); 87 return all; 88 } 89 90 public Object [] toArray () { 91 Object [] a = new Object [size()]; 92 int n = 0; 93 for (Iterator i = h.values().iterator(); i.hasNext(); ) 94 a[n++] = i.next(); 95 if (n != a.length) 96 throw new IllegalStateException ("InternSet.toArray failed"); 97 return a; 98 } 99 100 public Object [] toArray (Object [] a) { 101 if (a.length < size()) { 102 a = (Object []) 103 java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size()); 104 } 105 int n = 0; 106 for (Iterator i = h.values().iterator(); i.hasNext(); ) 107 a[n++] = i.next(); 108 if (n != a.length) 109 throw new IllegalStateException ("InternSet.toArray failed"); 110 return a; 111 } 112 113 public Object add2 (Object in) { 114 Object o = h.get (in); 115 if (o != null) 116 return o; 117 118 h.put (in, in); 119 return in; 120 } 121 122 public java.io.ObjectOutputStream writeObject () { throw new IllegalStateException ("no InternSet.writeObject"); } 123 public void readObject (java.io.ObjectInputStream o) { throw new IllegalStateException ("no InternSet.readObject"); } 124 125 public static InternSet EMPTY = new InternSet(); 126 } 127 | Popular Tags |