1
2 package eu.simuline.util;
3
4 import java.lang.ref.WeakReference;
5
6 /**
7 * Collection of static methods related to strings.
8 * This class is required because class {@link String} is declared final.
9 */
10 public final class Strings {
11
12 /* -------------------------------------------------------------------- *
13 * fields. *
14 * -------------------------------------------------------------------- */
15
16 /**
17 * An ever growing buffer of blanks used by {@link #getBlanks(int)}.
18 */
19 private static WeakReference<StringBuilder> bLANKS =
20 new WeakReference<StringBuilder>(new StringBuilder());
21
22
23 /* -------------------------------------------------------------------- *
24 * Constructors. *
25 * -------------------------------------------------------------------- */
26
27 /**
28 * Prevents <code>Strings</code> from being instantiated.
29 */
30 private Strings() {}
31
32 /* -------------------------------------------------------------------- *
33 * methods. *
34 * -------------------------------------------------------------------- */
35
36 /**
37 * Returns a string consisting of the given number of blanks.
38 */
39 public static String getBlanks(int num) {
40 StringBuilder blanks = bLANKS.get();
41 if (blanks == null) {
42 blanks = new StringBuilder();
43 bLANKS = new WeakReference<StringBuilder>(blanks);
44 }
45
46 while (blanks.length() < num) {
47 blanks.append(' ');
48 }
49 assert blanks.length() >= num;
50
51 return blanks.substring(0, num);
52 }
53 }