Author: bayard
Date: Fri Sep 29 12:20:03 2006
New Revision: 451394
URL: http://svn.apache.org/viewvc?view=rev&rev=451394
Log:
Applying patch from Will Pugh in #LANG-268 - Addition of new variants of the
join method.
Modified:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
Modified:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java?view=diff&rev=451394&r1=451393&r2=451394
==============================================================================
---
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
(original)
+++
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
Fri Sep 29 12:20:03 2006
@@ -2550,12 +2550,50 @@
if (array == null) {
return null;
}
- int arraySize = array.length;
- int bufSize = (arraySize == 0 ? 0 : ((array[0] == null ? 16 :
array[0].toString().length()) + 1) * arraySize);
+
+ 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(["a", "b", "c"], ';') = "a;b;c"
+ * StringUtils.join(["a", "b", "c"], null) = "abc"
+ * StringUtils.join([null, "", "a"], ';') = ";;a"
+ * </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</code> if null array input
+ * @since 2.0
+ */
+ public static String join(Object[] array, char separator, int startIndex,
int endIndex) {
+ if (array == null) {
+ return null;
+ }
+ int bufSize = (endIndex - startIndex);
+ if (bufSize <= 0) {
+ return EMPTY;
+ }
+
+ bufSize *= ((array[startIndex] == null ? 16 :
array[startIndex].toString().length()) + 1);
StringBuffer buf = new StringBuffer(bufSize);
- for (int i = 0; i < arraySize; i++) {
- if (i > 0) {
+ for (int i = startIndex; i < endIndex; i++) {
+ if (i > startIndex) {
buf.append(separator);
}
if (array[i] != null) {
@@ -2565,6 +2603,7 @@
return buf.toString();
}
+
/**
* <p>Joins the elements of the provided array into a single String
* containing the provided list of elements.</p>
@@ -2592,25 +2631,58 @@
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.
+ * A <code>null</code> separator is the same as an empty String ("").
+ * 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(["a", "b", "c"], "--") = "a--b--c"
+ * StringUtils.join(["a", "b", "c"], null) = "abc"
+ * StringUtils.join(["a", "b", "c"], "") = "abc"
+ * StringUtils.join([null, "", "a"], ',') = ",,a"
+ * </pre>
+ *
+ * @param array the array of values to join together, may be null
+ * @param separator the separator character to use, null treated as ""
+ * @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</code> if null array input
+ */
+ public static String join(Object[] array, String separator, int
startIndex, int endIndex) {
+ if (array == null) {
+ return null;
+ }
if (separator == null) {
separator = EMPTY;
}
- int arraySize = array.length;
- // ArraySize == 0: Len = 0
- // ArraySize > 0: Len = NofStrings *(len(firstString) +
len(separator))
+ // endIndex - startIndex > 0: Len = NofStrings *(len(firstString) +
len(separator))
// (Assuming that all Strings are roughly equally long)
- int bufSize =
- ((arraySize == 0)
- ? 0
- : arraySize
- * ((array[0] == null ? 16 : array[0].toString().length())
- + separator.length()));
+ int bufSize = (endIndex - startIndex);
+ if (bufSize <= 0) {
+ return EMPTY;
+ }
+
+ bufSize *= ((array[startIndex] == null ? 16 :
array[startIndex].toString().length())
+ + separator.length());
StringBuffer buf = new StringBuffer(bufSize);
- for (int i = 0; i < arraySize; i++) {
- if (i > 0) {
+ for (int i = startIndex; i < endIndex; i++) {
+ if (i > startIndex) {
buf.append(separator);
}
if (array[i] != null) {
Modified:
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java?view=diff&rev=451394&r1=451393&r2=451394
==============================================================================
---
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
(original)
+++
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
Fri Sep 29 12:20:03 2006
@@ -222,6 +222,12 @@
assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, SEPARATOR_CHAR));
assertEquals(";;foo", StringUtils.join(MIXED_ARRAY_LIST,
SEPARATOR_CHAR));
assertEquals("foo;2", StringUtils.join(MIXED_TYPE_LIST,
SEPARATOR_CHAR));
+
+ assertEquals("/", StringUtils.join(MIXED_ARRAY_LIST, '/', 0,
MIXED_ARRAY_LIST.length-1));
+ assertEquals("foo", StringUtils.join(MIXED_TYPE_LIST, '/', 0, 1));
+ assertEquals("foo/2", StringUtils.join(MIXED_TYPE_LIST, '/', 0, 2));
+ assertEquals("2", StringUtils.join(MIXED_TYPE_LIST, '/', 1, 2));
+ assertEquals("", StringUtils.join(MIXED_TYPE_LIST, '/', 2, 1));
}
public void testJoin_ArrayString() {
@@ -238,6 +244,13 @@
assertEquals(TEXT_LIST, StringUtils.join(ARRAY_LIST, SEPARATOR));
assertEquals(",,foo", StringUtils.join(MIXED_ARRAY_LIST, SEPARATOR));
assertEquals("foo,2", StringUtils.join(MIXED_TYPE_LIST, SEPARATOR));
+
+ assertEquals("/", StringUtils.join(MIXED_ARRAY_LIST, "/", 0,
MIXED_ARRAY_LIST.length-1));
+ assertEquals("", StringUtils.join(MIXED_ARRAY_LIST, "", 0,
MIXED_ARRAY_LIST.length-1));
+ assertEquals("foo", StringUtils.join(MIXED_TYPE_LIST, "/", 0, 1));
+ assertEquals("foo/2", StringUtils.join(MIXED_TYPE_LIST, "/", 0, 2));
+ assertEquals("2", StringUtils.join(MIXED_TYPE_LIST, "/", 1, 2));
+ assertEquals("", StringUtils.join(MIXED_TYPE_LIST, "/", 2, 1));
}
public void testJoin_IteratorChar() {
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]