StringUtil.java 9.8 KB

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