--- old/src/share/classes/java/lang/AbstractStringBuilder.java 2009-02-06 18:03:25.000000000 +0100 +++ new/src/share/classes/java/lang/AbstractStringBuilder.java 2009-02-06 18:03:24.000000000 +0100 @@ -27,6 +27,7 @@ import sun.misc.FloatingDecimal; import java.util.Arrays; +import java.util.Iterator; /** * A mutable sequence of characters. @@ -1410,6 +1411,90 @@ } /** + * 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 delimiter or elements is null + * + * @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 delimiter or elements is null + * + * @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; idelimiter or elements is null + * + * @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 String object is allocated and initialized to * contain the character sequence currently represented by this --- old/src/share/classes/java/lang/String.java 2009-02-06 18:03:25.000000000 +0100 +++ new/src/share/classes/java/lang/String.java 2009-02-06 18:03:25.000000000 +0100 @@ -2826,6 +2826,96 @@ } /** + * Returns a new String that is a composition of the values returned + * by calling {@link String#valueOf(Object)} on each element and joining them together + * with the current String instance. + * + *
An example: + *
+     * String message = " ".join("Java", "is", "cool" );
+     * // message returned is: "Java is cool"
+     * 
+ * + * @param first the first element to join + * @param elements the other Object elements to join together with the first one. + * @return a new String that is composed from the first and the elements parameters and + * the String instance used as delimiter. + * + * @see StringBuilder#join(String,Object,Object[]) + * + * @throws NullPointerException + * If elements is null + * + * @since 1.7 + */ + public String join(Object first, Object... elements) { + if (elements.length==0) + return String.valueOf(first); + return new StringBuilder().join(this, first, elements).toString(); + } + + /** + * Returns a new String that is a composition of the values returned + * by calling {@link String#valueOf(Object)} on each array element + * and joining them together with the current String instance. + * + *
An example: + *
+     * String[] strings = new String[]{ "Java", "is", "cool" };
+     * String message = " ".join( strings );
+     * // message returned is: "Java is cool"
+     * 
+ * + * @param elements an array of Object element to join together. + * @return a new String that is composed from the elements parameters joining + * with the current String instance. + * + * @see #join(Object[]) + * @see StringBuilder#join(String,Object[]) + * + * @throws NullPointerException + * If elements is null + * + * @since 1.7 + */ + public String join(Object[] elements) { + return new StringBuilder().join(this, elements).toString(); + } + + /** + * Returns a new String that is a composition of the values returned by + * calling {@link String#valueOf(Object)} on each of the elements + * parameter and joining them together with the current String instance. + * + *
Two examples: + *
+     * List<String> strings = new LinkedList<String>();
+     * strings.add( "Java" ); strings.add( "is" ); strings.add( "cool" );
+     * String message = " ".join( strings );
+     * //message returned is: "Java is cool"
+     * Set<String> strings = new HashSet<String>();
+     * strings.add( "Java" ); strings.add( "is" ); strings.add( "cool" );
+     * String message = " ".join( strings );
+     * //message returned is: "Java is cool"
+     * 
+ * + * @param elements an Iterable that will have its elements joined together. + * @return a new String that is composed from the elements parameters + * and the current String instance. + * + * @see #join(Object[]) + * @see StringBuilder#join(String,Iterable) + * + * @throws NullPointerException + * If elements is null + * + * @since 1.7 + */ + public String join(Iterable elements) { + return new StringBuilder().join(this, elements).toString(); + } + + /** * Returns the string representation of the Object argument. * * @param obj an Object. --- old/src/share/classes/java/lang/StringBuffer.java 2009-02-06 18:03:26.000000000 +0100 +++ new/src/share/classes/java/lang/StringBuffer.java 2009-02-06 18:03:26.000000000 +0100 @@ -557,6 +557,36 @@ return this; } + /** + * @throws NullPointerException {@inheritDoc} + * @since 1.7 + */ + @Override + public synchronized StringBuffer join(String delimiter, Object first, Object... elements) { + super.join(delimiter, first, elements); + return this; + } + + /** + * @throws NullPointerException {@inheritDoc} + * @since 1.7 + */ + @Override + public synchronized StringBuffer join(String delimiter, Object[] elements) { + super.join(delimiter, elements); + return this; + } + + /** + * @throws NullPointerException {@inheritDoc} + * @since 1.7 + */ + @Override + public synchronized StringBuffer join(String delimiter, Iterable elements) { + super.join(delimiter, elements); + return this; + } + public synchronized String toString() { return new String(value, 0, count); } --- old/src/share/classes/java/lang/StringBuilder.java 2009-02-06 18:03:26.000000000 +0100 +++ new/src/share/classes/java/lang/StringBuilder.java 2009-02-06 18:03:26.000000000 +0100 @@ -400,6 +400,36 @@ return this; } + /** + * @throws NullPointerException {@inheritDoc} + * @since 1.7 + */ + @Override + public StringBuilder join(String delimiter, Object first, Object... elements) { + super.join(delimiter, first, elements); + return this; + } + + /** + * @throws NullPointerException {@inheritDoc} + * @since 1.7 + */ + @Override + public StringBuilder join(String delimiter, Object[] elements) { + super.join(delimiter, elements); + return this; + } + + /** + * @throws NullPointerException {@inheritDoc} + * @since 1.7 + */ + @Override + public StringBuilder join(String delimiter, Iterable elements) { + super.join(delimiter, elements); + return this; + } + public String toString() { // Create a copy, don't share the array return new String(value, 0, count);