commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From di...@apache.org
Subject cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/util DefaultTransformerTest.java
Date Sat, 15 Nov 2003 19:02:45 GMT
dirkv       2003/11/15 11:02:45

  Modified:    math/src/java/org/apache/commons/math/util
                        DefaultTransformer.java TransformerMap.java
               math/src/test/org/apache/commons/math/util
                        DefaultTransformerTest.java
  Log:
  reapply DefaultTransformer update
  
  Revision  Changes    Path
  1.9       +26 -15    jakarta-commons/math/src/java/org/apache/commons/math/util/DefaultTransformer.java
  
  Index: DefaultTransformer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/util/DefaultTransformer.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- DefaultTransformer.java	14 Nov 2003 22:22:17 -0000	1.8
  +++ DefaultTransformer.java	15 Nov 2003 19:02:44 -0000	1.9
  @@ -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. 
  + * 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$ $Date$
    */
   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);
  +		}
       }
   }
  
  
  
  1.8       +115 -117  jakarta-commons/math/src/java/org/apache/commons/math/util/TransformerMap.java
  
  Index: TransformerMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/util/TransformerMap.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TransformerMap.java	14 Nov 2003 22:22:17 -0000	1.7
  +++ TransformerMap.java	15 Nov 2003 19:02:44 -0000	1.8
  @@ -58,6 +58,8 @@
   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 
  @@ -67,122 +69,118 @@
    */
   public class TransformerMap implements NumberTransformer {
   
  -    /**
  -     * A default Number Transformer for Numbers and numeric Strings.
  -     */
  -    private NumberTransformer defaultTransformer = null;
  -
  -    /**
  -     * The internal Map.
  -     */
  -    private Map map = null;
  -
  -    /**
  -     * 
  -     */
  -    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 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);
  -    }
  -
  -    /**
  -     * 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);
  -    }
  -
  -    /**
  -     * 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 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;
  -
  -        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();
  -        }
  +	/**
  +	 * A default Number Transformer for Numbers and numeric Strings.
  +	 */
  +	private NumberTransformer defaultTransformer = null;
  +
  +	/**
  +	 * The internal Map.
  +	 */
  +	private Map map = null;
  +
  +	/**
  +	 * 
  +	 */
  +	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 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);
  +	}
  +
  +	/**
  +	 * 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);
  +	}
  +
  +	/**
  +	 * 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 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) throws MathException {
  +		double value = Double.NaN;
  +
  +		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;
  +	}
   
   }
  
  
  
  1.6       +38 -7     jakarta-commons/math/src/test/org/apache/commons/math/util/DefaultTransformerTest.java
  
  Index: DefaultTransformerTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/util/DefaultTransformerTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DefaultTransformerTest.java	14 Nov 2003 22:22:23 -0000	1.5
  +++ DefaultTransformerTest.java	15 Nov 2003 19:02:45 -0000	1.6
  @@ -56,6 +56,7 @@
   
   import java.math.BigDecimal;
   
  +import org.apache.commons.math.MathException;
   import org.apache.commons.math.TestUtils;
   
   import junit.framework.TestCase;
  @@ -71,7 +72,12 @@
           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 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 @@
           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 @@
           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 @@
           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 @@
           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();
  +		}
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message