Coverage Report - org.mule.example.scripting.AccumulatorComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
AccumulatorComponent
45%
9/20
20%
2/10
3.333
 
 1  
 /*
 2  
  * $Id: AccumulatorComponent.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.MuleEventContext;
 15  
 import org.mule.api.lifecycle.Callable;
 16  
 import org.mule.api.transformer.TransformerException;
 17  
 import org.mule.config.i18n.MessageFactory;
 18  
 import org.mule.util.NumberUtils;
 19  
 
 20  
 /**
 21  
  * Component which accumulates successive numbers, by adding/subtracting/multiplying/dividing.  
 22  
  */
 23  16
 public class AccumulatorComponent implements Callable
 24  
 {
 25  
     /** Operation to perform: "add", "subtract", "multiply", "divide" */
 26  16
     private String operation = "add";
 27  
     
 28  16
     private double accumulatedValue = 0;
 29  
     
 30  
     public Object onCall(MuleEventContext context) throws Exception
 31  
     {
 32  32
         Object msg = context.transformMessage();
 33  
 
 34  32
         double data = NumberUtils.toDouble(msg);
 35  32
         if (data == NumberUtils.DOUBLE_ERROR)
 36  
         {
 37  0
             throw new TransformerException(MessageFactory.createStaticMessage("Unable to convert message to double: " + msg));
 38  
         }
 39  
         
 40  32
         if (operation.equalsIgnoreCase("add"))
 41  
         {
 42  32
             accumulatedValue += data;
 43  
         }
 44  0
         else if (operation.equalsIgnoreCase("subtract"))
 45  
         {
 46  0
             accumulatedValue -= data;
 47  
         }
 48  0
         else if (operation.equalsIgnoreCase("multiply"))
 49  
         {
 50  0
             accumulatedValue *= data;
 51  
         }
 52  0
         else if (operation.equalsIgnoreCase("divide"))
 53  
         {
 54  0
             accumulatedValue /= data;
 55  
         }
 56  
         else 
 57  
         {
 58  0
             throw new TransformerException(MessageFactory.createStaticMessage("Operation " + operation + " not recognized"));
 59  
         }
 60  
 
 61  
         // no auto-boxing
 62  32
         return new Double(accumulatedValue);
 63  
     }
 64  
 
 65  
     public String getOperation()
 66  
     {
 67  0
         return operation;
 68  
     }
 69  
 
 70  
     public void setOperation(String operation)
 71  
     {
 72  0
         this.operation = operation;
 73  0
     }
 74  
 }