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