1 16 package org.apache.commons.math.stat.univariate; 17 18 import java.io.Serializable ; 19 import java.lang.reflect.InvocationTargetException ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 import org.apache.commons.beanutils.PropertyUtils; 24 import org.apache.commons.beanutils.DynaBean; 25 import org.apache.commons.beanutils.BasicDynaClass; 26 import org.apache.commons.beanutils.DynaProperty; 27 import org.apache.commons.math.MathException; 28 import org.apache.commons.math.util.NumberTransformer; 29 30 37 public class BeanListUnivariateImpl extends ListUnivariateImpl implements Serializable { 38 39 40 static final long serialVersionUID = -6428201899045406285L; 41 42 45 private String propertyName; 46 47 50 public BeanListUnivariateImpl(){ 51 this(new ArrayList ()); 52 } 53 54 59 public BeanListUnivariateImpl(List list) { 60 this(list, null); 61 } 62 63 69 public BeanListUnivariateImpl(List list, String propertyName) { 70 super(list); 71 setPropertyName(propertyName); 72 } 73 74 77 public String getPropertyName() { 78 return propertyName; 79 } 80 81 84 public void setPropertyName(String propertyName) { 85 this.propertyName = propertyName; 86 this.transformer = new NumberTransformer() { 87 88 91 public double transform(final Object o) throws MathException { 92 try { 93 return ( 94 (Number ) PropertyUtils.getProperty( 95 o, 96 getPropertyName())) 97 .doubleValue(); 98 } catch (IllegalAccessException e) { 99 throw new MathException( 100 "IllegalAccessException in Transformation: " 101 + e.getMessage(), 102 e); 103 } catch (InvocationTargetException e) { 104 throw new MathException( 105 "InvocationTargetException in Transformation: " 106 + e.getMessage(), 107 e); 108 } catch (NoSuchMethodException e) { 109 throw new MathException( 110 "oSuchMethodException in Transformation: " 111 + e.getMessage(), 112 e); 113 } 114 } 115 }; 116 } 117 118 125 public void addValue(double v) { 126 DynaProperty[] props = new DynaProperty[] { 127 new DynaProperty(propertyName, Double .class) 128 }; 129 BasicDynaClass dynaClass = new BasicDynaClass(null, null, props); 130 DynaBean dynaBean = null; 131 try { 132 dynaBean = dynaClass.newInstance(); 133 } catch (Exception ex) { throw new RuntimeException (ex); } 136 dynaBean.set(propertyName, new Double (v)); 137 addObject(dynaBean); 138 } 139 140 145 public void addObject(Object bean) { 146 list.add(bean); 147 } 148 } 149 | Popular Tags |