1 10 11 package com.triactive.jdo.test; 12 13 import java.util.Hashtable ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.Map.Entry; 17 import javax.jdo.InstanceCallbacks; 18 import javax.jdo.JDOHelper; 19 import javax.jdo.PersistenceManager; 20 import junit.framework.Assert; 21 22 23 public class HashtableWidget extends Widget implements InstanceCallbacks 24 { 25 private Hashtable normalMap = new Hashtable (); 26 private Hashtable normalObjectMap = new Hashtable (); 27 private int numWidgets = 0; 28 29 30 public HashtableWidget() 31 { 32 } 33 34 35 public Map getNormalMap() 36 { 37 return normalMap; 38 } 39 40 41 public Map getNormalObjectMap() 42 { 43 return normalObjectMap; 44 } 45 46 47 public int getNumWidgets() 48 { 49 return numWidgets; 50 } 51 52 53 public Object [] getSCOFieldValues() 54 { 55 return new Object [] { normalMap, normalObjectMap }; 56 } 57 58 59 public Object clone() 60 { 61 HashtableWidget mw = (HashtableWidget)super.clone(); 62 63 64 mw.normalMap = new Hashtable (); 65 mw.normalObjectMap = new Hashtable (); 66 67 Iterator i = normalMap.entrySet().iterator(); 68 69 while (i.hasNext()) 70 { 71 Entry e = (Entry)i.next(); 72 String ks = (String )e.getKey(); 73 Widget v = (Widget)clone(e.getValue()); 74 75 Integer ki = new Integer (v.getIntField()); 76 77 mw.normalMap.put(ks, v); 78 mw.normalObjectMap.put(ki, v); 79 } 80 81 return mw; 82 } 83 84 85 88 89 public void fillRandom() 90 { 91 fillRandom(r.nextInt(5)); 92 } 93 94 95 public void fillRandom(int numWidgets) 96 { 97 fillRandom(numWidgets, false); 98 } 99 100 101 105 106 public void fillRandom(int numWidgets, boolean includeHtWidgets) 107 { 108 super.fillRandom(); 109 110 113 Iterator i = normalMap.entrySet().iterator(); 114 115 while (i.hasNext()) 116 { 117 Entry e = (Entry)i.next(); 118 Object skey = e.getKey(); 119 Widget val = (Widget)e.getValue(); 120 121 i.remove(); 122 123 Integer ikey = new Integer (val.getIntField()); 124 125 Assert.assertFalse("normalMap.containsKey() did not return false after removing existing key", normalMap.containsKey(skey)); 126 Assert.assertTrue("normalObjectMap.remove() did not return non-null after removing existing key", normalObjectMap.remove(ikey) != null); 127 Assert.assertTrue("normalObjectMap.remove() did not return null attempting to remove non-existent key", normalObjectMap.remove(ikey) == null); 128 129 if (JDOHelper.isPersistent(this)) 130 JDOHelper.getPersistenceManager(this).deletePersistent(val); 131 } 132 133 Assert.assertTrue("normalMap should have been empty", normalMap.isEmpty()); 134 Assert.assertTrue("normalObjectMap should have been empty", normalObjectMap.isEmpty()); 135 136 140 this.numWidgets = numWidgets; 141 142 while (numWidgets-- > 0) 143 { 144 Widget val; 145 146 switch (r.nextInt(includeHtWidgets ? 6 : 5)) 147 { 148 case 0: 149 default: 150 val = new Widget(); 151 break; 152 case 1: 153 val = new DateWidget(); 154 break; 155 case 2: 156 val = new StringWidget(); 157 break; 158 case 3: 159 val = new BinaryWidget(); 160 break; 161 case 4: 162 val = new FloatWidget(); 163 break; 164 case 5: 165 val = new HashtableWidget(); 166 break; 167 } 168 169 Integer key; 170 171 do 172 { 173 val.fillRandom(); 174 key = new Integer (val.getIntField()); 175 } while (normalObjectMap.containsKey(key)); 176 177 normalMap.put("K" + key, val); 178 normalObjectMap.put(key, val); 179 } 180 181 validate(); 182 } 183 184 185 private void validate() 186 { 187 Assert.assertEquals("numWidgets != normalMap.size(): " + this, numWidgets, normalMap.size()); 188 Assert.assertEquals("numWidgets != normalObjectMap.size(): " + this, numWidgets, normalObjectMap.size()); 189 } 190 191 192 203 204 public boolean compareTo(Object obj) 205 { 206 validate(); 207 208 if (obj == this) 209 return true; 210 211 if (!(obj instanceof HashtableWidget) || !super.compareTo(obj)) 212 return false; 213 214 HashtableWidget w = (HashtableWidget)obj; 215 216 w.validate(); 217 218 return compareMap(normalMap, w.normalMap) && 219 compareMap(normalObjectMap, w.normalObjectMap); 220 } 221 222 223 229 230 public String toString() 231 { 232 StringBuffer s = new StringBuffer (super.toString()); 233 234 s.append(" normalMap = ").append(normalMap); 235 s.append('\n'); 236 s.append(" normalObjectMap = ").append(normalObjectMap); 237 s.append('\n'); 238 s.append(" numWidgets = ").append(numWidgets); 239 s.append('\n'); 240 241 return s.toString(); 242 } 243 244 245 public void jdoPostLoad() {} 246 public void jdoPreClear() {} 247 public void jdoPreStore() {} 248 249 250 public void jdoPreDelete() 251 { 252 PersistenceManager myPM = JDOHelper.getPersistenceManager(this); 253 Object [] values = normalMap.values().toArray(); 254 255 normalMap.clear(); 256 normalObjectMap.clear(); 257 258 myPM.deletePersistentAll(values); 259 } 260 } 261 | Popular Tags |