1 16 package org.apache.commons.beanutils; 17 18 import java.util.Map ; 19 import java.util.Iterator ; 20 21 47 public class LazyDynaMap extends LazyDynaBean implements MutableDynaClass { 48 49 53 protected String name; 54 55 58 protected boolean restricted; 59 60 66 protected boolean returnNull = false; 67 68 69 71 74 public LazyDynaMap() { 75 this(null, (Map )null); 76 } 77 78 83 public LazyDynaMap(String name) { 84 this(name, (Map )null); 85 } 86 87 92 public LazyDynaMap(Map values) { 93 this(null, values); 94 } 95 96 102 public LazyDynaMap(String name, Map values) { 103 this.name = name == null ? "LazyDynaMap" : name; 104 this.values = values == null ? newMap() : values; 105 this.dynaClass = this; 106 } 107 108 113 public LazyDynaMap(DynaProperty[] properties) { 114 this(null, properties); 115 } 116 117 123 public LazyDynaMap(String name, DynaProperty[] properties) { 124 this(name, (Map )null); 125 if (properties != null) { 126 for (int i = 0; i < properties.length; i++) { 127 add(properties[i]); 128 } 129 } 130 } 131 132 137 public LazyDynaMap(DynaClass dynaClass) { 138 this(dynaClass.getName(), dynaClass.getDynaProperties()); 139 } 140 141 143 146 public void setMap(Map values) { 147 this.values = values; 148 } 149 150 152 158 public void set(String name, Object value) { 159 160 if (isRestricted() && !values.containsKey(name)) { 161 throw new IllegalArgumentException 162 ("Invalid property name '" + name + "' (DynaClass is restricted)"); 163 } 164 165 values.put(name, value); 166 167 } 168 169 171 175 public String getName() { 176 return this.name; 177 } 178 179 201 public DynaProperty getDynaProperty(String name) { 202 203 if (name == null) 204 throw new IllegalArgumentException ("Property name is missing."); 205 206 if (!values.containsKey(name) && isReturnNull()) { 209 return null; 210 } 211 212 Object value = values.get(name); 213 214 if (value == null) { 215 return new DynaProperty(name); 216 } else { 217 return new DynaProperty(name, value.getClass()); 218 } 219 220 } 221 222 231 public DynaProperty[] getDynaProperties() { 232 233 int i = 0; 234 DynaProperty[] properties = new DynaProperty[values.size()]; 235 Iterator iterator = values.keySet().iterator(); 236 237 while (iterator.hasNext()) { 238 String name = (String )iterator.next(); 239 Object value = values.get(name); 240 properties[i++] = new DynaProperty(name, value == null ? null : value.getClass()); 241 } 242 243 return properties; 244 245 } 246 247 251 public DynaBean newInstance() { 252 return new LazyDynaMap(this); 253 } 254 255 256 258 263 public boolean isRestricted() { 264 return restricted; 265 } 266 267 272 public void setRestricted(boolean restricted) { 273 this.restricted = restricted; 274 } 275 276 284 public void add(String name) { 285 add(name, null); 286 } 287 288 300 public void add(String name, Class type) { 301 302 if (name == null) { 303 throw new IllegalArgumentException ("Property name is missing."); 304 } 305 306 if (isRestricted()) 307 throw new IllegalStateException ("DynaClass is currently restricted. No new properties can be added."); 308 309 Object value = values.get(name); 310 311 if (value == null) { 313 values.put(name, type == null ? null : createProperty(name, type)); 314 } 315 316 } 317 318 339 public void add(String name, Class type, boolean readable, boolean writeable) { 340 throw new java.lang.UnsupportedOperationException ("readable/writable properties not supported"); 341 } 342 343 350 protected void add(DynaProperty property) { 351 add(property.getName(), property.getType()); 352 } 353 354 367 public void remove(String name) { 368 369 if (name == null) { 370 throw new IllegalArgumentException ("Property name is missing."); 371 } 372 373 if (isRestricted()) { 374 throw new IllegalStateException ("DynaClass is currently restricted. No properties can be removed."); 375 } 376 377 if (values.containsKey(name)) { 379 values.remove(name); 380 } 381 382 } 383 384 385 387 392 public boolean isReturnNull() { 393 return returnNull; 394 } 395 396 401 public void setReturnNull(boolean returnNull) { 402 this.returnNull = returnNull; 403 } 404 405 406 408 418 protected boolean isDynaProperty(String name) { 419 420 if (name == null) { 421 throw new IllegalArgumentException ("Property name is missing."); 422 } 423 424 return values.containsKey(name); 425 426 } 427 428 } | Popular Tags |