--- old/./src/share/classes/java/lang/AbstractStringBuilder.java 2012-08-21 16:41:04.493708619 -0400 +++ new/./src/share/classes/java/lang/AbstractStringBuilder.java 2012-08-21 16:41:04.381708621 -0400 @@ -1386,6 +1386,29 @@ } /** + * Appends {@code n} concatenated copies of the CharSequence to the + * current value. If {@code n == 0}, then adds the empty string. If + * @{code cs} is {@code null}, then adds {@code "null"} {@code n} times. + * + * @param n the number of copies of the CharSequence to concatenate + * @param cs the CharSequence to concatenate (n times) + * @return a reference to this object + * @throws IllegalArgumentException if n < 0 + * @since 1.8 + */ + public AbstractStringBuilder append(int n, CharSequence cs) { + if (n < 0) { + throw new IllegalArgumentException("n < 0"); + } + + for (int i = 0; i < n; i++) { + append(cs); + } + + return this; + } + + /** * Returns a string representing the data in this sequence. * A new {@code String} object is allocated and initialized to * contain the character sequence currently represented by this --- old/./src/share/classes/java/lang/String.java 2012-08-21 16:41:05.001708604 -0400 +++ new/./src/share/classes/java/lang/String.java 2012-08-21 16:41:04.893708606 -0400 @@ -2362,6 +2362,30 @@ } /** + * Returns a new {@code String} whose value is {@code n} concatenated copies of the + * current value. If {@code n == 0}, then an empty string is returned. + * If {@code n == 1}, then the current {@code String} is returned. + * + * @param n the number of times the current value is concatenated to itself + * @return a reference to this String or a new String depending on the value of n + * @throws IllegalArgumentException if n < 0 + * @since 1.8 + */ + public String repeat( int n ) { + if (n < 0) { + throw new IllegalArgumentException( "n < 0"); + } + if (n == 0) { + return ""; + } + if (n == 1) { + return this; + } + + return new StringBuilder().append(n, this).toString(); + } + + /** * Converts all of the characters in this {@code String} to lower * case using the rules of the given {@code Locale}. Case mapping is based * on the Unicode Standard version specified by the {@link java.lang.Character Character} --- old/./src/share/classes/java/lang/StringBuffer.java 2012-08-21 16:41:05.581708587 -0400 +++ new/./src/share/classes/java/lang/StringBuffer.java 2012-08-21 16:41:05.469708589 -0400 @@ -319,6 +319,16 @@ return this; } + /** + * @throws IllegalArgumentException {@inheritDoc} + * @since 1.8 + */ + public synchronized StringBuffer append(int n, CharSequence cs) { + super.append(n, cs); + return this; + } + + public synchronized StringBuffer append(char[] str) { super.append(str); return this; --- old/./src/share/classes/java/lang/StringBuilder.java 2012-08-21 16:41:06.185708568 -0400 +++ new/./src/share/classes/java/lang/StringBuilder.java 2012-08-21 16:41:06.077708571 -0400 @@ -183,7 +183,16 @@ return this.append((StringBuilder)s); return this.append(s, 0, s.length()); } - + + /** + * @throws IllegalArgumentException if n < 0 {@inheritDoc} + * @since 1.8 + */ + public StringBuilder append(int n, CharSequence cs) { + super.append(n, cs); + return this; + } + /** * @throws IndexOutOfBoundsException {@inheritDoc} */ --- old/./test/Makefile 2012-08-21 16:41:06.769708552 -0400 +++ new/./test/Makefile 2012-08-21 16:41:06.649708554 -0400 @@ -480,6 +480,9 @@ jdk_lang: $(call TestDirs, java/lang) $(call RunAgentvmBatch) +jdk_lang_string: $(call TestDirs, java/lang/String java/lang/StringBuilder java/lang/StringBuffer) + $(call RunAgentvmBatch) + # Stable othervm testruns (minus items from PROBLEM_LIST) # Using agentvm has serious problems with these tests JDK_ALL_TARGETS += jdk_management1 --- /dev/null 2012-08-21 14:22:12.669954797 -0400 +++ new/./test/java/lang/String/Repeat.java 2012-08-21 16:41:07.221708537 -0400 @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @bug 6984084 + * @summary Test the String repeat(int n) method (n times repetition of String value) + * @author Jim Gish + */ + + +public class Repeat { + + public static void main (String args[]) throws Exception { + testRepeat(); + testRepeatWithNegativeN(); + } + + public static void testRepeat() { + assertEquals( "*".repeat(5), "*****" ); + assertEquals( "xo".repeat(5), "xoxoxoxoxo"); + assertEquals( "a".repeat(1), "a"); + assertEquals( "b".repeat(0), "" ); + } + + public static void testRepeatWithNegativeN() { + String result = null; + boolean iaeThrown = false; + try { + result = "c".repeat(-1); + } catch (IllegalArgumentException iae) { + iaeThrown = true; + } catch (Throwable t) { + throw new RuntimeException( "Expected IllegalAccessException on n<1. Instead got " + t ); + } + if (!iaeThrown) { + throw new RuntimeException( "Expected IllegalAccessException on n<1. Instead returned: " + result ); + } + } + + public static void assertEquals( String expected, String actual ) { + if (!actual.equals(expected)) { + throw new RuntimeException( "Expected: " + expected + ", actual = " + actual ); + } + } + + } \ No newline at end of file --- /dev/null 2012-08-21 14:22:12.669954797 -0400 +++ new/./test/java/lang/StringBuilder/AppendIntCharSequence.java 2012-08-21 16:41:07.713708524 -0400 @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @bug 6984084 + * @summary Test the StringBuilder append(int n, CharSequence cs) method (n times repetition of character sequence) + * @author Jim Gish + */ +public class AppendIntCharSequence { + + public static void main(String... args) { + testAppend_int_CharSequence(); + } + + /** + * Test of add method, of class AbstractStringBuilder. + */ + public static void testAppend_int_CharSequence() { + /* + * Check for n==1 + */ + StringBuilder sb = new StringBuilder("1 foo:").append(1, "foo"); + if (!sb.toString().equals( "1 foo:foo")) { + throw new RuntimeException( "Expected '1 foo:foo' got '" + sb.toString() + "'" ); + } + + sb = new StringBuilder("3 foos:").append(3, "foo"); + if (!sb.toString().equals( "3 foos:foofoofoo")) { + throw new RuntimeException( "Expected '3 foos:foofoofoo' got '" + sb.toString() + "'" ); + } + + /* + * Check for n<0 -- should throw IAE + */ + boolean iaeThrown = false; + try { + sb = new StringBuilder("").append( -1, "should throw"); + } catch (IllegalArgumentException iae) { + iaeThrown = true; + } catch (Throwable t) { + throw new RuntimeException( "Expected IllegalArgumentException on n<0. Instead got: " + t ); + } + if (!iaeThrown) { + throw new RuntimeException( "Expected IllegalArgumentException on n<0. Instead returned: " + sb.toString() ); + } + } +} \ No newline at end of file