]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/format/SwitchFormat.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / format / SwitchFormat.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.format;\r
13 \r
14 import java.text.FieldPosition;\r
15 import java.text.NumberFormat;\r
16 import java.text.ParsePosition;\r
17 \r
18 public class SwitchFormat extends NumberFormat {\r
19 \r
20         private static final long serialVersionUID = -205210065554444251L;\r
21 \r
22         double low;\r
23         double high;\r
24         NumberFormat lowFormat;\r
25         NumberFormat middleFormat;\r
26         NumberFormat highFormat;\r
27         \r
28         public SwitchFormat(double low, double high, NumberFormat lowFormat, NumberFormat middleFormat, NumberFormat highFormat)\r
29         {\r
30                 this.low = low;\r
31                 this.high = high;\r
32                 this.lowFormat = lowFormat;\r
33                 this.middleFormat = middleFormat;\r
34                 this.highFormat = highFormat;\r
35         }\r
36         \r
37         @Override\r
38         public StringBuffer format(double number, StringBuffer toAppendTo,\r
39                         FieldPosition pos) {\r
40                 \r
41                 NumberFormat f;\r
42                 double x = Math.abs(number);\r
43                 if (x==0.0) f = middleFormat; else\r
44                 if (x<=low) f = lowFormat; else\r
45                 if (x>=high) f = highFormat; else f = middleFormat;\r
46                 \r
47                 return f.format(number, toAppendTo, pos);\r
48         }\r
49 \r
50         @Override\r
51         public StringBuffer format(long number, StringBuffer toAppendTo,\r
52                         FieldPosition pos) {\r
53                 NumberFormat f;\r
54                 double x = Math.abs(number);\r
55                 if (x==0.0) f = middleFormat; else\r
56                 if (x<=low) f = lowFormat; else\r
57                 if (x>=high) f = highFormat; else f = middleFormat;\r
58                 \r
59                 return f.format(number, toAppendTo, pos);\r
60         }\r
61 \r
62         @Override\r
63         public Number parse(String source, ParsePosition parsePosition) {\r
64                 return 0;\r
65         }\r
66 \r
67 }\r