]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/units/internal/deprecated/Unit.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / units / internal / deprecated / Unit.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.units.internal.deprecated;
13
14 import java.util.ArrayList;
15
16 import org.simantics.databoard.units.internal.UnitParseException;
17 import org.simantics.databoard.units.internal.parser.UnitParser;
18
19 public class Unit {
20
21         UnitPart[] parts;
22         
23         public Unit(UnitPart ... parts) {
24                 this.parts = parts;
25         }
26
27         @Override
28         public String toString() {
29                 StringBuilder b = new StringBuilder();
30                 toString(b);
31                 return b.toString();
32         }
33         
34         public void toString(StringBuilder b) {
35                 if(parts.length == 0)
36                         b.append('1');
37                 else
38                         for(int i=0;i<parts.length;++i) {
39                                 if(i>0)
40                                         b.append(".");
41                                 parts[i].toString(b);
42                         }       
43         }
44         
45         public static Unit parse(String string) throws UnitParseException {
46                 final ArrayList<UnitPart> parts = new ArrayList<UnitPart>();
47                 UnitParser parser = new UnitParser() {
48
49                         @Override
50                         public void visit(String baseUnit, int exponent) {
51                                 parts.add(new UnitPart(baseUnit, Magnitude.unscaled, exponent));
52                         }
53                         
54                 };
55                 parser.unit(string, 1);
56                 return new Unit(parts.toArray(new UnitPart[parts.size()]));
57         }
58         
59         public static void main(String[] args) {
60                 try {
61                         System.out.println(parse("m2/(s3/s)"));
62                 } catch (UnitParseException e) {
63                         // TODO Auto-generated catch block
64                         e.printStackTrace();
65                 }
66         }
67                 
68 }