Author: ggregory
Date: Sat Nov 10 23:30:43 2012
New Revision: 1407899
URL: http://svn.apache.org/viewvc?rev=1407899&view=rev
Log:
[LANG-853] StringUtils join APIs for primitives.
Modified:
commons/proper/lang/trunk/src/changes/changes.xml
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1407899&r1=1407898&r2=1407899&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml Sat Nov 10 23:30:43 2012
@@ -22,6 +22,7 @@
<body>
<release version="3.2" date="TBA" description="Next release">
+ <action issue="LANG-853" type="add">StringUtils join APIs for
primitives</action>
<action issue="LANG-849" type="fix">FastDateFormat and FastDatePrinter
generates Date objects wastefully</action>
<action issue="LANG-845" type="fix">Spelling fixes</action>
<action issue="LANG-844" type="fix">Fix examples contained in javadoc of
StringUtils.center methods</action>
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java?rev=1407899&r1=1407898&r2=1407899&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
(original)
+++
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
Sat Nov 10 23:30:43 2012
@@ -3170,6 +3170,231 @@ public class StringUtils {
}
/**
+ * <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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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.2
+ */
+ public static String join(long[] array, 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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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.2
+ */
+ public static String join(int[] array, 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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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.2
+ */
+ public static String join(short[] array, 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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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.2
+ */
+ public static String join(byte[] array, 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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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.2
+ */
+ public static String join(char[] array, 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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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.2
+ */
+ public static String join(float[] array, 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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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.2
+ */
+ public static String join(double[] array, 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>
*
@@ -3203,9 +3428,7 @@ public class StringUtils {
if (noOfItems <= 0) {
return EMPTY;
}
-
StringBuilder buf = new StringBuilder(noOfItems * 16);
-
for (int i = startIndex; i < endIndex; i++) {
if (i > startIndex) {
buf.append(separator);
@@ -3218,6 +3441,350 @@ public class StringUtils {
}
/**
+ * <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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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 an end 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.2
+ */
+ public static String join(long[] array, char separator, int startIndex,
int endIndex) {
+ if (array == null) {
+ return null;
+ }
+ int noOfItems = endIndex - startIndex;
+ if (noOfItems <= 0) {
+ return EMPTY;
+ }
+ StringBuilder buf = new StringBuilder(noOfItems * 16);
+ for (int i = startIndex; i < endIndex; i++) {
+ if (i > startIndex) {
+ 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.
+ * </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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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 an end 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.2
+ */
+ public static String join(int[] array, char separator, int startIndex, int
endIndex) {
+ if (array == null) {
+ return null;
+ }
+ int noOfItems = endIndex - startIndex;
+ if (noOfItems <= 0) {
+ return EMPTY;
+ }
+ StringBuilder buf = new StringBuilder(noOfItems * 16);
+ for (int i = startIndex; i < endIndex; i++) {
+ if (i > startIndex) {
+ 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.
+ * </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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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 an end 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.2
+ */
+ public static String join(byte[] array, char separator, int startIndex,
int endIndex) {
+ if (array == null) {
+ return null;
+ }
+ int noOfItems = endIndex - startIndex;
+ if (noOfItems <= 0) {
+ return EMPTY;
+ }
+ StringBuilder buf = new StringBuilder(noOfItems * 16);
+ for (int i = startIndex; i < endIndex; i++) {
+ if (i > startIndex) {
+ 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.
+ * </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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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 an end 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.2
+ */
+ public static String join(short[] array, char separator, int startIndex,
int endIndex) {
+ if (array == null) {
+ return null;
+ }
+ int noOfItems = endIndex - startIndex;
+ if (noOfItems <= 0) {
+ return EMPTY;
+ }
+ StringBuilder buf = new StringBuilder(noOfItems * 16);
+ for (int i = startIndex; i < endIndex; i++) {
+ if (i > startIndex) {
+ 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.
+ * </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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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 an end 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.2
+ */
+ public static String join(char[] array, char separator, int startIndex,
int endIndex) {
+ if (array == null) {
+ return null;
+ }
+ int noOfItems = endIndex - startIndex;
+ if (noOfItems <= 0) {
+ return EMPTY;
+ }
+ StringBuilder buf = new StringBuilder(noOfItems * 16);
+ for (int i = startIndex; i < endIndex; i++) {
+ if (i > startIndex) {
+ 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.
+ * </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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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 an end 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.2
+ */
+ public static String join(double[] array, char separator, int startIndex,
int endIndex) {
+ if (array == null) {
+ return null;
+ }
+ int noOfItems = endIndex - startIndex;
+ if (noOfItems <= 0) {
+ return EMPTY;
+ }
+ StringBuilder buf = new StringBuilder(noOfItems * 16);
+ for (int i = startIndex; i < endIndex; i++) {
+ if (i > startIndex) {
+ 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.
+ * </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([1, 2, 3], ';') = "1;2;3"
+ * StringUtils.join([1, 2, 3], null) = "123"
+ * </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 an end 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.2
+ */
+ public static String join(float[] array, char separator, int startIndex,
int endIndex) {
+ if (array == null) {
+ return null;
+ }
+ int noOfItems = endIndex - startIndex;
+ if (noOfItems <= 0) {
+ return EMPTY;
+ }
+ StringBuilder buf = new StringBuilder(noOfItems * 16);
+ for (int i = startIndex; i < endIndex; i++) {
+ if (i > startIndex) {
+ 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.</p>
*
Modified:
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java?rev=1407899&r1=1407898&r2=1407899&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
(original)
+++
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Sat Nov 10 23:30:43 2012
@@ -86,6 +86,13 @@ public class StringUtilsTest {
};
private static final String[] MIXED_ARRAY_LIST = {null, "", "foo"};
private static final Object[] MIXED_TYPE_LIST = {"foo", Long.valueOf(2L)};
+ private static final long[] LONG_PRIM_LIST = {1, 2};
+ private static final int[] INT_PRIM_LIST = {1, 2};
+ private static final byte[] BYTE_PRIM_LIST = {1, 2};
+ private static final short[] SHORT_PRIM_LIST = {1, 2};
+ private static final char[] CHAR_PRIM_LIST = {'1', '2'};
+ private static final float[] FLOAT_PRIM_LIST = {1, 2};
+ private static final double[] DOUBLE_PRIM_LIST = {1, 2};
private static final String SEPARATOR = ",";
private static final char SEPARATOR_CHAR = ';';
@@ -208,7 +215,7 @@ public class StringUtilsTest {
}
@Test
- public void testJoin_ArrayChar() {
+ public void testJoin_ArrayCharSeparator() {
assertEquals(null, StringUtils.join((Object[]) null, ','));
assertEquals(TEXT_LIST_CHAR, StringUtils.join(ARRAY_LIST,
SEPARATOR_CHAR));
assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, SEPARATOR_CHAR));
@@ -224,6 +231,55 @@ public class StringUtilsTest {
}
@Test
+ public void testJoin_ArrayOfChars() {
+ assertEquals(null, StringUtils.join((char[]) null, ','));
+ assertEquals("1;2", StringUtils.join(CHAR_PRIM_LIST, SEPARATOR_CHAR));
+ assertEquals("2", StringUtils.join(CHAR_PRIM_LIST, SEPARATOR_CHAR, 1,
2));
+ }
+
+ @Test
+ public void testJoin_ArrayOfBytes() {
+ assertEquals(null, StringUtils.join((byte[]) null, ','));
+ assertEquals("1;2", StringUtils.join(BYTE_PRIM_LIST, SEPARATOR_CHAR));
+ assertEquals("2", StringUtils.join(BYTE_PRIM_LIST, SEPARATOR_CHAR, 1,
2));
+ }
+
+ @Test
+ public void testJoin_ArrayOfInts() {
+ assertEquals(null, StringUtils.join((int[]) null, ','));
+ assertEquals("1;2", StringUtils.join(INT_PRIM_LIST, SEPARATOR_CHAR));
+ assertEquals("2", StringUtils.join(INT_PRIM_LIST, SEPARATOR_CHAR, 1,
2));
+ }
+
+ @Test
+ public void testJoin_ArrayOfLongs() {
+ assertEquals(null, StringUtils.join((long[]) null, ','));
+ assertEquals("1;2", StringUtils.join(LONG_PRIM_LIST, SEPARATOR_CHAR));
+ assertEquals("2", StringUtils.join(LONG_PRIM_LIST, SEPARATOR_CHAR, 1,
2));
+ }
+
+ @Test
+ public void testJoin_ArrayOfFloats() {
+ assertEquals(null, StringUtils.join((float[]) null, ','));
+ assertEquals("1.0;2.0", StringUtils.join(FLOAT_PRIM_LIST,
SEPARATOR_CHAR));
+ assertEquals("2.0", StringUtils.join(FLOAT_PRIM_LIST, SEPARATOR_CHAR,
1, 2));
+ }
+
+ @Test
+ public void testJoin_ArrayOfDoubles() {
+ assertEquals(null, StringUtils.join((double[]) null, ','));
+ assertEquals("1.0;2.0", StringUtils.join(DOUBLE_PRIM_LIST,
SEPARATOR_CHAR));
+ assertEquals("2.0", StringUtils.join(DOUBLE_PRIM_LIST, SEPARATOR_CHAR,
1, 2));
+ }
+
+ @Test
+ public void testJoin_ArrayOfShorts() {
+ assertEquals(null, StringUtils.join((short[]) null, ','));
+ assertEquals("1;2", StringUtils.join(SHORT_PRIM_LIST, SEPARATOR_CHAR));
+ assertEquals("2", StringUtils.join(SHORT_PRIM_LIST, SEPARATOR_CHAR, 1,
2));
+ }
+
+ @Test
public void testJoin_ArrayString() {
assertEquals(null, StringUtils.join((Object[]) null, null));
assertEquals(TEXT_LIST_NOSEP, StringUtils.join(ARRAY_LIST, null));