View Javadoc

1   /*
2    * $Id: StringToNumber.java 11748 2008-05-14 07:17:24Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.example.scripting;
12  
13  
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.transformer.AbstractTransformer;
16  import org.mule.util.NumberUtils;
17  
18  /** 
19   * Converts a string to a number.
20   */
21  public class StringToNumber extends AbstractTransformer 
22  {
23      /** Convert the string to an integer (by default it will convert it to a double) */
24      private boolean integer = false;
25      
26      public StringToNumber()
27      {
28          registerSourceType(String.class);
29          setReturnClass(Number.class);
30      }
31  
32      public Object doTransform(Object src, String encoding) throws TransformerException
33      {         
34          if (integer)
35          {
36              return new Integer(NumberUtils.toInt(src));
37          }
38          else
39          {
40              return new Double(NumberUtils.toDouble(src));
41          }
42      }
43  
44      public boolean isInteger()
45      {
46          return integer;
47      }
48  
49      public void setInteger(boolean integer)
50      {
51          this.integer = integer;
52      }
53  }