StringUtil.java 9.5 KB

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