Skip to content

Commit b78cb2c

Browse files
committed
Add arrayToString util and tests
Introduce StringUtils.arrayToString(Object[], String) which joins object array elements using a delimiter: returns empty string for null/empty arrays, returns single element as-is, and otherwise joins via StringJoiner. Added necessary imports and a static ArrayUtils.length usage. Updated tests to cover normal cases and null-delimiter behavior (expecting NPE), and replaced literal empty string usages with the EMPTY_STRING constant in CharSequenceUtilsTest.
1 parent 225828a commit b78cb2c

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

microsphere-java-core/src/main/java/io/microsphere/util/StringUtils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222

2323
import java.util.ArrayList;
2424
import java.util.Collection;
25+
import java.util.StringJoiner;
2526

2627
import static io.microsphere.collection.CollectionUtils.isEmpty;
2728
import static io.microsphere.collection.ListUtils.newArrayList;
29+
import static io.microsphere.util.ArrayUtils.length;
2830
import static io.microsphere.util.ArrayUtils.ofArray;
2931
import static io.microsphere.util.CharSequenceUtils.isEmpty;
3032
import static io.microsphere.util.CharSequenceUtils.length;
@@ -835,6 +837,41 @@ public static String uncapitalize(String str) {
835837
public static String[] toStringArray(@Nullable Collection<String> collection) {
836838
return isEmpty(collection) ? EMPTY_STRING_ARRAY : collection.toArray(EMPTY_STRING_ARRAY);
837839
}
840+
841+
/**
842+
* <p>Converts an array of Objects into a single String, with each element separated by the specified delimiter.</p>
843+
*
844+
* <p>A {@code null} or empty array returns an empty string. Each element is converted to a String using
845+
* {@link String#valueOf(Object)}. If the array contains only one element, that element's String representation
846+
* is returned without the delimiter.</p>
847+
*
848+
* <h3>Example Usage</h3>
849+
* <pre>{@code
850+
* StringUtils.arrayToString(null, ",") = ""
851+
* StringUtils.arrayToString(new Object[]{}, ",") = ""
852+
* StringUtils.arrayToString(new Object[]{"a"}, ",") = "a"
853+
* StringUtils.arrayToString(new Object[]{"a", "b", "c"}, ",") = "a,b,c"
854+
* StringUtils.arrayToString(new Object[]{1, 2, 3}, "-") = "1-2-3"
855+
* }</pre>
856+
*
857+
* @param values the array of Objects to convert, may be {@code null} or empty
858+
* @param delimiter the String to use as a delimiter between elements, may be {@code null} (treated as "null")
859+
* @return the joined String, or an empty string if the input array is {@code null} or empty
860+
*/
861+
public static String arrayToString(@Nullable Object[] values, String delimiter) {
862+
int length = length(values);
863+
if (length < 1) {
864+
return EMPTY_STRING;
865+
}
866+
if (length == 1) {
867+
return valueOf(values[0]);
868+
}
869+
StringJoiner sj = new StringJoiner(delimiter);
870+
for (Object elem : values) {
871+
sj.add(valueOf(elem));
872+
}
873+
return sj.toString();
874+
}
838875

839876
/**
840877
* Changes the first character of the given {@code String} to uppercase or lowercase,

microsphere-java-core/src/test/java/io/microsphere/util/CharSequenceUtilsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static io.microsphere.util.CharSequenceUtils.isNotEmpty;
99
import static io.microsphere.util.CharSequenceUtils.length;
1010
import static io.microsphere.util.CharSequenceUtils.trimAllWhitespace;
11+
import static io.microsphere.util.StringUtils.EMPTY_STRING;
1112
import static org.junit.jupiter.api.Assertions.assertEquals;
1213
import static org.junit.jupiter.api.Assertions.assertFalse;
1314
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -22,7 +23,7 @@
2223
*/
2324
class CharSequenceUtilsTest {
2425

25-
static final String TEST_EMPTY_STRING = "";
26+
static final String TEST_EMPTY_STRING = EMPTY_STRING;
2627

2728
static final String TEST_BLANK_STRING = SPACE;
2829

microsphere-java-core/src/test/java/io/microsphere/util/StringUtilsTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static io.microsphere.util.StringUtils.EMPTY;
1919
import static io.microsphere.util.StringUtils.EMPTY_STRING;
2020
import static io.microsphere.util.StringUtils.EMPTY_STRING_ARRAY;
21+
import static io.microsphere.util.StringUtils.arrayToString;
2122
import static io.microsphere.util.StringUtils.capitalize;
2223
import static io.microsphere.util.StringUtils.contains;
2324
import static io.microsphere.util.StringUtils.containsWhitespace;
@@ -45,6 +46,7 @@
4546
import static org.junit.jupiter.api.Assertions.assertFalse;
4647
import static org.junit.jupiter.api.Assertions.assertNull;
4748
import static org.junit.jupiter.api.Assertions.assertSame;
49+
import static org.junit.jupiter.api.Assertions.assertThrows;
4850
import static org.junit.jupiter.api.Assertions.assertTrue;
4951
import static org.springframework.util.StringUtils.delimitedListToStringArray;
5052

@@ -359,4 +361,16 @@ void testToStringArray() {
359361
assertArrayEquals(ofArray("a", "b", "c"), toStringArray(ofList("a", "b", "c")));
360362
}
361363

364+
@Test
365+
void testArrayToString() {
366+
assertSame(TEST_EMPTY_STRING, arrayToString(null, COMMA));
367+
assertSame(TEST_EMPTY_STRING, arrayToString(EMPTY_STRING_ARRAY, COMMA));
368+
assertSame("a", arrayToString(ofArray("a"), COMMA));
369+
assertEquals("a,b,c", arrayToString(ofArray("a", "b", "c"), COMMA));
370+
assertEquals("1-2-3", arrayToString(ofArray(1, 2, 3), "-"));
371+
assertEquals("1,2,3", arrayToString(ofArray(1, 2, 3), COMMA));
372+
373+
assertThrows(NullPointerException.class, () -> arrayToString(ofArray(1, 2, 3), null));
374+
}
375+
362376
}

0 commit comments

Comments
 (0)