]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/Units.java
(refs #7771) Fixed invalid inline
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / Units.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
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
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard;
13
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;
19
20 public class Units {
21
22         public static UnitLibrary library = UnitLibrary.createDefault();
23         
24         /**
25          * Create converter
26          * 
27          * @param sourceUnit
28          * @param targetUnit
29          * @return the converter
30          * @throws UnitParseException
31          */
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();
39         }       
40         
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);
45         }
46
47         
48 }