View Javadoc

1   /*
2    * $Id: SimpleMathTransformer.java 19366 2010-09-04 03:26:47Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.DataType;
15  import org.mule.api.transformer.TransformerException;
16  import org.mule.config.i18n.MessageFactory;
17  import org.mule.transformer.AbstractTransformer;
18  import org.mule.transformer.types.DataTypeFactory;
19  import org.mule.util.NumberUtils;
20  
21  /**
22   * A simple transformer which adds/subtracts/multiplies/divides a constant factor to numeric messages.
23   */
24  public class SimpleMathTransformer extends AbstractTransformer
25  {
26      /** Operation to perform: "add", "subtract", "multiply", "divide" */
27      private String operation = "add";
28      
29      /** Factor to be applied */
30      private double factor;
31      
32      public SimpleMathTransformer()
33      {
34          DataType<Number> numberDataType = DataTypeFactory.create(Number.class);
35          registerSourceType(numberDataType);
36          setReturnDataType(numberDataType);
37      }
38  
39      @Override
40      public Object doTransform(Object src, String outputEncoding) throws TransformerException
41      {
42          double data = NumberUtils.toDouble(src);
43          if (data == NumberUtils.DOUBLE_ERROR)
44          {
45              throw new TransformerException(MessageFactory.createStaticMessage("Unable to convert message to double: " + src));
46          }
47          
48          double result;
49          if (operation.equalsIgnoreCase("add"))
50          {
51              result = data + factor;
52          }
53          else if (operation.equalsIgnoreCase("subtract"))
54          {
55              result = data - factor;
56          }
57          else if (operation.equalsIgnoreCase("multiply"))
58          {
59              result = data * factor;
60          }
61          else if (operation.equalsIgnoreCase("divide"))
62          {
63              result = data / factor;
64          }
65          else
66          {
67              throw new TransformerException(MessageFactory.createStaticMessage("Operation " + operation + " not recognized"));
68          }
69          
70          // no auto-boxing
71          return new Double(result);
72      }
73  
74      public String getOperation()
75      {
76          return operation;
77      }
78  
79      public void setOperation(String operation)
80      {
81          this.operation = operation;
82      }
83  
84      public double getFactor()
85      {
86          return factor;
87      }
88  
89      public void setFactor(double factor)
90      {
91          this.factor = factor;
92      }
93  }