StringUtils.join(Iterator, String) should only return null when the

Iterator is null
This commit is contained in:
Gary Gregory 2022-08-12 14:30:09 -04:00
parent 39f08ac62f
commit ec93f3b7f5
3 changed files with 6 additions and 8 deletions

View File

@ -90,6 +90,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="guicaiyue">Javadoc: StringUtils.repeat("", "x", 3) = "xx"; #918.</action> <action type="fix" dev="ggregory" due-to="guicaiyue">Javadoc: StringUtils.repeat("", "x", 3) = "xx"; #918.</action>
<action type="fix" dev="ggregory" due-to="Marc Wrobel">Fix typos #920, #923.</action> <action type="fix" dev="ggregory" due-to="Marc Wrobel">Fix typos #920, #923.</action>
<action type="fix" dev="ggregory" due-to="Bhimantoro Suryo Admodjo">Simplify condition #925.</action> <action type="fix" dev="ggregory" due-to="Bhimantoro Suryo Admodjo">Simplify condition #925.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">StringUtils.join(Iterable, String) should only return null when the Iterable is null.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">StringUtils.join(Iterator, String) should only return null when the Iterator is null.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">StringUtils.join(Iterator, String) should only return null when the Iterator is null.</action>
<!-- ADD --> <!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add GitHub coverage.yml.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add GitHub coverage.yml.</action>

View File

@ -4394,23 +4394,19 @@ public class StringUtils {
if (!iterator.hasNext()) { if (!iterator.hasNext()) {
return EMPTY; return EMPTY;
} }
final Object first = iterator.next();
if (!iterator.hasNext()) {
return toStringOrEmpty(first);
}
// two or more elements // two or more elements
final StringBuilder buf = new StringBuilder(STRING_BUILDER_SIZE); // Java default is 16, probably too small final StringBuilder buf = new StringBuilder(STRING_BUILDER_SIZE); // Java default is 16, probably too small
if (first != null) {
buf.append(first);
}
while (iterator.hasNext()) { while (iterator.hasNext()) {
buf.append(separator);
final Object obj = iterator.next(); final Object obj = iterator.next();
if (obj != null) { if (obj != null) {
buf.append(obj); buf.append(obj);
} }
if (iterator.hasNext()) {
buf.append(separator);
}
} }
return buf.toString(); return buf.toString();

View File

@ -1292,6 +1292,7 @@ public class StringUtilsTest extends AbstractLangTest {
assertEquals("", StringUtils.join(Arrays.asList(NULL_ARRAY_LIST).iterator(), SEPARATOR_CHAR)); assertEquals("", StringUtils.join(Arrays.asList(NULL_ARRAY_LIST).iterator(), SEPARATOR_CHAR));
assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), SEPARATOR_CHAR)); assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), SEPARATOR_CHAR));
assertEquals("foo", StringUtils.join(Collections.singleton("foo").iterator(), 'x')); assertEquals("foo", StringUtils.join(Collections.singleton("foo").iterator(), 'x'));
assertEquals("null", StringUtils.join(Arrays.asList(NULL_TO_STRING_LIST).iterator(), SEPARATOR_CHAR));
} }
@Test @Test