1 16 package org.quartz.utils; 17 18 import java.io.Serializable ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 33 public class StringKeyDirtyFlagMap extends DirtyFlagMap { 34 static final long serialVersionUID = -9076749120524952280L; 35 36 41 private boolean allowsTransientData = false; 42 43 public StringKeyDirtyFlagMap() { 44 super(); 45 } 46 47 public StringKeyDirtyFlagMap(int initialCapacity) { 48 super(initialCapacity); 49 } 50 51 public StringKeyDirtyFlagMap(int initialCapacity, float loadFactor) { 52 super(initialCapacity, loadFactor); 53 } 54 55 58 public String [] getKeys() { 59 return (String []) keySet().toArray(new String [size()]); 60 } 61 62 71 public void setAllowsTransientData(boolean allowsTransientData) { 72 73 if (containsTransientData() && !allowsTransientData) { 74 throw new IllegalStateException ( 75 "Cannot set property 'allowsTransientData' to 'false' " 76 + "when data map contains non-serializable objects."); 77 } 78 79 this.allowsTransientData = allowsTransientData; 80 } 81 82 90 public boolean getAllowsTransientData() { 91 return allowsTransientData; 92 } 93 94 103 public boolean containsTransientData() { 104 if (!getAllowsTransientData()) { return false; 106 } 107 108 String [] keys = getKeys(); 109 for (int i = 0; i < keys.length; i++) { 110 Object o = super.get(keys[i]); 111 if (!(o instanceof Serializable )) { 112 return true; 113 } 114 } 115 116 return false; 117 } 118 119 127 public void removeTransientData() { 128 if (!getAllowsTransientData()) { return; 130 } 131 132 String [] keys = getKeys(); 133 for (int i = 0; i < keys.length; i++) { 134 Object o = super.get(keys[i]); 135 if (!(o instanceof Serializable )) { 136 remove(keys[i]); 137 } 138 } 139 } 140 141 151 public void putAll(Map map) { 152 for (Iterator entryIter = map.entrySet().iterator(); entryIter.hasNext();) { 153 Map.Entry entry = (Map.Entry ) entryIter.next(); 154 155 put(entry.getKey(), entry.getValue()); 157 } 158 } 159 160 165 public void put(String key, int value) { 166 super.put(key, new Integer (value)); 167 } 168 169 174 public void put(String key, long value) { 175 super.put(key, new Long (value)); 176 } 177 178 183 public void put(String key, float value) { 184 super.put(key, new Float (value)); 185 } 186 187 192 public void put(String key, double value) { 193 super.put(key, new Double (value)); 194 } 195 196 201 public void put(String key, boolean value) { 202 super.put(key, new Boolean (value)); 203 } 204 205 210 public void put(String key, char value) { 211 super.put(key, new Character (value)); 212 } 213 214 219 public void put(String key, String value) { 220 super.put(key, value); 221 } 222 223 228 public Object put(Object key, Object value) { 229 if (!(key instanceof String )) { 230 throw new IllegalArgumentException ( 231 "Keys in map must be Strings."); 232 } 233 234 return super.put(key, value); 235 } 236 237 245 public int getInt(String key) { 246 Object obj = get(key); 247 248 try { 249 return ((Integer ) obj).intValue(); 250 } catch (Exception e) { 251 throw new ClassCastException ("Identified object is not an Integer."); 252 } 253 } 254 255 263 public long getLong(String key) { 264 Object obj = get(key); 265 266 try { 267 return ((Long ) obj).longValue(); 268 } catch (Exception e) { 269 throw new ClassCastException ("Identified object is not a Long."); 270 } 271 } 272 273 281 public float getFloat(String key) { 282 Object obj = get(key); 283 284 try { 285 return ((Float ) obj).floatValue(); 286 } catch (Exception e) { 287 throw new ClassCastException ("Identified object is not a Float."); 288 } 289 } 290 291 299 public double getDouble(String key) { 300 Object obj = get(key); 301 302 try { 303 return ((Double ) obj).doubleValue(); 304 } catch (Exception e) { 305 throw new ClassCastException ("Identified object is not a Double."); 306 } 307 } 308 309 317 public boolean getBoolean(String key) { 318 Object obj = get(key); 319 320 try { 321 return ((Boolean ) obj).booleanValue(); 322 } catch (Exception e) { 323 throw new ClassCastException ("Identified object is not a Boolean."); 324 } 325 } 326 327 335 public char getChar(String key) { 336 Object obj = get(key); 337 338 try { 339 return ((Character ) obj).charValue(); 340 } catch (Exception e) { 341 throw new ClassCastException ("Identified object is not a Character."); 342 } 343 } 344 345 353 public String getString(String key) { 354 Object obj = get(key); 355 356 try { 357 return (String ) obj; 358 } catch (Exception e) { 359 throw new ClassCastException ("Identified object is not a String."); 360 } 361 } 362 } 363 | Popular Tags |