]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils/src/org/simantics/utils/strings/StringUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / strings / StringUtils.java
diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/strings/StringUtils.java b/bundles/org.simantics.utils/src/org/simantics/utils/strings/StringUtils.java
new file mode 100644 (file)
index 0000000..df77806
--- /dev/null
@@ -0,0 +1,69 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in 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.strings;\r
+\r
+import java.text.DecimalFormat;\r
+import java.text.NumberFormat;\r
+import java.text.ParseException;\r
+import java.util.Locale;\r
+\r
+public class StringUtils {\r
+\r
+    private static NumberFormat usFormat = DecimalFormat.getInstance(Locale.US);\r
+    public final static String ZERO_LENGTH_STRING = ""; //$NON-NLS-1$\r
+\r
+    /**\r
+     * Ensures that a string is not null. Converts null strings into empty\r
+     * strings, and leaves any other string unmodified. Use this to help\r
+     * wrap calls to methods that return null instead of the empty string.\r
+     * Can also help protect against implementation errors in methods that\r
+     * are not supposed to return null. \r
+     * \r
+     * @param input input string (may be null)\r
+     * @return input if not null, or the empty string if input is null\r
+     */\r
+    public static String safeString(String input) {\r
+        if (input != null) {\r
+            return input;\r
+        }\r
+\r
+        return ZERO_LENGTH_STRING;\r
+    }\r
+    \r
+\r
+    public static double safeDouble(String input) {\r
+\r
+        if(input == null) return Double.NaN;\r
+        \r
+        try {\r
+\r
+            String usafied = input.replace(",", ".");\r
+            Number number = usFormat.parse(usafied);\r
+            return number.doubleValue();\r
+            \r
+        } catch (ParseException e) {\r
+            \r
+            return Double.NaN;\r
+            \r
+        }\r
+        \r
+    }\r
+    \r
+    public static String repeat(String pattern, int repeatCount)\r
+    {\r
+        StringBuilder sb = new StringBuilder(pattern.length() * repeatCount);\r
+        for (int i=0; i<repeatCount; i++)\r
+            sb.append(pattern);\r
+        return sb.toString();\r
+    }\r
+    \r
+}\r