1 7 package com.inversoft.beans; 8 9 10 import java.util.HashMap ; 11 import java.util.Map ; 12 13 import com.inversoft.util.typeconverter.TypeConversionException; 14 15 16 25 public class IndexedBeanProperty extends BeanProperty { 26 27 31 46 public static BeanProperty getInstance(String propertyName, Class beanClass) 47 throws BeanException { 48 return getIndexedInstance(propertyName, beanClass); 49 } 50 51 62 public static IndexedBeanProperty getIndexedInstance(String propertyName, 63 Class beanClass) 64 throws BeanException { 65 66 if (propertyName == null || propertyName.indexOf(".") != -1) { 67 throw new BeanException("Invalid propertyName: " + propertyName); 68 } 69 70 if (!CACHING) { 72 return new IndexedBeanProperty(propertyName, beanClass); 73 } 74 75 synchronized (cache) { 76 Map propMap = (Map ) cache.get(beanClass); 78 if (propMap == null) { 79 propMap = new HashMap (); 80 cache.put(beanClass, propMap); 81 } 82 83 BeanProperty bp = (BeanProperty) propMap.get(propertyName); 85 if (bp == null) { 86 bp = new SynchronizedIndexedBeanProperty(propertyName, beanClass); 87 propMap.put(propertyName, bp); 88 } else { 89 if (bp.getClass() != SynchronizedIndexedBeanProperty.class) { 90 throw new BeanException("Looking for indexed property but " + 91 "cached version is not indexed for property named: " + 92 propertyName); 93 } 94 } 95 96 return (IndexedBeanProperty) bp; 97 } 98 } 99 100 101 108 protected IndexedBeanProperty() { 109 } 111 112 131 public IndexedBeanProperty(String propertyName, Class beanClass) 132 throws BeanException { 133 super(propertyName, beanClass); } 135 136 155 public IndexedBeanProperty(String propertyName, String beanClass) 156 throws BeanException { 157 super(propertyName, beanClass); } 159 160 166 protected void initialize() throws BeanException { 167 168 read = JavaBeanTools.findIndexedReadMethod(propertyName, beanClass); 169 propertyType = read.getReturnType(); 170 171 try { 172 write = JavaBeanTools.findIndexedWriteMethod(propertyName, beanClass, 173 propertyType); 174 } catch (BeanException be) { 175 } 177 } 178 179 182 public boolean isIndexable(Object bean) { 183 return true; 184 } 185 186 196 public Object getPropertyValue(final Object bean, final int index) 197 throws BeanException { 198 return getPropertyValue(bean, new Integer (index)); 199 } 200 201 204 public Object getPropertyValue(Object bean) throws BeanException { 205 throw new BeanException("This operation not supported, getPropertyValue " + 206 "must have an indices for property named: " + getPropertyName()); 207 } 208 209 215 public Object getPropertyValue(final Object bean, final Object key) 216 throws BeanException { 217 Integer index; 218 try { 219 index = (Integer ) key; 220 } catch (ClassCastException cce) { 221 throw new BeanException("This operation not supported, getPropertyValue " + 222 "must have an integer indices for property named: " + getPropertyName()); 223 } 224 225 Object value = JavaBeanTools.callIndexedGetter(this, bean, index); 226 if (hasPropertyListeners()) { 227 firePropertyEvent(GET, value, value, bean, index); 228 } 229 230 return value; 231 } 232 233 247 public void setPropertyValue(final Object bean, final int index, Object value, 248 final boolean convert) throws BeanException, TypeConversionException { 249 setPropertyValue(bean, new Integer (index), value, convert); 250 } 251 252 264 public void setPropertyValue(final Object bean, final int index, Object value) 265 throws BeanException { 266 try { 267 setPropertyValue(bean, index, value, false); 268 } catch (TypeConversionException tce) { 269 assert false : "FATAL: should never throw TypeConversionException" + 270 " because this method does no conversion"; 271 } 272 } 273 274 277 public void setPropertyValue(final Object bean, Object value, 278 final boolean convert) 279 throws BeanException { 280 throw new BeanException("This operation not supported, setPropertyValue " + 281 "must have an indices for property named: " + getPropertyName()); 282 } 283 284 291 public void setPropertyValue(final Object bean, final Object key, Object value, 292 final boolean convert) 293 throws BeanException, TypeConversionException { 294 Integer index; 295 try { 296 index = (Integer ) key; 297 } catch (ClassCastException cce) { 298 throw new BeanException("This operation not supported, " + 299 "setPropertyValue must have an indices for property named: " + 300 getPropertyName()); 301 } 302 303 304 if (convert) { 306 value = convertParameter(value, bean, propertyType); 307 } 308 309 Object oldValue = null; 311 312 if (hasPropertyListeners()) { 313 oldValue = JavaBeanTools.callIndexedGetter(this, bean, index); 314 } 315 316 JavaBeanTools.callIndexedSetter(this, bean, index, value); 317 318 if (hasPropertyListeners()) { 319 firePropertyEvent(SET, oldValue, value, bean, index); 320 } 321 } 322 } | Popular Tags |