StringUtil.java 9.8 KB

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