⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplemathtransformer.java

📁 提供ESB 应用mule源代码 提供ESB 应用mule源代码
💻 JAVA
字号:
/* * $Id: SimpleMathTransformer.java 11968 2008-06-06 04:06:18Z dfeist $ * -------------------------------------------------------------------------------------- * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com * * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */package org.mule.example.scripting;import org.mule.api.transformer.TransformerException;import org.mule.config.i18n.MessageFactory;import org.mule.transformer.AbstractTransformer;import org.mule.util.NumberUtils;/**  * A simple transformer which adds/subtracts/multiplies/divides a constant factor to numeric messages.  */public class SimpleMathTransformer extends AbstractTransformer {    /** Operation to perform: "add", "subtract", "multiply", "divide" */    private String operation = "add";        /** Factor to be applied */    private double factor;        public SimpleMathTransformer()    {        registerSourceType(Number.class);        setReturnClass(Number.class);    }    public Object doTransform(Object src, String encoding) throws TransformerException    {                 double data = NumberUtils.toDouble(src);        if (data == NumberUtils.DOUBLE_ERROR)        {            throw new TransformerException(MessageFactory.createStaticMessage("Unable to convert message to double: " + src));        }                double result;        if (operation.equalsIgnoreCase("add"))        {            result = data + factor;        }        else if (operation.equalsIgnoreCase("subtract"))        {            result = data - factor;        }        else if (operation.equalsIgnoreCase("multiply"))        {            result = data * factor;        }        else if (operation.equalsIgnoreCase("divide"))        {            result = data / factor;        }        else         {            throw new TransformerException(MessageFactory.createStaticMessage("Operation " + operation + " not recognized"));        }                // no auto-boxing        return new Double(result);    }    public String getOperation()    {        return operation;    }    public void setOperation(String operation)    {        this.operation = operation;    }    public double getFactor()    {        return factor;    }    public void setFactor(double factor)    {        this.factor = factor;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -