KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > util > BeanTransformer


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.util;
17
18 import java.beans.Expression JavaDoc;
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import org.apache.commons.math.MathException;
21
22 /**
23  * Uses PropertyUtils to map a Bean getter to a double value.
24  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
25  */

26 public class BeanTransformer implements NumberTransformer {
27
28     /**
29      * The propertyName for this Transformer
30      */

31     private String JavaDoc propertyName = null;
32     
33     private String JavaDoc propertyGetter = null;
34
35     /**
36      * Create a BeanTransformer
37      */

38     public BeanTransformer() {
39         super();
40     }
41
42     /**
43      * Create a BeanTransformer with a specific PropertyName.
44      * @param property The property.
45      */

46     public BeanTransformer(final String JavaDoc property) {
47         super();
48         setPropertyName(property);
49     }
50
51     /**
52      * Get the property String
53      * @return the Property Name String
54      */

55     public String JavaDoc getPropertyName() {
56         return propertyName;
57     }
58
59     /**
60      * Set the propertyString
61      * @param string The string to set the property to.
62      */

63     public void setPropertyName(final String JavaDoc string) {
64         this.propertyName = string;
65         this.propertyGetter = "get" + string.substring(0,1).toUpperCase() + string.substring(1);
66     }
67
68     
69     /**
70      * @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
71      */

72     public double transform(final Object JavaDoc o) throws MathException {
73         Expression JavaDoc expr = new Expression JavaDoc(o, propertyGetter, new Object JavaDoc[0]);
74         Object JavaDoc result;
75         try {
76             expr.execute();
77             result = expr.getValue();
78         } catch (IllegalAccessException JavaDoc e) {
79             throw new MathException("IllegalAccessException in Transformation: " + e.getMessage(), e);
80         } catch (InvocationTargetException JavaDoc e) {
81             throw new MathException("InvocationTargetException in Transformation: " + e.getMessage(), e);
82         } catch (NoSuchMethodException JavaDoc e) {
83             throw new MathException("NoSuchMethodException in Transformation: " + e.getMessage(), e);
84         } catch (ClassCastException JavaDoc e) {
85             throw new MathException("ClassCastException in Transformation: " + e.getMessage(), e);
86         } catch (Exception JavaDoc e) {
87             throw new MathException("Exception in Transformation: " + e.getMessage(), e);
88         }
89         
90         return ((Number JavaDoc) result).doubleValue();
91     }
92 }
Popular Tags