File: StringUtils.java

package info (click to toggle)
libbase 1.1.6-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 944 kB
  • sloc: java: 8,709; xml: 1,522; makefile: 18
file content (392 lines) | stat: -rw-r--r-- 12,338 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright (c) 2007 - 2009 Pentaho Corporation and Contributors.  All rights reserved.
 */

package org.pentaho.reporting.libraries.base.util;

import java.text.Format;
import java.text.MessageFormat;
import java.util.HashSet;
import java.util.StringTokenizer;

/**
 * String utilities.
 *
 * @author Thomas Morgner.
 */
public class StringUtils
{
  // Constants used to convert a string to a boolean
  private static final String TRUE = "true";
  private static final String YES = "yes";
  private static final String ON = "on";

  /**
   * Private constructor prevents object creation.
   */
  private StringUtils()
  {
  }

  /**
   * Helper functions to query a strings start portion. The comparison is case insensitive.
   *
   * @param base  the base string.
   * @param start the starting text.
   * @return true, if the string starts with the given starting text.
   */
  public static boolean startsWithIgnoreCase(final String base, final String start)
  {
    if (base.length() < start.length())
    {
      return false;
    }
    return base.regionMatches(true, 0, start, 0, start.length());
  }

  /**
   * Helper functions to query a strings end portion. The comparison is case insensitive.
   *
   * @param base the base string.
   * @param end  the ending text.
   * @return true, if the string ends with the given ending text.
   */
  public static boolean endsWithIgnoreCase(final String base, final String end)
  {
    if (base.length() < end.length())
    {
      return false;
    }
    return base.regionMatches(true, base.length() - end.length(), end, 0, end.length());
  }

  /**
   * Queries the system properties for the line separator. If access
   * to the System properties is forbidden, the UNIX default is returned.
   *
   * @return the line separator.
   * @noinspection AccessOfSystemProperties
   */
  public static String getLineSeparator()
  {
    try
    {
      return System.getProperty("line.separator", "\n");
    }
    catch (Exception e)
    {
      return "\n";
    }
  }

  /**
   * Splits a given string on any whitespace character. Duplicate separators will be merged into a single separator
   * occurance. This implementation provides the same functionality as the REGEXP-based String.split(..) operation but
   * does not use Regular expressions and therefore it is faster and less memory consuming.
   *
   * @param string the text to be split.
   * @return the text elements as array.
   */
  public static String[] split(final String string)
  {
    return split(string, " \t\n\r\f");
  }

  /**
   * Splits a given string at the given separator string. Duplicate separators will be merged into a single separator
   * occurance.
   *
   * @param string    the text to be split.
   * @param separator the separator chacters used for the split.
   * @return the splitted array.
   */
  public static String[] split(final String string, final String separator)
  {
    if (separator == null)
    {
      throw new NullPointerException("Separator characters must not be null.");
    }
    if (string == null)
    {
      throw new NullPointerException("String to be split must not be null.");
    }

    final StringTokenizer strtok = new StringTokenizer(string, separator, false);
    final String[] tokens = new String[strtok.countTokens()];
    int i = 0;
    while (strtok.hasMoreTokens())
    {
      final String token = strtok.nextToken();
      tokens[i] = (token);
      i += 1;
    }
    return tokens;
  }

  /**
   * Splits a given string at the given separator string. Duplicate separators will be merged into a single separator
   * occurance.
   *
   * @param string    the text to be split.
   * @param separator the separator chacters used for the split.
   * @param quate     the quoting character.
   * @return the splitted array.
   */
  public static String[] split(final String string, final String separator, final String quate)
  {
    final CSVTokenizer strtok = new CSVTokenizer(string, separator, quate, false);
    final String[] tokens = new String[strtok.countTokens()];
    int i = 0;
    while (strtok.hasMoreTokens())
    {
      final String token = strtok.nextToken();
      if (token.length() > 0)
      {
        tokens[i] = (token);
        i += 1;
      }
    }
    if (i == tokens.length)
    {
      return tokens;
    }

    final String[] retval = new String[i];
    System.arraycopy(tokens, 0, retval, 0, i);
    return retval;
  }

  /**
   * Splits a given string at the given separator string. Duplicate separators will result in empty strings thus
   * preserving the number of fields specified in the original string.
   *
   * @param string    the text to be split.
   * @param separator the separator chacters used for the split.
   * @return the splitted array.
   */
  public static String[] splitCSV(final String string, final String separator)
  {
    return splitCSV(string, separator, null);
  }

  /**
   * Splits a given string at the given separator string. Duplicate separators will result in empty strings thus
   * preserving the number of fields specified in the original string.
   *
   * @param string    the text to be split.
   * @param separator the separator chacters used for the split.
   * @param quate     the quoting character.
   * @return the splitted array.
   */
  public static String[] splitCSV(final String string, final String separator, final String quate)
  {
    final CSVTokenizer strtok = new CSVTokenizer(string, separator, quate, false);
    final String[] tokens = new String[strtok.countTokens()];
    int i = 0;
    while (strtok.hasMoreTokens())
    {
      final String token = strtok.nextToken();
      tokens[i] = (token);
      i += 1;
    }
    return tokens;
  }

  /**
   * Computes a unique name using the given known-names array as filter. This method is not intended for large
   * datasets.
   *
   * @param knownNames the list of known names.
   * @param pattern the name pattern, which should have one integer slot to create derived names.
   * @return the unique name or null, if no unqiue name could be created.
   */
  public static String makeUniqueName(final String[] knownNames, final String pattern)
  {
    final HashSet knownNamesSet = new HashSet(knownNames.length);
    for (int i = 0; i < knownNames.length; i++)
    {
      final String name = knownNames[i];
      knownNamesSet.add(name);
    }

    final MessageFormat message = new MessageFormat(pattern);
    final Object[] objects = {""};
    final String plain = message.format(objects);
    if (knownNamesSet.contains(plain) == false)
    {
      return plain;
    }

    final Format[] formats = message.getFormats();
    if (formats.length == 0)
    {
      // there is no variation in this name.
      return null;
    }

    int count = 1;
    while (count < 2000000)
    {
      objects[0] = String.valueOf(count);
      final String testFile = message.format(objects);
      if (knownNamesSet.contains(testFile) == false)
      {
        return testFile;
      }
      count += 1;
    }

    return null;
  }

  /**
   * Merges the contents of the first and second array returning a array that contains only unique strings.
   * The order of the returned array is undefined.
   *
   * @param first  the first array to be merged.
   * @param second the second array to be merged.
   * @return the merged araray.
   */
  public static String[] merge(final String[] first, final String[] second)
  {
    if (first.length == 0)
    {
      return (String[]) second.clone();
    }
    if (second.length == 0)
    {
      return (String[]) first.clone();
    }
    final HashSet total = new HashSet(first.length + second.length);
    for (int i = 0; i < first.length; i++)
    {
      total.add(first[i]);
    }
    for (int i = 0; i < second.length; i++)
    {
      total.add(second[i]);
    }
    return (String[]) total.toArray(new String[total.size()]);
  }

  /**
   * Returns <code>true</code> if the source string evaulates (case insensative and trimmed) to <code>true</code>,
   * <code>yes</code>, or <code>on</code>. It will return <code>false</code> otherwise (including <code>null</code>).
   *
   * @param source the string to check
   * @return <code>true</code> if the source string evaulates to <code>true</code> or similar value,
   *         <code>false</code> otherwise.
   */
  public static boolean toBoolean(final String source)
  {
    return toBoolean(source, false);
  }

  /**
   * Returns <code>true</code> if the source string evaulates (case insensative and trimmed) to <code>true</code>,
   * <code>yes</code>, or <code>on</code>. It will return <code>false otherwise. If the source string
   * is <code>null</code>, it will return the value of the default.
   *
   * @param source      the string to check
   * @param nullDefault to value to return if the source string is <code>null</code>
   * @return <code>true</code> if the source string evaulates to <code>true</code> or similar value,
   *         <code>false</code> otherwise.
   */
  public static boolean toBoolean(String source, final boolean nullDefault)
  {
    // If the source is null, use the default
    if (source == null)
    {
      return nullDefault;
    }

    // Check for valid values
    source = source.trim().toLowerCase();
    return (TRUE.equals(source) || YES.equals(source) || ON.equals(source));
  }

  /**
   * Determines if the string is empty or <code>null</code>.
   *
   * @param source the string to check
   * @return <code>true</code> if the source string is <code>null</code> or an emptry string,
   *         <code>false</code> otherwise.
   */
  public static boolean isEmpty(final String source)
  {
    return isEmpty(source, true);
  }


  /**
   * Determines if the string is empty or <code>null</code>. If the </code>trim</code> is <code>true</code>,
   * the string will be trimmed before checking for an empty string.
   *
   * @param source the string to check
   * @param trim   indicates if the string should be trimmed before checking for an empty string.
   * @return <code>true</code> if the source string is <code>null</code> or an emptry string,
   *         <code>false</code> otherwise.
   */
  public static boolean isEmpty(final String source, final boolean trim)
  {
    if (source == null)
    {
      return true;
    }
    if (source.length() == 0)
    {
      return true;
    }
    if (trim == false)
    {
      return false;
    }
    final char[] chars = source.toCharArray();
    for (int i = 0; i < chars.length; i++)
    {
      final char c = chars[i];
      if (c > ' ')
      {
        return false;
      }
    }
    return true;
  }

  /**
   * Determines if the two Strings are equals (taking nulls into account).
   * 
   * @param s1 the first string to compare.
   * @param s2 the second string to compare.
   * @return <code>true</code> if both string are null or the contain the same value, <code>false</code>otherwise
   */
  public static boolean equals(final String s1, final String s2)
  {
    return ((s1 == null && s2 == null) || (s1 != null && s1.equals(s2)));
  }

  /**
   * Determines if the two Strings are equals ingnoring case sensitivity (taking nulls into account).
   *
   * @param s1 the first string to compare.
   * @param s2 the second string to compare.
   * @return <code>true</code> if both string are null or the contain the same case-insensitive value, <code>false</code>otherwise
   */
  public static boolean equalsIgnoreCase(final String s1, final String s2)
  {
    return ((s1 == null && s2 == null) || (s1 != null && s1.equalsIgnoreCase(s2)));
  }
}