📄 currencyconverter.java
字号:
/*
Copyright (C) 2003 Together
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.webdocwf.util.loader.transformation;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
/**
* This class implements Transformer interface. It can be used to convert
* currency from one to another type
* Date: 23.07.2004.
* @version 1.0
* @author Zeljko Kovacevic
*
*/
public class CurrencyConverter implements Transformer {
private String filePath = "";
private List retValue = new Vector();
private String multiplyValue = "";
private Properties properties = new Properties();
private String multiplyAll = "";
private String scale = "";
/**
* This method reads path to property file where currency is placed.
* @param filePath Path to property file
*/
public void configure(String filePath) throws Exception {
try {
this.filePath = filePath;
File propertyFile = new File(this.filePath);
this.properties.load(new FileInputStream(propertyFile));
this.multiplyValue = this.properties.getProperty("multiplyValue");
this.multiplyAll = this.properties.getProperty("multiplyAll");
this.scale = this.properties.getProperty("scale");
} catch (FileNotFoundException e) {
throw new Exception("File not found!");
} catch (IOException e) {
throw new Exception("Error while reading property file!");
}
}
/**
* This method release resources
*/
public void release() throws Exception {
}
/**
* This method will multiply currency from source for given value from
* property file
* @param valueToTransform List for transformation
* @return List with transformed values
*/
public List transformValue(List valueToTransform) throws Exception,ArithmeticException {
retValue.clear();
String oldValue = valueToTransform.get(0).toString();
BigDecimal bdMultiplyValue;
BigDecimal finalValue;
String key = "";
try {
if (this.multiplyAll.equalsIgnoreCase("true")) {
bdMultiplyValue = new BigDecimal(this.multiplyValue);
BigDecimal bigdec = new BigDecimal(oldValue);
finalValue = bigdec.multiply(bdMultiplyValue);
String multyValue ="";
try {
if (this.scale != ""){
multyValue = finalValue.setScale(new Integer(this.scale).intValue()).toString();
}else{
multyValue = finalValue.setScale(2).toString();
}
} catch (ArithmeticException e) {
throw new Exception("Error. Check values in property file.Probably, value for scale is too small, because of multiplyValue!");
}
retValue.add(multyValue);
} else {
boolean containsKey = this.properties.containsKey(oldValue);
if (containsKey == true) {
String specificMultiplyValue = this.properties.getProperty(oldValue);
BigDecimal bigdec = new BigDecimal(oldValue);
bdMultiplyValue = new BigDecimal(specificMultiplyValue);
finalValue = bigdec.multiply(bdMultiplyValue);
String multyValue;
if (this.scale != ""){
multyValue = finalValue.setScale(new Integer(this.scale).intValue()).toString();
}else{
multyValue = finalValue.setScale(2).toString();
}
retValue.add(multyValue);
} else {
retValue.add(oldValue);
}
}
} catch (Exception e) {
throw new Exception("Error while converting currency using class CurrencyConverter",e);
}
return retValue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -