Coverage Report - org.mule.example.scripting.SimpleMathTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleMathTransformer
0%
0/25
0%
0/10
0
 
 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  0
     private String operation = "add";
 28  
     
 29  
     /** Factor to be applied */
 30  
     private double factor;
 31  
     
 32  
     public SimpleMathTransformer()
 33  0
     {
 34  0
         DataType<Number> numberDataType = DataTypeFactory.create(Number.class);
 35  0
         registerSourceType(numberDataType);
 36  0
         setReturnDataType(numberDataType);
 37  0
     }
 38  
 
 39  
     @Override
 40  
     public Object doTransform(Object src, String outputEncoding) throws TransformerException
 41  
     {
 42  0
         double data = NumberUtils.toDouble(src);
 43  0
         if (data == NumberUtils.DOUBLE_ERROR)
 44  
         {
 45  0
             throw new TransformerException(MessageFactory.createStaticMessage("Unable to convert message to double: " + src));
 46  
         }
 47  
         
 48  
         double result;
 49  0
         if (operation.equalsIgnoreCase("add"))
 50  
         {
 51  0
             result = data + factor;
 52  
         }
 53  0
         else if (operation.equalsIgnoreCase("subtract"))
 54  
         {
 55  0
             result = data - factor;
 56  
         }
 57  0
         else if (operation.equalsIgnoreCase("multiply"))
 58  
         {
 59  0
             result = data * factor;
 60  
         }
 61  0
         else if (operation.equalsIgnoreCase("divide"))
 62  
         {
 63  0
             result = data / factor;
 64  
         }
 65  
         else
 66  
         {
 67  0
             throw new TransformerException(MessageFactory.createStaticMessage("Operation " + operation + " not recognized"));
 68  
         }
 69  
         
 70  
         // no auto-boxing
 71  0
         return new Double(result);
 72  
     }
 73  
 
 74  
     public String getOperation()
 75  
     {
 76  0
         return operation;
 77  
     }
 78  
 
 79  
     public void setOperation(String operation)
 80  
     {
 81  0
         this.operation = operation;
 82  0
     }
 83  
 
 84  
     public double getFactor()
 85  
     {
 86  0
         return factor;
 87  
     }
 88  
 
 89  
     public void setFactor(double factor)
 90  
     {
 91  0
         this.factor = factor;
 92  0
     }
 93  
 }