1 16 package org.apache.commons.beanutils; 17 18 44 public class LazyDynaClass extends BasicDynaClass implements MutableDynaClass { 45 46 49 protected boolean restricted; 50 51 57 protected boolean returnNull = false; 58 59 62 public LazyDynaClass() { 63 this(null, (DynaProperty[])null); 64 } 65 66 71 public LazyDynaClass(String name) { 72 this(name, (DynaProperty[])null); 73 } 74 75 81 public LazyDynaClass(String name, Class dynaBeanClass) { 82 this(name, dynaBeanClass, null); 83 } 84 85 91 public LazyDynaClass(String name, DynaProperty[] properties) { 92 this(name, LazyDynaBean.class, properties); 93 } 94 95 102 public LazyDynaClass(String name, Class dynaBeanClass, DynaProperty properties[]) { 103 super(name, dynaBeanClass, properties); 104 } 105 106 111 public boolean isRestricted() { 112 return restricted; 113 } 114 115 120 public void setRestricted(boolean restricted) { 121 this.restricted = restricted; 122 } 123 124 129 public boolean isReturnNull() { 130 return returnNull; 131 } 132 133 138 public void setReturnNull(boolean returnNull) { 139 this.returnNull = returnNull; 140 } 141 142 152 public void add(String name) { 153 add(new DynaProperty(name)); 154 } 155 156 168 public void add(String name, Class type) { 169 add(new DynaProperty(name, type)); 170 } 171 172 193 public void add(String name, Class type, boolean readable, boolean writeable) { 194 throw new java.lang.UnsupportedOperationException ("readable/writable properties not supported"); 195 } 196 197 206 protected void add(DynaProperty property) { 207 208 if (property.getName() == null) { 209 throw new IllegalArgumentException ("Property name is missing."); 210 } 211 212 if (isRestricted()) { 213 throw new IllegalStateException ("DynaClass is currently restricted. No new properties can be added."); 214 } 215 216 if (propertiesMap.get(property.getName()) != null) { 218 return; 219 } 220 221 DynaProperty[] oldProperties = getDynaProperties(); 223 DynaProperty[] newProperties = new DynaProperty[oldProperties.length+1]; 224 System.arraycopy(oldProperties, 0, newProperties, 0, oldProperties.length); 225 newProperties[oldProperties.length] = property; 226 227 setProperties(newProperties); 229 230 } 231 232 245 public void remove(String name) { 246 247 if (name == null) { 248 throw new IllegalArgumentException ("Property name is missing."); 249 } 250 251 if (isRestricted()) { 252 throw new IllegalStateException ("DynaClass is currently restricted. No properties can be removed."); 253 } 254 255 if (propertiesMap.get(name) == null) { 257 return; 258 } 259 260 261 DynaProperty[] oldProperties = getDynaProperties(); 263 DynaProperty[] newProperties = new DynaProperty[oldProperties.length-1]; 264 int j = 0; 265 for (int i = 0; i < oldProperties.length; i++) { 266 if (!(name.equals(oldProperties[i].getName()))) { 267 newProperties[j] = oldProperties[i]; 268 j++; 269 } 270 } 271 272 setProperties(newProperties); 274 275 } 276 277 299 public DynaProperty getDynaProperty(String name) { 300 301 if (name == null) { 302 throw new IllegalArgumentException ("Property name is missing."); 303 } 304 305 DynaProperty dynaProperty = (DynaProperty)propertiesMap.get(name); 306 307 if (dynaProperty == null && !isReturnNull() && !isRestricted()) { 310 dynaProperty = new DynaProperty(name); 311 } 312 313 return dynaProperty; 314 315 } 316 317 327 public boolean isDynaProperty(String name) { 328 329 if (name == null) { 330 throw new IllegalArgumentException ("Property name is missing."); 331 } 332 333 return propertiesMap.get(name) == null ? false : true; 334 335 } 336 337 } | Popular Tags |