This commit is contained in:
Looly
2024-04-27 23:35:42 +08:00
parent 284bb06416
commit 6c55464dc9
2 changed files with 40 additions and 6 deletions

View File

@@ -145,7 +145,7 @@ public class StrJoiner implements Appendable, Serializable {
/** /**
* 设置分隔符 * 设置分隔符
* *
* @param delimiter 分隔符 * @param delimiter 分隔符{@code null}表示无连接符,直接拼接
* @return this * @return this
*/ */
public StrJoiner setDelimiter(final CharSequence delimiter) { public StrJoiner setDelimiter(final CharSequence delimiter) {
@@ -422,7 +422,9 @@ public class StrJoiner implements Appendable, Serializable {
*/ */
private Appendable prepare() throws IOException { private Appendable prepare() throws IOException {
if (hasContent) { if (hasContent) {
this.appendable.append(delimiter); if(null != delimiter){
this.appendable.append(delimiter);
}
} else { } else {
if (null == this.appendable) { if (null == this.appendable) {
this.appendable = new StringBuilder(); this.appendable = new StringBuilder();

View File

@@ -13,7 +13,6 @@
package org.dromara.hutool.core.array; package org.dromara.hutool.core.array;
import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.util.CharsetUtil; import org.dromara.hutool.core.util.CharsetUtil;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -26,10 +25,9 @@ import java.util.*;
* *
* @author Looly * @author Looly
*/ */
@SuppressWarnings("ConstantValue")
public class ArrayUtilTest { public class ArrayUtilTest {
@SuppressWarnings("DataFlowIssue") @SuppressWarnings({"DataFlowIssue", "ConstantValue"})
@Test @Test
public void isEmptyTest() { public void isEmptyTest() {
final int[] a = {}; final int[] a = {};
@@ -330,6 +328,41 @@ public class ArrayUtilTest {
Assertions.assertEquals("aa,bb,cc,dd", join2); Assertions.assertEquals("aa,bb,cc,dd", join2);
} }
@Test
public void testJoinWithNullElement() {
final String[] array = { "Java", null, "Python" };
final String result = ArrayUtil.join(array, ", ", value -> value == null ? "null" : value);
Assertions.assertEquals("Java, null, Python", result);
}
@Test
public void testJoinWithEmptyArray() {
final String[] array = {};
final String result = ArrayUtil.join(array, ", ", String::toUpperCase);
Assertions.assertEquals("", result);
}
@Test
public void testJoinWithoutEditor() {
final Integer[] array = { 1, 2, 3 };
final String result = ArrayUtil.join(array, ", ");
Assertions.assertEquals("1, 2, 3", result);
}
@Test
public void testJoinWithEditor() {
final String[] array = { "java", "scala", "kotlin" };
final String result = ArrayUtil.join(array, " -> ", String::toUpperCase);
Assertions.assertEquals("JAVA -> SCALA -> KOTLIN", result);
}
@Test
public void testJoinWithNullConjunction() {
final String[] array = { "one", "two", "three" };
final String result = ArrayUtil.join(array, null, value -> value + "!");
Assertions.assertEquals("one!two!three!", result);
}
@Test @Test
public void getArrayTypeTest() { public void getArrayTypeTest() {
Class<?> arrayType = ArrayUtil.getArrayType(int.class); Class<?> arrayType = ArrayUtil.getArrayType(int.class);
@@ -656,7 +689,6 @@ public class ArrayUtilTest {
void setOrPaddingTest(){ void setOrPaddingTest(){
final String[] arr = new String[0]; final String[] arr = new String[0];
final String[] newArr = ArrayUtil.setOrPadding(arr, 2, "Good"); final String[] newArr = ArrayUtil.setOrPadding(arr, 2, "Good");
Console.log(newArr);
Assertions.assertArrayEquals(new String[]{null, null, "Good"}, newArr); Assertions.assertArrayEquals(new String[]{null, null, "Good"}, newArr);
} }