LANG-1636 - Boolean Join Function (#686)
This commit is contained in:
parent
1d0422c909
commit
8dc37dc4a7
|
@ -3858,6 +3858,84 @@ public static boolean isWhitespace(final CharSequence cs) {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Joins the elements of the provided array into a single String containing the provided list of elements.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* No delimiter is added before or after the list. Null objects or empty strings within the array are represented
|
||||
* by empty strings.
|
||||
* </p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.join(null, *) = null
|
||||
* StringUtils.join([], *) = ""
|
||||
* StringUtils.join([null], *) = ""
|
||||
* StringUtils.join([false, false], ';') = "false;false"
|
||||
* </pre>
|
||||
*
|
||||
* @param array
|
||||
* the array of values to join together, may be null
|
||||
* @param separator
|
||||
* the separator character to use
|
||||
* @return the joined String, {@code null} if null array input
|
||||
* @since 3.12
|
||||
*/
|
||||
public static String join(final boolean[] array, final char separator) {
|
||||
if (array == null) {
|
||||
return null;
|
||||
}
|
||||
return join(array, separator, 0, array.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Joins the elements of the provided array into a single String containing the provided list of elements.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* No delimiter is added before or after the list. Null objects or empty strings within the array are represented
|
||||
* by empty strings.
|
||||
* </p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.join(null, *) = null
|
||||
* StringUtils.join([], *) = ""
|
||||
* StringUtils.join([null], *) = ""
|
||||
* StringUtils.join([true, false, true], ';') = "true;false;true"
|
||||
* </pre>
|
||||
*
|
||||
* @param array
|
||||
* the array of values to join together, may be null
|
||||
* @param separator
|
||||
* the separator character to use
|
||||
* @param startIndex
|
||||
* the first index to start joining from. It is an error to pass in a start index past the end of the
|
||||
* array
|
||||
* @param endIndex
|
||||
* the index to stop joining from (exclusive). It is an error to pass in an end index past the end of
|
||||
* the array
|
||||
* @return the joined String, {@code null} if null array input
|
||||
* @since 3.12
|
||||
*/
|
||||
public static String join(final boolean[] array, final char separator, final int startIndex, final int endIndex) {
|
||||
if (array == null) {
|
||||
return null;
|
||||
}
|
||||
final int noOfItems = endIndex - startIndex;
|
||||
if (noOfItems <= 0) {
|
||||
return EMPTY;
|
||||
}
|
||||
final StringBuilder buf = newStringBuilder(noOfItems);
|
||||
buf.append(array[startIndex]);
|
||||
for (int i = startIndex + 1; i < endIndex; i++) {
|
||||
buf.append(separator);
|
||||
buf.append(array[i]);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Joins the elements of the provided array into a single String containing the provided list of elements.
|
||||
|
|
|
@ -114,6 +114,7 @@ public String toString() {
|
|||
|
||||
private static final String SEPARATOR = ",";
|
||||
private static final char SEPARATOR_CHAR = ';';
|
||||
private static final char COMMA_SEPARATOR_CHAR = ',';
|
||||
|
||||
private static final String TEXT_LIST = "foo,bar,baz";
|
||||
private static final String TEXT_LIST_CHAR = "foo;bar;baz";
|
||||
|
@ -125,6 +126,11 @@ public String toString() {
|
|||
private static final String SENTENCE_UNCAP = "foo bar baz";
|
||||
private static final String SENTENCE_CAP = "Foo Bar Baz";
|
||||
|
||||
private static final boolean[] EMPTY = {};
|
||||
private static final boolean[] ARRAY_FALSE_FALSE = {false, false};
|
||||
private static final boolean[] ARRAY_FALSE_TRUE = {false, true};
|
||||
private static final boolean[] ARRAY_FALSE_TRUE_FALSE = {false, true, false};
|
||||
|
||||
private void assertAbbreviateWithAbbrevMarkerAndOffset(final String expected, final String abbrevMarker, final int offset, final int maxWidth) {
|
||||
final String abcdefghijklmno = "abcdefghijklmno";
|
||||
final String message = "abbreviate(String,String,int,int) failed";
|
||||
|
@ -1155,6 +1161,19 @@ public void testJoin_ArrayOfBytes() {
|
|||
assertEquals(StringUtils.EMPTY, StringUtils.join(BYTE_PRIM_LIST, SEPARATOR_CHAR, 1, 0));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testJoin_ArrayOfBooleans() {
|
||||
assertNull(StringUtils.join((boolean[]) null, COMMA_SEPARATOR_CHAR));
|
||||
assertEquals("false;false", StringUtils.join(ARRAY_FALSE_FALSE, SEPARATOR_CHAR));
|
||||
assertEquals("", StringUtils.join(EMPTY, SEPARATOR_CHAR));
|
||||
assertEquals("false,true,false", StringUtils.join(ARRAY_FALSE_TRUE_FALSE, COMMA_SEPARATOR_CHAR));
|
||||
assertEquals("true", StringUtils.join(ARRAY_FALSE_TRUE, SEPARATOR_CHAR, 1, 2));
|
||||
assertNull(StringUtils.join((boolean[]) null, SEPARATOR_CHAR, 0, 1));
|
||||
assertEquals(StringUtils.EMPTY, StringUtils.join(ARRAY_FALSE_FALSE, SEPARATOR_CHAR, 0, 0));
|
||||
assertEquals(StringUtils.EMPTY, StringUtils.join(ARRAY_FALSE_TRUE_FALSE, SEPARATOR_CHAR, 1, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoin_ArrayOfChars() {
|
||||
assertNull(StringUtils.join((char[]) null, ','));
|
||||
|
|
Loading…
Reference in New Issue