StringUtil.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.util;
  16. import javolution.text.TextBuilder;
  17. /**
  18. * String utilities optimized for the best performance.<br>
  19. * <h1>How to Use It</h1> <h2>concat() or append()</h2> If concatenating strings<br>
  20. * in single call, use StringUtil.concat(), otherwise use StringUtil.append()<br>
  21. * and its variants.<br>
  22. * <br>
  23. * <h2>Minimum Calls</h2><br>
  24. * Bad:
  25. *
  26. * <pre>
  27. * final StringBuilder sbString = new StringBuilder();
  28. * StringUtil.append(sbString, &quot;text 1&quot;, String.valueOf(npcId));
  29. * StringUtil.append(&quot;text 2&quot;);
  30. * </pre>
  31. *
  32. * Good:
  33. *
  34. * <pre>
  35. * final StringBuilder sbString = new StringBuilder();
  36. * StringUtil.append(sbString, &quot;text 1&quot;, String.valueOf(npcId), &quot;text 2&quot;);
  37. * </pre>
  38. *
  39. * Why?<br/>
  40. * Because the less calls you do, the less memory re-allocations have to be done<br>
  41. * so the whole text fits into the memory and less array copy tasks has to be<br>
  42. * performed. So if using less calls, less memory is used and string concatenation is faster.<br>
  43. * <br>
  44. * <h2>Size Hints for Loops</h2><br>
  45. * Bad:
  46. *
  47. * <pre>
  48. * final StringBuilder sbString = new StringBuilder();
  49. * StringUtil.append(sbString, &quot;header start&quot;, someText, &quot;header end&quot;);
  50. * for (int i = 0; i &lt; 50; i++)
  51. * {
  52. * StringUtil.append(sbString, &quot;text 1&quot;, stringArray[i], &quot;text 2&quot;);
  53. * }
  54. * </pre>
  55. *
  56. * Good:
  57. *
  58. * <pre>
  59. * final StringBuilder sbString = StringUtil.startAppend(1300, &quot;header start&quot;, someText, &quot;header end&quot;);
  60. * for (int i = 0; i &lt; 50; i++)
  61. * {
  62. * StringUtil.append(sbString, &quot;text 1&quot;, stringArray[i], &quot;text 2&quot;);
  63. * }
  64. * </pre>
  65. *
  66. * Why?<br/>
  67. * When using StringUtil.append(), memory is only allocated to fit in the strings in method argument. So on each loop new memory for the string has to be allocated and old string has to be copied to the new string. With size hint, even if the size hint is above the needed memory, memory is saved
  68. * because new memory has not to be allocated on each cycle. Also it is much faster if no string copy tasks has to be performed. So if concatenating strings in a loop, count approximately the size and set it as the hint for the string builder size. It's better to make the size hint little bit larger
  69. * rather than smaller.<br/>
  70. * In case there is no text appended before the cycle, just use <code>new
  71. * StringBuilder(1300)</code>.<br>
  72. * <br>
  73. * <h2>Concatenation and Constants</h2><br>
  74. * Bad:
  75. *
  76. * <pre>
  77. * StringUtil.concat(&quot;text 1 &quot;, &quot;text 2&quot;, String.valueOf(npcId));
  78. * </pre>
  79. *
  80. * Good:
  81. *
  82. * <pre>
  83. * StringUtil.concat(&quot;text 1 &quot; + &quot;text 2&quot;, String.valueOf(npcId));
  84. * </pre>
  85. *
  86. * or
  87. *
  88. * <pre>
  89. * StringUtil.concat(&quot;text 1 text 2&quot;, String.valueOf(npcId));
  90. * </pre>
  91. *
  92. * Why?<br/>
  93. * It saves some cycles when determining size of memory that needs to be allocated because less strings are passed to concat() method. But do not use + for concatenation of non-constant strings, that degrades performance and makes extra memory allocations needed.<br>
  94. * <h2>Concatenation and Constant Variables</h2> Bad:
  95. *
  96. * <pre>
  97. * String glue = &quot;some glue&quot;;
  98. * StringUtil.concat(&quot;text 1&quot;, glue, &quot;text 2&quot;, glue, String.valueOf(npcId));
  99. * </pre>
  100. *
  101. * Good:
  102. *
  103. * <pre>
  104. * final String glue = &quot;some glue&quot;;
  105. * StringUtil.concat(&quot;text 1&quot; + glue + &quot;text2&quot; + glue, String.valueOf(npcId));
  106. * </pre>
  107. *
  108. * Why? Because when using <code>final</code> keyword, the <code>glue</code> is marked as constant string and compiler treats it as a constant string so it is able to create string "text1some gluetext2some glue" during the compilation. But this only works in case the value is known at compilation
  109. * time, so this cannot be used for cases like <code>final String objectIdString =
  110. * String.valueOf(getObjectId)</code>.<br>
  111. * <br>
  112. * <h2>StringBuilder Reuse</h2><br>
  113. * Bad:
  114. *
  115. * <pre>
  116. * final StringBuilder sbString1 = new StringBuilder();
  117. * StringUtil.append(sbString1, &quot;text 1&quot;, String.valueOf(npcId), &quot;text 2&quot;);
  118. * ... // output of sbString1, it is no more needed
  119. * final StringBuilder sbString2 = new StringBuilder();
  120. * StringUtil.append(sbString2, &quot;text 3&quot;, String.valueOf(npcId), &quot;text 4&quot;);
  121. * </pre>
  122. *
  123. * Good:
  124. *
  125. * <pre>
  126. * final StringBuilder sbString = new StringBuilder();
  127. * StringUtil.append(sbString, &quot;text 1&quot;, String.valueOf(npcId), &quot;text 2&quot;);
  128. * ... // output of sbString, it is no more needed
  129. * sbString.setLength(0);
  130. * StringUtil.append(sbString, &quot;text 3&quot;, String.valueOf(npcId), &quot;text 4&quot;);
  131. * </pre>
  132. *
  133. * Why?</br> In first case, new memory has to be allocated for the second string. In second case already allocated memory is reused, but only in case the new string is not longer than the previously allocated string. Anyway, the second way is better because the string either fits in the memory and
  134. * some memory is saved, or it does not fit in the memory, and in that case it works as in the first case. <h2>Primitives to Strings</h2> To convert primitives to string, use String.valueOf().<br>
  135. * <br>
  136. * <h2>How much faster is it?</h2><br>
  137. * Here are some results of my tests. Count is number of strings concatenated. Don't take the numbers as 100% true as the numbers are affected by other programs running on my computer at the same time. Anyway, from the results it is obvious that using StringBuilder with predefined size is the
  138. * fastest (and also most memory efficient) solution. It is about 5 times faster when concatenating 7 strings, compared to TextBuilder. Also, with more strings concatenated, the difference between StringBuilder and TextBuilder gets larger. In code, there are many cases, where there are concatenated
  139. * 50+ strings so the time saving is even greater.<br>
  140. *
  141. * <pre>
  142. * Count: 2
  143. * TextBuilder: 1893
  144. * TextBuilder with size: 1703
  145. * String: 1033
  146. * StringBuilder: 993
  147. * StringBuilder with size: 1024
  148. * Count: 3
  149. * TextBuilder: 1973
  150. * TextBuilder with size: 1872
  151. * String: 2583
  152. * StringBuilder: 1633
  153. * StringBuilder with size: 1156
  154. * Count: 4
  155. * TextBuilder: 2188
  156. * TextBuilder with size: 2229
  157. * String: 4207
  158. * StringBuilder: 1816
  159. * StringBuilder with size: 1444
  160. * Count: 5
  161. * TextBuilder: 9185
  162. * TextBuilder with size: 9464
  163. * String: 6937
  164. * StringBuilder: 2745
  165. * StringBuilder with size: 1882
  166. * Count: 6
  167. * TextBuilder: 9785
  168. * TextBuilder with size: 10082
  169. * String: 9471
  170. * StringBuilder: 2889
  171. * StringBuilder with size: 1857
  172. * Count: 7
  173. * TextBuilder: 10169
  174. * TextBuilder with size: 10528
  175. * String: 12746
  176. * StringBuilder: 3081
  177. * StringBuilder with size: 2139
  178. * </pre>
  179. * @author fordfrog
  180. */
  181. public final class StringUtil
  182. {
  183. private StringUtil()
  184. {
  185. }
  186. /**
  187. * Concatenates strings.
  188. * @param strings strings to be concatenated
  189. * @return concatenated string
  190. * @see StringUtil
  191. */
  192. public static String concat(final String... strings)
  193. {
  194. final TextBuilder sbString = TextBuilder.newInstance();
  195. for (final String string : strings)
  196. {
  197. sbString.append(string);
  198. }
  199. String result = sbString.toString();
  200. TextBuilder.recycle(sbString);
  201. return result;
  202. }
  203. /**
  204. * Creates new string builder with size initializated to <code>sizeHint</code>, unless total length of strings is greater than <code>sizeHint</code>.
  205. * @param sizeHint hint for string builder size allocation
  206. * @param strings strings to be appended
  207. * @return created string builder
  208. * @see StringUtil
  209. */
  210. public static StringBuilder startAppend(final int sizeHint, final String... strings)
  211. {
  212. final int length = getLength(strings);
  213. final StringBuilder sbString = new StringBuilder(sizeHint > length ? sizeHint : length);
  214. for (final String string : strings)
  215. {
  216. sbString.append(string);
  217. }
  218. return sbString;
  219. }
  220. /**
  221. * Appends strings to existing string builder.
  222. * @param sbString string builder
  223. * @param strings strings to be appended
  224. * @see StringUtil
  225. */
  226. public static void append(final StringBuilder sbString, final String... strings)
  227. {
  228. sbString.ensureCapacity(sbString.length() + getLength(strings));
  229. for (final String string : strings)
  230. {
  231. sbString.append(string);
  232. }
  233. }
  234. /**
  235. * Counts total length of all the strings.
  236. * @param strings array of strings
  237. * @return total length of all the strings
  238. */
  239. private static int getLength(final String[] strings)
  240. {
  241. int length = 0;
  242. for (final String string : strings)
  243. {
  244. if (string == null)
  245. {
  246. length += 4;
  247. }
  248. else
  249. {
  250. length += string.length();
  251. }
  252. }
  253. return length;
  254. }
  255. public static String getTraceString(StackTraceElement[] trace)
  256. {
  257. final TextBuilder sbString = TextBuilder.newInstance();
  258. for (final StackTraceElement element : trace)
  259. {
  260. sbString.append(element.toString()).append('\n');
  261. }
  262. String result = sbString.toString();
  263. TextBuilder.recycle(sbString);
  264. return result;
  265. }
  266. }