reapply DefaultTransformer update
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141025 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
90017dc7e7
commit
b033502afa
|
@ -54,28 +54,39 @@
|
|||
|
||||
package org.apache.commons.math.util;
|
||||
|
||||
import org.apache.commons.beanutils.ConversionException;
|
||||
import org.apache.commons.beanutils.converters.DoubleConverter;
|
||||
import org.apache.commons.math.MathException;
|
||||
|
||||
/**
|
||||
* A Default NumberTransformer for java.lang.Numbers and Numeric Strings.
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/14 22:22:17 $
|
||||
* A Default NumberTransformer for java.lang.Numbers and Numeric Strings. This
|
||||
* provides some simple conversion capabilities to turn any java/lang.Number
|
||||
* into a primitive double or to turn a String representation of a Number into
|
||||
* a double.
|
||||
*
|
||||
* @version $Revision: 1.9 $ $Date: 2003/11/15 19:02:44 $
|
||||
*/
|
||||
public class DefaultTransformer implements NumberTransformer {
|
||||
/** Converter used to transform objects. */
|
||||
private static final DoubleConverter converter =
|
||||
new DoubleConverter(new Double(Double.NaN));
|
||||
|
||||
/**
|
||||
* @param Object o Is the object that gets transformed.
|
||||
* @return a double primitive representation of the Object o.
|
||||
* @throws org.apache.commons.math.MathException If it cannot successfully
|
||||
* be transformed or is null.
|
||||
* @see org.apache.commons.collections.Transformer#transform(java.lang.Object)
|
||||
*/
|
||||
public double transform(Object o) {
|
||||
double d;
|
||||
try {
|
||||
d = ((Double)converter.convert(Double.class, o)).doubleValue();
|
||||
} catch(ConversionException ex){
|
||||
d = Double.NaN;
|
||||
}
|
||||
return d;
|
||||
public double transform(Object o) throws MathException{
|
||||
|
||||
if (o == null) {
|
||||
throw new MathException("Conversion Exception in Transformation, Object is null");
|
||||
}
|
||||
|
||||
if (o instanceof Number) {
|
||||
return ((Number)o).doubleValue();
|
||||
}
|
||||
|
||||
try {
|
||||
return new Double(o.toString()).doubleValue();
|
||||
} catch (Exception e) {
|
||||
throw new MathException("Conversion Exception in Transformation: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -58,131 +58,129 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
|
||||
/**
|
||||
* This TansformerMap automates the transformation of of mixed object types.
|
||||
* It provides a means to set NumberTransformers that will be selected
|
||||
* based on the Class of the object handed to the Maps
|
||||
* <code>double transform(Object o)</code> method.
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/14 22:22:17 $
|
||||
* @version $Revision: 1.8 $ $Date: 2003/11/15 19:02:44 $
|
||||
*/
|
||||
public class TransformerMap implements NumberTransformer {
|
||||
|
||||
/**
|
||||
* A default Number Transformer for Numbers and numeric Strings.
|
||||
*/
|
||||
private NumberTransformer defaultTransformer = null;
|
||||
/**
|
||||
* A default Number Transformer for Numbers and numeric Strings.
|
||||
*/
|
||||
private NumberTransformer defaultTransformer = null;
|
||||
|
||||
/**
|
||||
* The internal Map.
|
||||
*/
|
||||
private Map map = null;
|
||||
/**
|
||||
* The internal Map.
|
||||
*/
|
||||
private Map map = null;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public TransformerMap() {
|
||||
map = new HashMap();
|
||||
defaultTransformer = new DefaultTransformer();
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public TransformerMap() {
|
||||
map = new HashMap();
|
||||
defaultTransformer = new DefaultTransformer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a Class is present in the TransformerMap.
|
||||
* @param key Class to check
|
||||
* @return true|false
|
||||
*/
|
||||
public boolean containsClass(Class key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
/**
|
||||
* Tests if a Class is present in the TransformerMap.
|
||||
* @param key Class to check
|
||||
* @return true|false
|
||||
*/
|
||||
public boolean containsClass(Class key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a NumberTransformer is present in the TransformerMap.
|
||||
* @param value NumberTransformer to check
|
||||
* @return true|false
|
||||
*/
|
||||
public boolean containsTransformer(NumberTransformer value) {
|
||||
return map.containsValue(value);
|
||||
}
|
||||
/**
|
||||
* Tests if a NumberTransformer is present in the TransformerMap.
|
||||
* @param value NumberTransformer to check
|
||||
* @return true|false
|
||||
*/
|
||||
public boolean containsTransformer(NumberTransformer value) {
|
||||
return map.containsValue(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Transformer that is mapped to a class
|
||||
* if mapping is not present, this returns null.
|
||||
* @param key The Class of the object
|
||||
* @return the mapped NumberTransformer or null.
|
||||
*/
|
||||
public NumberTransformer getTransformer(Class key) {
|
||||
return (NumberTransformer) map.get(key);
|
||||
}
|
||||
/**
|
||||
* Returns the Transformer that is mapped to a class
|
||||
* if mapping is not present, this returns null.
|
||||
* @param key The Class of the object
|
||||
* @return the mapped NumberTransformer or null.
|
||||
*/
|
||||
public NumberTransformer getTransformer(Class key) {
|
||||
return (NumberTransformer) map.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a Class to Transformer Mapping in the Map. If
|
||||
* the Class is already present, this overwrites that
|
||||
* mapping.
|
||||
* @param key The Class
|
||||
* @param transformer The NumberTransformer
|
||||
* @return the replaced transformer if one is present
|
||||
*/
|
||||
public Object putTransformer(Class key, NumberTransformer transformer) {
|
||||
return map.put(key, transformer);
|
||||
}
|
||||
/**
|
||||
* Sets a Class to Transformer Mapping in the Map. If
|
||||
* the Class is already present, this overwrites that
|
||||
* mapping.
|
||||
* @param key The Class
|
||||
* @param transformer The NumberTransformer
|
||||
* @return the replaced transformer if one is present
|
||||
*/
|
||||
public Object putTransformer(Class key, NumberTransformer transformer) {
|
||||
return map.put(key, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a Class to Transformer Mapping in the Map.
|
||||
* @param key The Class
|
||||
* @return the removed transformer if one is present or
|
||||
* null if none was present.
|
||||
*/
|
||||
public Object removeTransformer(Class key) {
|
||||
return map.remove(key);
|
||||
}
|
||||
/**
|
||||
* Removes a Class to Transformer Mapping in the Map.
|
||||
* @param key The Class
|
||||
* @return the removed transformer if one is present or
|
||||
* null if none was present.
|
||||
*/
|
||||
public Object removeTransformer(Class key) {
|
||||
return map.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all the Class to Transformer mappings.
|
||||
*/
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
/**
|
||||
* Clears all the Class to Transformer mappings.
|
||||
*/
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Set of Classes used as keys in the map.
|
||||
* @return Set of Classes
|
||||
*/
|
||||
public Set classes() {
|
||||
return map.keySet();
|
||||
}
|
||||
/**
|
||||
* Returns the Set of Classes used as keys in the map.
|
||||
* @return Set of Classes
|
||||
*/
|
||||
public Set classes() {
|
||||
return map.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Set of NumberTransformers used as values
|
||||
* in the map.
|
||||
* @return Set of NumberTransformers
|
||||
*/
|
||||
public Collection transformers() {
|
||||
return map.values();
|
||||
}
|
||||
/**
|
||||
* Returns the Set of NumberTransformers used as values
|
||||
* in the map.
|
||||
* @return Set of NumberTransformers
|
||||
*/
|
||||
public Collection transformers() {
|
||||
return map.values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to transform the Object against the map of
|
||||
* NumberTransformers. Otherwise it returns Double.NaN.
|
||||
*
|
||||
* @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
|
||||
*/
|
||||
public double transform(Object o) {
|
||||
double value = Double.NaN;
|
||||
/**
|
||||
* Attempts to transform the Object against the map of
|
||||
* NumberTransformers. Otherwise it returns Double.NaN.
|
||||
*
|
||||
* @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
|
||||
*/
|
||||
public double transform(Object o) throws MathException {
|
||||
double value = Double.NaN;
|
||||
|
||||
try {
|
||||
if (o instanceof Number || o instanceof String) {
|
||||
value = defaultTransformer.transform(o);
|
||||
} else {
|
||||
NumberTransformer trans =
|
||||
(NumberTransformer) this.getTransformer(o.getClass());
|
||||
if (trans != null) {
|
||||
value = trans.transform(o);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (o instanceof Number || o instanceof String) {
|
||||
value = defaultTransformer.transform(o);
|
||||
} else {
|
||||
NumberTransformer trans =
|
||||
(NumberTransformer) this.getTransformer(o.getClass());
|
||||
if (trans != null) {
|
||||
value = trans.transform(o);
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
|
@ -56,12 +56,13 @@ package org.apache.commons.math.util;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
import org.apache.commons.math.TestUtils;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.5 $ $Date: 2003/11/14 22:22:23 $
|
||||
* @version $Revision: 1.6 $ $Date: 2003/11/15 19:02:45 $
|
||||
*/
|
||||
public class DefaultTransformerTest extends TestCase {
|
||||
/**
|
||||
|
@ -71,7 +72,12 @@ public class DefaultTransformerTest extends TestCase {
|
|||
double expected = 1.0;
|
||||
Double input = new Double(expected);
|
||||
DefaultTransformer t = new DefaultTransformer();
|
||||
assertEquals(expected, t.transform(input), 1.0e-4);
|
||||
try {
|
||||
assertEquals(expected, t.transform(input), 1.0e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -80,7 +86,12 @@ public class DefaultTransformerTest extends TestCase {
|
|||
public void testTransformNull(){
|
||||
double expected = Double.NaN;
|
||||
DefaultTransformer t = new DefaultTransformer();
|
||||
TestUtils.assertEquals(expected, t.transform(null), 1.0e-4);
|
||||
try {
|
||||
TestUtils.assertEquals(expected, t.transform(null), 1.0e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,7 +101,12 @@ public class DefaultTransformerTest extends TestCase {
|
|||
double expected = 1.0;
|
||||
Integer input = new Integer(1);
|
||||
DefaultTransformer t = new DefaultTransformer();
|
||||
assertEquals(expected, t.transform(input), 1.0e-4);
|
||||
try {
|
||||
assertEquals(expected, t.transform(input), 1.0e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -100,7 +116,12 @@ public class DefaultTransformerTest extends TestCase {
|
|||
double expected = 1.0;
|
||||
BigDecimal input = new BigDecimal("1.0");
|
||||
DefaultTransformer t = new DefaultTransformer();
|
||||
assertEquals(expected, t.transform(input), 1.0e-4);
|
||||
try {
|
||||
assertEquals(expected, t.transform(input), 1.0e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -110,7 +131,12 @@ public class DefaultTransformerTest extends TestCase {
|
|||
double expected = 1.0;
|
||||
String input = "1.0";
|
||||
DefaultTransformer t = new DefaultTransformer();
|
||||
assertEquals(expected, t.transform(input), 1.0e-4);
|
||||
try {
|
||||
assertEquals(expected, t.transform(input), 1.0e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,6 +146,11 @@ public class DefaultTransformerTest extends TestCase {
|
|||
double expected = Double.NaN;
|
||||
Boolean input = Boolean.TRUE;
|
||||
DefaultTransformer t = new DefaultTransformer();
|
||||
TestUtils.assertEquals(expected, t.transform(input), 1.0e-4);
|
||||
try {
|
||||
TestUtils.assertEquals(expected, t.transform(input), 1.0e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue