📄 currencyconverter.java
字号:
/* * Copyright (c) 2004-2007 Marco Maccaferri and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Marco Maccaferri - initial API and implementation */package net.sourceforge.eclipsetrader.core;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.InputStreamReader;import java.text.NumberFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Comparator;import java.util.Currency;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Observable;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.UsernamePasswordCredentials;import org.apache.commons.httpclient.auth.AuthScope;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.eclipse.core.runtime.IProgressMonitor;import org.eclipse.core.runtime.IStatus;import org.eclipse.core.runtime.Platform;import org.eclipse.core.runtime.Status;import org.eclipse.jface.preference.IPreferenceStore;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;public class CurrencyConverter extends Observable{ private static CurrencyConverter instance = new CurrencyConverter(); List currencies = new ArrayList(); Map map = new HashMap(); Map historyMap = new HashMap(); NumberFormat numberFormat = NumberFormat.getInstance(Locale.US); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); //$NON-NLS-1$ Log logger = LogFactory.getLog(getClass()); private class History { Date date; Double ratio; public History() { } public History(Date date, Double ratio) { this.date = date; this.ratio = ratio; } public boolean equals(Object obj) { if (obj instanceof Date) return date.equals((Date)obj); if (obj instanceof History) return date.equals(((History)obj).date); return false; } } CurrencyConverter() { File file = new File(Platform.getLocation().toFile(), "currencies.xml"); //$NON-NLS-1$ if (file.exists() == true) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(file); Node firstNode = document.getFirstChild(); NodeList childNodes = firstNode.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node item = childNodes.item(i); String nodeName = item.getNodeName(); if (nodeName.equalsIgnoreCase("currency")) //$NON-NLS-1$ { Node valueNode = item.getFirstChild(); if (valueNode != null) currencies.add(valueNode.getNodeValue()); } else if (nodeName.equalsIgnoreCase("conversion")) //$NON-NLS-1$ { String symbol = ((Node)item).getAttributes().getNamedItem("symbol").getNodeValue(); //$NON-NLS-1$ Node valueNode = null; if (((Node)item).getAttributes().getNamedItem("ratio") != null) //$NON-NLS-1$ valueNode = ((Node)item).getAttributes().getNamedItem("ratio"); //$NON-NLS-1$ if (valueNode == null) valueNode = item.getFirstChild(); if (valueNode != null) { Double value = new Double(Double.parseDouble(valueNode.getNodeValue())); map.put(symbol, value); } readHistory(symbol, item.getChildNodes()); } } } catch (Exception e) { logger.error(e, e); } } } private void readHistory(String symbol, NodeList nodeList) { List list = new ArrayList(); for (int i = 0; i < nodeList.getLength(); i++) { Node item = nodeList.item(i); String nodeName = item.getNodeName(); if (nodeName.equalsIgnoreCase("history")) //$NON-NLS-1$ { History history = new History(); try { history.date = dateFormat.parse(item.getAttributes().getNamedItem("date").getNodeValue()); history.ratio = new Double(item.getAttributes().getNamedItem("ratio").getNodeValue()); } catch (Exception e) { logger.warn(e); } if (history.date != null && history.ratio != null && !list.contains(history)) list.add(history); } } historyMap.put(symbol, list); } public static CurrencyConverter getInstance() { return instance; } public void clear() { File file = new File(Platform.getLocation().toFile(), "currencies.xml"); //$NON-NLS-1$ if (file.exists() == true) file.delete(); currencies.clear(); map.clear(); historyMap.clear(); } public void dispose() { save(); } public List getCurrencies() { return currencies; } public void setCurrencies(List currencies) { this.currencies = currencies; } public double convert(double amount, Currency from, Currency to) { if (from == null || to == null || from.equals(to)) return amount; return convert(null, amount, from.getCurrencyCode(), to.getCurrencyCode()); } public double convert(Date date, double amount, Currency from, Currency to) { if (from == null || to == null || from.equals(to)) return amount; return convert(date, amount, from.getCurrencyCode(), to.getCurrencyCode()); } public double convert(Double amount, Currency from, Currency to) { if (from == null || to == null || from.equals(to)) return amount.doubleValue(); return convert(null, amount.doubleValue(), from.getCurrencyCode(), to.getCurrencyCode()); } public double convert(Date date, Double amount, Currency from, Currency to) { if (from == null || to == null || from.equals(to)) return amount.doubleValue(); return convert(date, amount.doubleValue(), from.getCurrencyCode(), to.getCurrencyCode()); } public double convert(double amount, String from, String to) { return convert(null, amount, from, to); } public double convert(Date date, double amount, String from, String to) { double result = amount; if (from == null || to == null || from.equals(to)) return result; Double ratio = null; if (date != null) { ratio = getExchangeRatio(date, from, to); if (ratio == null) { Double r = getExchangeRatio(date, to, from); if (r != null) ratio = new Double(1 / r.doubleValue()); } } if (ratio == null) ratio = getExchangeRatio(from, to); if (ratio != null) result = amount * ratio.doubleValue(); if (date != null && ratio != null) setExchangeRatio(date, from, to, ratio.doubleValue()); return result; } public IStatus updateExchanges(IProgressMonitor monitor) { if (monitor != null) monitor.beginTask("Updating currencies", currencies.size() + 1); map.clear(); Object[] symbols = currencies.toArray(); for (int x = 0; x < symbols.length; x++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -