1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard;
14 import org.simantics.databoard.units.ConverterCreator;
15 import org.simantics.databoard.units.IUnitConverter;
16 import org.simantics.databoard.units.IdentityConverter;
17 import org.simantics.databoard.units.internal.UnitParseException;
18 import org.simantics.databoard.units.internal.library.UnitLibrary;
22 public static UnitLibrary library = UnitLibrary.createDefault();
29 * @return the converter
30 * @throws UnitParseException
32 public static IUnitConverter createConverter(String sourceUnit, String targetUnit) throws UnitParseException {
33 if(sourceUnit.equals(targetUnit))
34 return IdentityConverter.INSTANCE;
35 ConverterCreator creator = new ConverterCreator(library);
36 creator.unit(sourceUnit, 1);
37 creator.unit(targetUnit, -1);
38 return creator.createConverter();
41 public static double convert(double value, String sourceUnit, String targetUnit) throws UnitParseException {
42 IUnitConverter c = createConverter(sourceUnit, targetUnit);
43 if (c==null) throw new UnitParseException(sourceUnit+" or "+targetUnit, "could not convert");
44 return c.convert(value);