StringUtil.java 9.9 KB

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