1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\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.strings;
\r
14 import java.text.DecimalFormat;
\r
15 import java.text.NumberFormat;
\r
16 import java.text.ParseException;
\r
17 import java.util.Locale;
\r
19 public class StringUtils {
\r
21 private static NumberFormat usFormat = DecimalFormat.getInstance(Locale.US);
\r
22 public final static String ZERO_LENGTH_STRING = ""; //$NON-NLS-1$
\r
25 * Ensures that a string is not null. Converts null strings into empty
\r
26 * strings, and leaves any other string unmodified. Use this to help
\r
27 * wrap calls to methods that return null instead of the empty string.
\r
28 * Can also help protect against implementation errors in methods that
\r
29 * are not supposed to return null.
\r
31 * @param input input string (may be null)
\r
32 * @return input if not null, or the empty string if input is null
\r
34 public static String safeString(String input) {
\r
35 if (input != null) {
\r
39 return ZERO_LENGTH_STRING;
\r
43 public static double safeDouble(String input) {
\r
45 if(input == null) return Double.NaN;
\r
49 String usafied = input.replace(",", ".");
\r
50 Number number = usFormat.parse(usafied);
\r
51 return number.doubleValue();
\r
53 } catch (ParseException e) {
\r
61 public static String repeat(String pattern, int repeatCount)
\r
63 StringBuilder sb = new StringBuilder(pattern.length() * repeatCount);
\r
64 for (int i=0; i<repeatCount; i++)
\r
66 return sb.toString();
\r