Coverage Report - org.mule.example.scripting.AccumulatorComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
AccumulatorComponent
0%
0/20
0%
0/10
3.333
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.example.scripting;
 8  
 
 9  
 
 10  
 import org.mule.api.MuleEventContext;
 11  
 import org.mule.api.lifecycle.Callable;
 12  
 import org.mule.api.transformer.TransformerException;
 13  
 import org.mule.config.i18n.MessageFactory;
 14  
 import org.mule.util.NumberUtils;
 15  
 
 16  
 /**
 17  
  * Component which accumulates successive numbers, by adding/subtracting/multiplying/dividing.  
 18  
  */
 19  0
 public class AccumulatorComponent implements Callable
 20  
 {
 21  
     /** Operation to perform: "add", "subtract", "multiply", "divide" */
 22  0
     private String operation = "add";
 23  
     
 24  0
     private double accumulatedValue = 0;
 25  
     
 26  
     public Object onCall(MuleEventContext context) throws Exception
 27  
     {
 28  0
         Object msg = context.getMessage().getPayload();
 29  
 
 30  0
         double data = NumberUtils.toDouble(msg);
 31  0
         if (data == NumberUtils.DOUBLE_ERROR)
 32  
         {
 33  0
             throw new TransformerException(MessageFactory.createStaticMessage("Unable to convert message to double: " + msg));
 34  
         }
 35  
         
 36  0
         if (operation.equalsIgnoreCase("add"))
 37  
         {
 38  0
             accumulatedValue += data;
 39  
         }
 40  0
         else if (operation.equalsIgnoreCase("subtract"))
 41  
         {
 42  0
             accumulatedValue -= data;
 43  
         }
 44  0
         else if (operation.equalsIgnoreCase("multiply"))
 45  
         {
 46  0
             accumulatedValue *= data;
 47  
         }
 48  0
         else if (operation.equalsIgnoreCase("divide"))
 49  
         {
 50  0
             accumulatedValue /= data;
 51  
         }
 52  
         else 
 53  
         {
 54  0
             throw new TransformerException(MessageFactory.createStaticMessage("Operation " + operation + " not recognized"));
 55  
         }
 56  
 
 57  
         // no auto-boxing
 58  0
         return new Double(accumulatedValue);
 59  
     }
 60  
 
 61  
     public String getOperation()
 62  
     {
 63  0
         return operation;
 64  
     }
 65  
 
 66  
     public void setOperation(String operation)
 67  
     {
 68  0
         this.operation = operation;
 69  0
     }
 70  
 }