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       * </p>
24       *
25       * <p>
26       * A <code>null</code> passed as the whole <tt>elements</tt> array will
27       * result in empty string being returned.
28       * </p>
29       *
30       * @param elements elements to convert
31       * @return string resulting from concatenating <tt>elements</tt>
32       */
33      public static String asString(@Nullable Object... elements)
34      {
35          // NOTE: don't rename to toString, its not usable for static imports
36          if (elements == null) {
37              return "";
38          }
39          int length = elements.length;
40          if (length  == 0)
41          {
42              return "";
43          }
44          if (length == 1)
45          {
46              singleAsString(elements[0]);
47          }
48          StringBuilder answer = new StringBuilder(length * EXPECTED_ELEMENT_LENGTH);
49          for (Object elem : elements)
50          {
51              answer.append(singleAsString(elem));
52          }
53          return answer.toString();
54      }
55  
56      private static String singleAsString(Object obj)
57      {
58          return obj != null ? obj.toString() : "null";
59      }
60  }