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       * Concatenate array of objects into a string in accordance with
20       * JLS $15.18.1 (except that primitive values are not accepted
21       * by this method other than by autoboxing).
22       *
23       * @param elements elements to convert
24       * @return string resulting from concatenating <tt>elements</tt>
25       */
26      public static String asString(@Nullable Object... elements)
27      {
28          // don't rename to toString, its not usable for static imports
29          int length = elements.length;
30          if (length  == 0)
31          {
32              return "";
33          }
34          if (length == 1)
35          {
36              singleAsString(elements[0]);
37          }
38          StringBuilder answer = new StringBuilder(length * EXPECTED_ELEMENT_LENGTH);
39          for (Object elem : elements)
40          {
41              answer.append(singleAsString(elem));
42          }
43          return answer.toString();
44      }
45  
46      private static String singleAsString(Object obj)
47      {
48          return obj != null ? obj.toString() : "null";
49      }
50  }