Adding Locale overloads for toUpperCase and toLowerCase as provided by Benjamin Bentmann in LANG-430

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@655239 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2008-05-11 04:52:10 +00:00
parent 0e42c3d9da
commit 23b89b4eb5
2 changed files with 69 additions and 1 deletions

View File

@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
/**
* <p>Operations on {@link java.lang.String} that are
@ -4708,6 +4709,11 @@ public static String center(String str, int size, String padStr) {
* StringUtils.upperCase("aBc") = "ABC"
* </pre>
*
* <p><strong>Note:</strong> As described in the documentation for {@link String#toUpperCase()},
* the result of this method is affected by the current locale.
* For platform-independent case transformations, the method {@link #lowerCase(String, Locale)}
* should be used with a specific locale (e.g. {@link Locale#ENGLISH}).</p>
*
* @param str the String to upper case, may be null
* @return the upper cased String, <code>null</code> if null String input
*/
@ -4718,6 +4724,29 @@ public static String upperCase(String str) {
return str.toUpperCase();
}
/**
* <p>Converts a String to upper case as per {@link String#toUpperCase(Locale)}.</p>
*
* <p>A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* StringUtils.upperCase(null, Locale.ENGLISH) = null
* StringUtils.upperCase("", Locale.ENGLISH) = ""
* StringUtils.upperCase("aBc", Locale.ENGLISH) = "ABC"
* </pre>
*
* @param str the String to upper case, may be null
* @param locale the locale that defines the case transformation rules, must not be null
* @return the upper cased String, <code>null</code> if null String input
* @since 3.0
*/
public static String upperCase(String str, Locale locale) {
if (str == null) {
return null;
}
return str.toUpperCase(locale);
}
/**
* <p>Converts a String to lower case as per {@link String#toLowerCase()}.</p>
*
@ -4729,6 +4758,11 @@ public static String upperCase(String str) {
* StringUtils.lowerCase("aBc") = "abc"
* </pre>
*
* <p><strong>Note:</strong> As described in the documentation for {@link String#toLowerCase()},
* the result of this method is affected by the current locale.
* For platform-independent case transformations, the method {@link #lowerCase(String, Locale)}
* should be used with a specific locale (e.g. {@link Locale#ENGLISH}).</p>
*
* @param str the String to lower case, may be null
* @return the lower cased String, <code>null</code> if null String input
*/
@ -4739,6 +4773,29 @@ public static String lowerCase(String str) {
return str.toLowerCase();
}
/**
* <p>Converts a String to lower case as per {@link String#toLowerCase(Locale)}.</p>
*
* <p>A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* StringUtils.lowerCase(null, Locale.ENGLISH) = null
* StringUtils.lowerCase("", Locale.ENGLISH) = ""
* StringUtils.lowerCase("aBc", Locale.ENGLISH) = "abc"
* </pre>
*
* @param str the String to lower case, may be null
* @param locale the locale that defines the case transformation rules, must not be null
* @return the lower cased String, <code>null</code> if null String input
* @since 3.0
*/
public static String lowerCase(String str, Locale locale) {
if (str == null) {
return null;
}
return str.toLowerCase(locale);
}
/**
* <p>Capitalizes a String changing the first letter to title case as
* per {@link Character#toTitleCase(char)}. No other letters are changed.</p>

View File

@ -22,6 +22,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Locale;
import junit.framework.Test;
import junit.framework.TestCase;
@ -127,7 +128,9 @@ public void testConstructor() {
//-----------------------------------------------------------------------
public void testCaseFunctions() {
assertEquals(null, StringUtils.upperCase(null));
assertEquals(null, StringUtils.upperCase(null, Locale.ENGLISH));
assertEquals(null, StringUtils.lowerCase(null));
assertEquals(null, StringUtils.lowerCase(null, Locale.ENGLISH));
assertEquals(null, StringUtils.capitalize(null));
assertEquals(null, StringUtils.uncapitalise(null));
assertEquals(null, StringUtils.uncapitalize(null));
@ -185,7 +188,15 @@ public void testCaseFunctions() {
"foo test thing", StringUtils.lowerCase("fOo test THING") );
assertEquals("lowerCase(empty-string) failed",
"", StringUtils.lowerCase("") );
assertEquals("upperCase(String, Locale) failed",
"FOO TEST THING", StringUtils.upperCase("fOo test THING", Locale.ENGLISH) );
assertEquals("upperCase(empty-string, Locale) failed",
"", StringUtils.upperCase("", Locale.ENGLISH) );
assertEquals("lowerCase(String, Locale) failed",
"foo test thing", StringUtils.lowerCase("fOo test THING", Locale.ENGLISH) );
assertEquals("lowerCase(empty-string, Locale) failed",
"", StringUtils.lowerCase("", Locale.ENGLISH) );
}
public void testSwapCase_String() {