1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.format;
\r
14 import java.text.FieldPosition;
\r
15 import java.text.NumberFormat;
\r
16 import java.text.ParsePosition;
\r
18 public class SwitchFormat extends NumberFormat {
\r
20 private static final long serialVersionUID = -205210065554444251L;
\r
24 NumberFormat lowFormat;
\r
25 NumberFormat middleFormat;
\r
26 NumberFormat highFormat;
\r
28 public SwitchFormat(double low, double high, NumberFormat lowFormat, NumberFormat middleFormat, NumberFormat highFormat)
\r
32 this.lowFormat = lowFormat;
\r
33 this.middleFormat = middleFormat;
\r
34 this.highFormat = highFormat;
\r
38 public StringBuffer format(double number, StringBuffer toAppendTo,
\r
39 FieldPosition pos) {
\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
47 return f.format(number, toAppendTo, pos);
\r
51 public StringBuffer format(long number, StringBuffer toAppendTo,
\r
52 FieldPosition pos) {
\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
59 return f.format(number, toAppendTo, pos);
\r
63 public Number parse(String source, ParsePosition parsePosition) {
\r