src/share/classes/java/lang/AbstractStringBuilder.java

Print this page

        

@@ -25,10 +25,11 @@
 
 package java.lang;
 
 import sun.misc.FloatingDecimal;
 import java.util.Arrays;
+import java.util.Iterator;
 
 /**
  * A mutable sequence of characters.
  * <p>
  * Implements a modifiable string. At any point in time it contains some

@@ -1408,10 +1409,94 @@
         }
         return this;
     }
 
     /**
+     * Append to this sequence a composition of the values returned
+     * by calling {@link String#valueOf(Object)} on each argument and joining them together
+     * with the delimiter parameter.
+     * 
+     * @param delimiter the String appended between each elements.
+     * @param first the first element to append.
+     * @param elements  the other Object elements to join together with the first one.
+     * @return a reference to this object.
+     * 
+     * @throws  NullPointerException
+     *          If <tt>delimiter</tt> or <tt>elements</tt> is <tt>null</tt>
+     * 
+     * @since 1.7
+     */
+    public AbstractStringBuilder join(String delimiter, Object first, Object... elements) {
+        if (delimiter == null) {
+            throw new NullPointerException("delimiter is null");
+        }
+        append(first);
+        for(Object o : elements) {
+            append(delimiter).append(o);
+        }
+        return this;
+    }
+
+    /**
+     * Append to this sequence a composition of the values returned
+     * by calling {@link String#valueOf(Object)} on each array element
+     * and joining them together with the delimiter parameter.
+     * 
+     * @param delimiter the String appended between each elements.
+     * @param elements an array of Object element to join together.
+     * @return a reference to this object.
+     * 
+     * @throws  NullPointerException
+     *          If <tt>delimiter</tt> or <tt>elements</tt> is <tt>null</tt>
+     * 
+     * @since 1.7
+     */
+     public AbstractStringBuilder join(String delimiter, Object[] elements) {
+        if (delimiter == null) {
+            throw new NullPointerException("delimiter is null");
+        }
+        int length = elements.length;
+        if (length == 0) {
+            return this;
+        }
+        append(elements[0]);
+        for(int i=1; i<length; i++) {
+            append(delimiter).append(elements[i]);
+        }
+        return this;
+     }
+
+    /**
+     * Append to this sequence a composition of the values returned
+     * by calling {@link String#valueOf(Object)} on each argument and joining them together
+     * with the delimiter parameter.
+     * 
+     * @param delimiter the String appended between each elements.
+     * @param elements an Iterable that will have its elements joined together.
+     * @return a reference to this object.
+     * 
+     * @throws  NullPointerException
+     *          If <tt>delimiter</tt> or <tt>elements</tt> is <tt>null</tt>
+     * 
+     * @since 1.7
+     */
+    public AbstractStringBuilder join(String delimiter, Iterable<?> elements) {
+        if (delimiter == null) {
+            throw new NullPointerException("delimiter is null");
+        }
+        Iterator<?> iterator = elements.iterator();
+        if (!iterator.hasNext()) {
+            return this;
+        }
+        append(iterator.next());
+        while(iterator.hasNext()) {
+            append(delimiter).append(iterator.next());
+        }
+        return this;
+    }
+    
+    /**
      * Returns a string representing the data in this sequence.
      * A new <code>String</code> object is allocated and initialized to
      * contain the character sequence currently represented by this
      * object. This <code>String</code> is then returned. Subsequent
      * changes to this sequence do not affect the contents of the