]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/format/SwitchFormat.java b/bundles/org.simantics.utils/src/org/simantics/utils/format/SwitchFormat.java
new file mode 100644 (file)
index 0000000..28e28e6
--- /dev/null
@@ -0,0 +1,67 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.format;\r
+\r
+import java.text.FieldPosition;\r
+import java.text.NumberFormat;\r
+import java.text.ParsePosition;\r
+\r
+public class SwitchFormat extends NumberFormat {\r
+\r
+       private static final long serialVersionUID = -205210065554444251L;\r
+\r
+       double low;\r
+       double high;\r
+       NumberFormat lowFormat;\r
+       NumberFormat middleFormat;\r
+       NumberFormat highFormat;\r
+       \r
+       public SwitchFormat(double low, double high, NumberFormat lowFormat, NumberFormat middleFormat, NumberFormat highFormat)\r
+       {\r
+               this.low = low;\r
+               this.high = high;\r
+               this.lowFormat = lowFormat;\r
+               this.middleFormat = middleFormat;\r
+               this.highFormat = highFormat;\r
+       }\r
+       \r
+       @Override\r
+       public StringBuffer format(double number, StringBuffer toAppendTo,\r
+                       FieldPosition pos) {\r
+               \r
+               NumberFormat f;\r
+               double x = Math.abs(number);\r
+               if (x==0.0) f = middleFormat; else\r
+               if (x<=low) f = lowFormat; else\r
+               if (x>=high) f = highFormat; else f = middleFormat;\r
+               \r
+               return f.format(number, toAppendTo, pos);\r
+       }\r
+\r
+       @Override\r
+       public StringBuffer format(long number, StringBuffer toAppendTo,\r
+                       FieldPosition pos) {\r
+               NumberFormat f;\r
+               double x = Math.abs(number);\r
+               if (x==0.0) f = middleFormat; else\r
+               if (x<=low) f = lowFormat; else\r
+               if (x>=high) f = highFormat; else f = middleFormat;\r
+               \r
+               return f.format(number, toAppendTo, pos);\r
+       }\r
+\r
+       @Override\r
+       public Number parse(String source, ParsePosition parsePosition) {\r
+               return 0;\r
+       }\r
+\r
+}\r