KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > stat > univariate > BeanListUnivariateImpl


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.math.stat.univariate;
17
18 import java.io.Serializable JavaDoc;
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
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 /**
31  * This implementation of DescriptiveStatistics uses commons-beanutils to gather
32  * univariate statistics for a List of Java Beans by property. This
33  * implementation uses beanutils' PropertyUtils to get a simple, nested,
34  * indexed, mapped, or combined property from an element of a List.
35  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
36  */

37 public class BeanListUnivariateImpl extends ListUnivariateImpl implements Serializable JavaDoc {
38
39     /** Serializable version identifier */
40     static final long serialVersionUID = -6428201899045406285L;
41     
42     /**
43      * propertyName of the property to get from the bean
44      */

45     private String JavaDoc propertyName;
46
47     /**
48      * No argument Constructor
49      */

50     public BeanListUnivariateImpl(){
51         this(new ArrayList JavaDoc());
52     }
53     
54     /**
55      * Construct a BeanListUnivariate with specified
56      * backing list
57      * @param list Backing List
58      */

59     public BeanListUnivariateImpl(List JavaDoc list) {
60         this(list, null);
61     }
62
63     /**
64      * Construct a BeanListUnivariate with specified
65      * backing list and propertyName
66      * @param list Backing List
67      * @param propertyName Bean propertyName
68      */

69     public BeanListUnivariateImpl(List JavaDoc list, String JavaDoc propertyName) {
70         super(list);
71         setPropertyName(propertyName);
72     }
73
74     /**
75      * @return propertyName
76      */

77     public String JavaDoc getPropertyName() {
78         return propertyName;
79     }
80
81     /**
82      * @param propertyName Name of Property
83      */

84     public void setPropertyName(String JavaDoc propertyName) {
85         this.propertyName = propertyName;
86         this.transformer = new NumberTransformer() {
87
88             /**
89              * @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
90              */

91             public double transform(final Object JavaDoc o) throws MathException {
92                 try {
93                     return (
94                         (Number JavaDoc) PropertyUtils.getProperty(
95                             o,
96                             getPropertyName()))
97                         .doubleValue();
98                 } catch (IllegalAccessException JavaDoc e) {
99                     throw new MathException(
100                         "IllegalAccessException in Transformation: "
101                             + e.getMessage(),
102                         e);
103                 } catch (InvocationTargetException JavaDoc e) {
104                     throw new MathException(
105                         "InvocationTargetException in Transformation: "
106                             + e.getMessage(),
107                         e);
108                 } catch (NoSuchMethodException JavaDoc e) {
109                     throw new MathException(
110                         "oSuchMethodException in Transformation: "
111                             + e.getMessage(),
112                         e);
113                 }
114             }
115         };
116     }
117
118     /**
119       * Creates a {@link org.apache.commons.beanutils.DynaBean} with a
120       * {@link org.apache.commons.beanutils.DynaProperty} named
121       * <code>propertyName,</code> sets the value of the property to <code>v</code>
122       * and adds the DynaBean to the underlying list.
123       *
124       */

125     public void addValue(double v) {
126         DynaProperty[] props = new DynaProperty[] {
127                 new DynaProperty(propertyName, Double JavaDoc.class)
128         };
129         BasicDynaClass dynaClass = new BasicDynaClass(null, null, props);
130         DynaBean dynaBean = null;
131         try {
132             dynaBean = dynaClass.newInstance();
133         } catch (Exception JavaDoc ex) { // InstantiationException, IllegalAccessException
134
throw new RuntimeException JavaDoc(ex); // should never happen
135
}
136         dynaBean.set(propertyName, new Double JavaDoc(v));
137         addObject(dynaBean);
138     }
139
140     /**
141      * Adds a bean to this list.
142      *
143      * @param bean Bean to add to the list
144      */

145     public void addObject(Object JavaDoc bean) {
146         list.add(bean);
147     }
148 }
149
Popular Tags