|
From: Hirzel P. <ph...@us...> - 2006-05-08 17:59:25
|
Update of /cvsroot/tcotool/TCO-Tool/src/org/tcotool/tools In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4183/src/org/tcotool/tools Added Files: CourseUtils.java Log Message: New --- NEW FILE: CourseUtils.java --- package org.tcotool.tools; /* * 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 */ import java.util.ArrayList; import org.tcotool.application.LauncherView; import org.tcotool.model.Course; import org.tcotool.model.Currency; import ch.softenvironment.util.UserException; /** * @author Peter Hirzel <i>soft</i>Environment * @version $Revision: 1.1 $ $Date: 2006/05/07 14:55:32 $ */ public class CourseUtils { /** * Return the targetAmount, which is: * targetAmount = sourceAmount * factor * @see #getCourse(Currency, Currency) */ public static double getAmount(double sourceAmount, Currency source, Currency target) throws UserException { return sourceAmount * getCourse(source, target); } /** * Return a course between source- and target-Currency. * @param source * @param target * @return * @throws UserException */ public static double getCourse(Currency source, Currency target) throws UserException { //TODO NLS if ((source == null) || (target == null)) { throw new IllegalArgumentException("source AND target must not be null"); } if (source.equals(target)) { // no transformation at all return 1.0; } java.util.List list = null; try { list = LauncherView.getInstance().getUtility().getSystemParameter().getCourse(); } catch(Throwable e) { LauncherView.getInstance().handleException(e); list = new ArrayList(); } java.util.Iterator it = list.iterator(); while (it.hasNext()) { Course course = (Course)it.next(); if (source.equals(course.getSource()) && target.equals(course.getTarget())) { // straight return course.getFactor().doubleValue(); } if (source.equals(course.getTarget()) && target.equals(course.getSource())) { // 1/x return 1.0 / course.getFactor().doubleValue(); } } throw new UserException(CourseUtils.class, "getCourse()", "No course defined for <" + source.getNameString() + "> to <" + target.getNameString() + ">"); } } |