View Javadoc

1   package com.atlassian.pageobjects.elements.util;
2   
3   import javax.annotation.Nullable;
4   
5   /**
6    * Utility for string concatenation.
7    *
8    */
9   public final class StringConcat
10  {
11      public static final int EXPECTED_ELEMENT_LENGTH = 8;
12  
13      private StringConcat()
14      {
15          throw new AssertionError("Don't instantiate me");
16      }
17  
18      /**
19       * <p/>
20       * Concatenate array of objects into a string in accordance with
21       * JLS $15.18.1 (except that primitive values are not accepted
22       * by this method other than by autoboxing).
23       *
24       * <p/>
25       * A <code>null</code> passed as the whole <tt>elements</tt> array will
26       * result in empty string being returned.
27       *
28       * @param elements elements to convert
29       * @return string resulting from concatenating <tt>elements</tt>
30       */
31      public static String asString(@Nullable Object... elements)
32      {
33          // NOTE: don't rename to toString, its not usable for static imports
34          if (elements == null) {
35              return "";
36          }
37          int length = elements.length;
38          if (length  == 0)
39          {
40              return "";
41          }
42          if (length == 1)
43          {
44              singleAsString(elements[0]);
45          }
46          StringBuilder answer = new StringBuilder(length * EXPECTED_ELEMENT_LENGTH);
47          for (Object elem : elements)
48          {
49              answer.append(singleAsString(elem));
50          }
51          return answer.toString();
52      }
53  
54      private static String singleAsString(Object obj)
55      {
56          return obj != null ? obj.toString() : "null";
57      }
58  }