/*
* Copyright (C) 2004-2015 L2J Server
*
* This file is part of L2J Server.
*
* L2J Server is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* L2J Server is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
*
* final StringBuilder sbString = new StringBuilder(); * StringUtil.append(sbString, "text 1", String.valueOf(npcId)); * StringUtil.append("text 2"); ** * Good: * *
* final StringBuilder sbString = new StringBuilder(); * StringUtil.append(sbString, "text 1", String.valueOf(npcId), "text 2"); ** * Why?
* final StringBuilder sbString = new StringBuilder(); * StringUtil.append(sbString, "header start", someText, "header end"); * for (int i = 0; i < 50; i++) * { * StringUtil.append(sbString, "text 1", stringArray[i], "text 2"); * } ** * Good: * *
* final StringBuilder sbString = StringUtil.startAppend(1300, "header start", someText, "header end"); * for (int i = 0; i < 50; i++) * { * StringUtil.append(sbString, "text 1", stringArray[i], "text 2"); * } ** * Why?
new
* StringBuilder(1300)
.* StringUtil.concat("text 1 ", "text 2", String.valueOf(npcId)); ** * Good: * *
* StringUtil.concat("text 1 " + "text 2", String.valueOf(npcId)); ** * or * *
* StringUtil.concat("text 1 text 2", String.valueOf(npcId)); ** * Why?
* String glue = "some glue"; * StringUtil.concat("text 1", glue, "text 2", glue, String.valueOf(npcId)); ** * Good: * *
* final String glue = "some glue"; * StringUtil.concat("text 1" + glue + "text2" + glue, String.valueOf(npcId)); ** * Why? Because when using
final
keyword, the glue
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
* time, so this cannot be used for cases like final String objectIdString =
* String.valueOf(getObjectId)
.* final StringBuilder sbString1 = new StringBuilder(); * StringUtil.append(sbString1, "text 1", String.valueOf(npcId), "text 2"); * ... // output of sbString1, it is no more needed * final StringBuilder sbString2 = new StringBuilder(); * StringUtil.append(sbString2, "text 3", String.valueOf(npcId), "text 4"); ** * Good: * *
* final StringBuilder sbString = new StringBuilder(); * StringUtil.append(sbString, "text 1", String.valueOf(npcId), "text 2"); * ... // output of sbString, it is no more needed * sbString.setLength(0); * StringUtil.append(sbString, "text 3", String.valueOf(npcId), "text 4"); ** * Why? 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 * some memory is saved, or it does not fit in the memory, and in that case it works as in the first case.
* Count: 2 * TextBuilder: 1893 * TextBuilder with size: 1703 * String: 1033 * StringBuilder: 993 * StringBuilder with size: 1024 * Count: 3 * TextBuilder: 1973 * TextBuilder with size: 1872 * String: 2583 * StringBuilder: 1633 * StringBuilder with size: 1156 * Count: 4 * TextBuilder: 2188 * TextBuilder with size: 2229 * String: 4207 * StringBuilder: 1816 * StringBuilder with size: 1444 * Count: 5 * TextBuilder: 9185 * TextBuilder with size: 9464 * String: 6937 * StringBuilder: 2745 * StringBuilder with size: 1882 * Count: 6 * TextBuilder: 9785 * TextBuilder with size: 10082 * String: 9471 * StringBuilder: 2889 * StringBuilder with size: 1857 * Count: 7 * TextBuilder: 10169 * TextBuilder with size: 10528 * String: 12746 * StringBuilder: 3081 * StringBuilder with size: 2139 ** @author fordfrog */ public final class StringUtil { private StringUtil() { } /** * Concatenates strings. * @param strings strings to be concatenated * @return concatenated string */ public static String concat(final String... strings) { final StringBuilder sbString = new StringBuilder(); for (final String string : strings) { sbString.append(string); } return sbString.toString(); } /** * Creates new string builder with size initializated to
sizeHint
, unless total length of strings is greater than sizeHint
.
* @param sizeHint hint for string builder size allocation
* @param strings strings to be appended
* @return created string builder
*/
public static StringBuilder startAppend(final int sizeHint, final String... strings)
{
final int length = getLength(strings);
final StringBuilder sbString = new StringBuilder(sizeHint > length ? sizeHint : length);
for (final String string : strings)
{
sbString.append(string);
}
return sbString;
}
/**
* Appends strings to existing string builder.
* @param sbString string builder
* @param strings strings to be appended
*/
public static void append(final StringBuilder sbString, final String... strings)
{
sbString.ensureCapacity(sbString.length() + getLength(strings));
for (final String string : strings)
{
sbString.append(string);
}
}
public static int getLength(final Iterable