StringUtil.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. import com.l2jserver.Config;
  18. /**
  19. * String utilities optimized for the best performance.<br>
  20. * <h1>How to Use It</h1> <h2>concat() or append()</h2> If concatenating strings<br>
  21. * in single call, use StringUtil.concat(), otherwise use StringUtil.append()<br>
  22. * and its variants.<br>
  23. * <br>
  24. * <h2>Minimum Calls</h2><br>
  25. * Bad:
  26. *
  27. * <pre>
  28. * final StringBuilder sbString = new StringBuilder();
  29. * StringUtil.append(sbString, &quot;text 1&quot;, String.valueOf(npcId));
  30. * StringUtil.append(&quot;text 2&quot;);
  31. * </pre>
  32. *
  33. * Good:
  34. *
  35. * <pre>
  36. * final StringBuilder sbString = new StringBuilder();
  37. * StringUtil.append(sbString, &quot;text 1&quot;, String.valueOf(npcId), &quot;text 2&quot;);
  38. * </pre>
  39. *
  40. * Why?<br/>
  41. * Because the less calls you do, the less memory re-allocations have to be done<br>
  42. * so the whole text fits into the memory and less array copy tasks has to be<br>
  43. * performed. So if using less calls, less memory is used and string concatenation is faster.<br>
  44. * <br>
  45. * <h2>Size Hints for Loops</h2><br>
  46. * Bad:
  47. *
  48. * <pre>
  49. * final StringBuilder sbString = new StringBuilder();
  50. * StringUtil.append(sbString, &quot;header start&quot;, someText, &quot;header end&quot;);
  51. * for (int i = 0; i &lt; 50; i++)
  52. * {
  53. * StringUtil.append(sbString, &quot;text 1&quot;, stringArray[i], &quot;text 2&quot;);
  54. * }
  55. * </pre>
  56. *
  57. * Good:
  58. *
  59. * <pre>
  60. * final StringBuilder sbString = StringUtil.startAppend(1300, &quot;header start&quot;, someText, &quot;header end&quot;);
  61. * for (int i = 0; i &lt; 50; i++)
  62. * {
  63. * StringUtil.append(sbString, &quot;text 1&quot;, stringArray[i], &quot;text 2&quot;);
  64. * }
  65. * </pre>
  66. *
  67. * Why?<br/>
  68. * 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
  69. * 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
  70. * rather than smaller.<br/>
  71. * In case there is no text appended before the cycle, just use <code>new
  72. * StringBuilder(1300)</code>.<br>
  73. * <br>
  74. * <h2>Concatenation and Constants</h2><br>
  75. * Bad:
  76. *
  77. * <pre>
  78. * StringUtil.concat(&quot;text 1 &quot;, &quot;text 2&quot;, String.valueOf(npcId));
  79. * </pre>
  80. *
  81. * Good:
  82. *
  83. * <pre>
  84. * StringUtil.concat(&quot;text 1 &quot; + &quot;text 2&quot;, String.valueOf(npcId));
  85. * </pre>
  86. *
  87. * or
  88. *
  89. * <pre>
  90. * StringUtil.concat(&quot;text 1 text 2&quot;, String.valueOf(npcId));
  91. * </pre>
  92. *
  93. * Why?<br/>
  94. * 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>
  95. * <h2>Concatenation and Constant Variables</h2> Bad:
  96. *
  97. * <pre>
  98. * String glue = &quot;some glue&quot;;
  99. * StringUtil.concat(&quot;text 1&quot;, glue, &quot;text 2&quot;, glue, String.valueOf(npcId));
  100. * </pre>
  101. *
  102. * Good:
  103. *
  104. * <pre>
  105. * final String glue = &quot;some glue&quot;;
  106. * StringUtil.concat(&quot;text 1&quot; + glue + &quot;text2&quot; + glue, String.valueOf(npcId));
  107. * </pre>
  108. *
  109. * 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
  110. * time, so this cannot be used for cases like <code>final String objectIdString =
  111. * String.valueOf(getObjectId)</code>.<br>
  112. * <br>
  113. * <h2>StringBuilder Reuse</h2><br>
  114. * Bad:
  115. *
  116. * <pre>
  117. * final StringBuilder sbString1 = new StringBuilder();
  118. * StringUtil.append(sbString1, &quot;text 1&quot;, String.valueOf(npcId), &quot;text 2&quot;);
  119. * ... // output of sbString1, it is no more needed
  120. * final StringBuilder sbString2 = new StringBuilder();
  121. * StringUtil.append(sbString2, &quot;text 3&quot;, String.valueOf(npcId), &quot;text 4&quot;);
  122. * </pre>
  123. *
  124. * Good:
  125. *
  126. * <pre>
  127. * final StringBuilder sbString = new StringBuilder();
  128. * StringUtil.append(sbString, &quot;text 1&quot;, String.valueOf(npcId), &quot;text 2&quot;);
  129. * ... // output of sbString, it is no more needed
  130. * sbString.setLength(0);
  131. * StringUtil.append(sbString, &quot;text 3&quot;, String.valueOf(npcId), &quot;text 4&quot;);
  132. * </pre>
  133. *
  134. * 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
  135. * 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>
  136. * <br>
  137. * <h2>How much faster is it?</h2><br>
  138. * 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
  139. * 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
  140. * 50+ strings so the time saving is even greater.<br>
  141. *
  142. * <pre>
  143. * Count: 2
  144. * TextBuilder: 1893
  145. * TextBuilder with size: 1703
  146. * String: 1033
  147. * StringBuilder: 993
  148. * StringBuilder with size: 1024
  149. * Count: 3
  150. * TextBuilder: 1973
  151. * TextBuilder with size: 1872
  152. * String: 2583
  153. * StringBuilder: 1633
  154. * StringBuilder with size: 1156
  155. * Count: 4
  156. * TextBuilder: 2188
  157. * TextBuilder with size: 2229
  158. * String: 4207
  159. * StringBuilder: 1816
  160. * StringBuilder with size: 1444
  161. * Count: 5
  162. * TextBuilder: 9185
  163. * TextBuilder with size: 9464
  164. * String: 6937
  165. * StringBuilder: 2745
  166. * StringBuilder with size: 1882
  167. * Count: 6
  168. * TextBuilder: 9785
  169. * TextBuilder with size: 10082
  170. * String: 9471
  171. * StringBuilder: 2889
  172. * StringBuilder with size: 1857
  173. * Count: 7
  174. * TextBuilder: 10169
  175. * TextBuilder with size: 10528
  176. * String: 12746
  177. * StringBuilder: 3081
  178. * StringBuilder with size: 2139
  179. * </pre>
  180. * @author fordfrog
  181. */
  182. public final class StringUtil
  183. {
  184. private StringUtil()
  185. {
  186. }
  187. /**
  188. * Concatenates strings.
  189. * @param strings strings to be concatenated
  190. * @return concatenated string
  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. */
  209. public static StringBuilder startAppend(final int sizeHint, final String... strings)
  210. {
  211. final int length = getLength(strings);
  212. final StringBuilder sbString = new StringBuilder(sizeHint > length ? sizeHint : length);
  213. for (final String string : strings)
  214. {
  215. sbString.append(string);
  216. }
  217. return sbString;
  218. }
  219. /**
  220. * Appends strings to existing string builder.
  221. * @param sbString string builder
  222. * @param strings strings to be appended
  223. */
  224. public static void append(final StringBuilder sbString, final String... strings)
  225. {
  226. sbString.ensureCapacity(sbString.length() + getLength(strings));
  227. for (final String string : strings)
  228. {
  229. sbString.append(string);
  230. }
  231. }
  232. /**
  233. * Counts total length of all the strings.
  234. * @param strings array of strings
  235. * @return total length of all the strings
  236. */
  237. private static int getLength(final String[] strings)
  238. {
  239. int length = 0;
  240. for (final String string : strings)
  241. {
  242. if (string == null)
  243. {
  244. length += 4;
  245. }
  246. else
  247. {
  248. length += string.length();
  249. }
  250. }
  251. return length;
  252. }
  253. public static String getTraceString(StackTraceElement[] trace)
  254. {
  255. final TextBuilder sbString = TextBuilder.newInstance();
  256. for (final StackTraceElement element : trace)
  257. {
  258. sbString.append(element.toString()).append(Config.EOL);
  259. }
  260. String result = sbString.toString();
  261. TextBuilder.recycle(sbString);
  262. return result;
  263. }
  264. }