commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang StringUtilsTest.java
Date Sun, 23 Mar 2003 05:26:23 GMT
bayard      2003/03/22 21:26:23

  Modified:    lang/src/java/org/apache/commons/lang StringUtils.java
               lang/src/test/org/apache/commons/lang StringUtilsTest.java
  Log:
  Bug 17882 submitted [by hand as the patch was out of date].
  
  join and split now take chars as separators as well as Strings.
  
  Submitted by:	Henning P. Schmiedehausen
  
  Revision  Changes    Path
  1.35      +55 -9     jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- StringUtils.java	23 Mar 2003 04:58:47 -0000	1.34
  +++ StringUtils.java	23 Mar 2003 05:26:23 -0000	1.35
  @@ -523,7 +523,7 @@
        * @return the concatenated string.
        */
       public static String concatenate(Object[] array) {
  -        return join(array, "");
  +        return join(array, null);
       }
       
       /**
  @@ -538,12 +538,40 @@
        * @return the joined String
        */
       public static String join(Object[] array, String separator) {
  -        if (separator == null) {
  -            separator = "";
  +        int arraySize = array.length;
  +
  +        // ArraySize ==  0: Len = 0
  +        // ArraySize > 0:   Len = NofStrings *(len(firstString) + len(separator))
  +        //           (Assuming that all strings are roughly equally long)
  +        int bufSize 
  +            = ((arraySize == 0) ? 0 
  +                : arraySize * (array[0].toString().length() 
  +                    + ((separator != null) ? separator.length(): 0)));
  +
  +        StringBuffer buf = new StringBuffer(bufSize);
  +
  +        for (int i = 0; i < arraySize; i++) {
  +            if ((separator != null) && (i > 0)) {
  +                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. A
  +     *
  +     * @param array the array of values to join together
  +     * @param separator the separator character to use
  +     * @return the joined String
  +     */
  +    public static String join(Object[] array, char separator) {
           int arraySize = array.length;
  -        int bufSize = (arraySize == 0 ? 0 : (array[0].toString().length() +
  -                                 separator.length()) * arraySize);
  +        int bufSize = (arraySize == 0 ? 0 : (array[0].toString().length() + 1) * arraySize);
           StringBuffer buf = new StringBuffer(bufSize);
   
           for (int i = 0; i < arraySize; i++) {
  @@ -567,9 +595,27 @@
        * @return the joined String
        */
       public static String join(Iterator iterator, String separator) {
  -        if (separator == null) {
  -            separator = "";
  -        }
  +        StringBuffer buf = new StringBuffer(256);  // Java default is 16, probably too
small
  +        while (iterator.hasNext()) {
  +            buf.append(iterator.next());
  +            if ((separator != null) && iterator.hasNext()) {
  +                buf.append(separator);
  +            }
  +         }
  +        return buf.toString();
  +    }
  +
  +    /**
  +     * <p>Joins the elements of the provided <code>Iterator</code> into
  +     * a single String containing the provided elements.</p>
  +     *
  +     * <p>No delimiter is added before or after the list. A
  +     *
  +     * @param iterator the <code>Iterator</code> of values to join together
  +     * @param separator  the separator character to use
  +     * @return the joined String
  +     */
  +    public static String join(Iterator iterator, char separator) {
           StringBuffer buf = new StringBuffer(256);  // Java default is 16, probably too
small
           while (iterator.hasNext()) {
               buf.append(iterator.next());
  
  
  
  1.15      +39 -2     jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java
  
  Index: StringUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- StringUtilsTest.java	17 Mar 2003 05:28:37 -0000	1.14
  +++ StringUtilsTest.java	23 Mar 2003 05:26:23 -0000	1.15
  @@ -68,15 +68,20 @@
    * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
    * @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
    * @author Holger Krauth
  + * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
    * @version $Id$
    */
   public class StringUtilsTest extends TestCase {
   
       private static final String[] ARRAY_LIST = { "foo", "bar", "baz" };
  +    private static final String[] EMPTY_ARRAY_LIST = {};
   
       private static final String SEPARATOR = ",";
  +    private static final char   SEPARATOR_CHAR = ';';
   
       private static final String TEXT_LIST = "foo,bar,baz";
  +    private static final String TEXT_LIST_CHAR = "foo;bar;baz";
  +    private static final String TEXT_LIST_NOSEP = "foobarbaz";
   
       private static final String FOO = "foo";
       private static final String BAR = "bar";
  @@ -149,12 +154,44 @@
   
       public void testJoin() {
           assertEquals("concatenate(Object[]) failed", 
  -                     "foobarbaz", StringUtils.concatenate(ARRAY_LIST));
  +                     TEXT_LIST_NOSEP, StringUtils.concatenate(ARRAY_LIST));
           assertEquals("join(Object[], String) failed", TEXT_LIST,
                        StringUtils.join(ARRAY_LIST, SEPARATOR));
           assertEquals("join(Iterator, String) failed", TEXT_LIST,
                        StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(),
                                         SEPARATOR));
  +
  +        assertEquals("join(Object[], char) failed", TEXT_LIST_CHAR,
  +                     StringUtils.join(ARRAY_LIST, SEPARATOR_CHAR));
  +        assertEquals("join(Iterator, char) failed", TEXT_LIST_CHAR,
  +                     StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(),
  +                                      SEPARATOR_CHAR));
  +
  +        assertEquals("join(Object[], null) failed", TEXT_LIST_NOSEP,
  +                     StringUtils.join(ARRAY_LIST, null));
  +        assertEquals("join(Iterator, null) failed", TEXT_LIST_NOSEP,
  +                     StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(),
  +                                      null));
  +
  +        assertEquals("concatenate(Object[]) failed", 
  +                     "", StringUtils.concatenate(EMPTY_ARRAY_LIST));
  +        assertEquals("join(Object[], String) failed", "",
  +                     StringUtils.join(EMPTY_ARRAY_LIST, SEPARATOR));
  +        assertEquals("join(Iterator, String) failed", "",
  +                     StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(),
  +                                      SEPARATOR));
  +
  +        assertEquals("join(Object[], char) failed", "",
  +                     StringUtils.join(EMPTY_ARRAY_LIST, SEPARATOR_CHAR));
  +        assertEquals("join(Iterator, char) failed", "",
  +                     StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(),
  +                                      SEPARATOR_CHAR));
  +
  +        assertEquals("join(Object[], null) failed", "",
  +                     StringUtils.join(EMPTY_ARRAY_LIST, null));
  +        assertEquals("join(Iterator, null) failed", "",
  +                     StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(),
  +                                      null));
       }
   
       public void testSplit() {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message