]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/format/SwitchFormat.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / format / SwitchFormat.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 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.utils.format;
13
14 import java.text.FieldPosition;
15 import java.text.NumberFormat;
16 import java.text.ParsePosition;
17
18 public class SwitchFormat extends NumberFormat {
19
20         private static final long serialVersionUID = -205210065554444251L;
21
22         double low;
23         double high;
24         NumberFormat lowFormat;
25         NumberFormat middleFormat;
26         NumberFormat highFormat;
27         
28         public SwitchFormat(double low, double high, NumberFormat lowFormat, NumberFormat middleFormat, NumberFormat highFormat)
29         {
30                 this.low = low;
31                 this.high = high;
32                 this.lowFormat = lowFormat;
33                 this.middleFormat = middleFormat;
34                 this.highFormat = highFormat;
35         }
36         
37         @Override
38         public StringBuffer format(double number, StringBuffer toAppendTo,
39                         FieldPosition pos) {
40                 
41                 NumberFormat f;
42                 double x = Math.abs(number);
43                 if (x==0.0) f = middleFormat; else
44                 if (x<=low) f = lowFormat; else
45                 if (x>=high) f = highFormat; else f = middleFormat;
46                 
47                 return f.format(number, toAppendTo, pos);
48         }
49
50         @Override
51         public StringBuffer format(long number, StringBuffer toAppendTo,
52                         FieldPosition pos) {
53                 NumberFormat f;
54                 double x = Math.abs(number);
55                 if (x==0.0) f = middleFormat; else
56                 if (x<=low) f = lowFormat; else
57                 if (x>=high) f = highFormat; else f = middleFormat;
58                 
59                 return f.format(number, toAppendTo, pos);
60         }
61
62         @Override
63         public Number parse(String source, ParsePosition parsePosition) {
64                 return 0;
65         }
66
67 }