You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/08/18 00:56:11 UTC
cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang WordUtils.java StringUtils.java
scolebourne 2003/08/17 15:56:11
Modified: lang/src/test/org/apache/commons/lang WordUtilsTest.java
StringUtilsTest.java
lang/src/java/org/apache/commons/lang WordUtils.java
StringUtils.java
Log:
Move capitalizeAllWords, uncapitalizeAllWords, swapCase to WordUtils
Revision Changes Path
1.2 +42 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/WordUtilsTest.java
Index: WordUtilsTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/WordUtilsTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- WordUtilsTest.java 17 Aug 2003 21:57:37 -0000 1.1
+++ WordUtilsTest.java 17 Aug 2003 22:56:11 -0000 1.2
@@ -195,4 +195,45 @@
// System.err.println(expected);
// System.err.println(WordUtils.wrap(input, 20, "\n", false));
}
+
+ //-----------------------------------------------------------------------
+ public void testCapitalize_String() {
+ assertEquals(null, WordUtils.capitalize(null));
+ assertEquals("", WordUtils.capitalize(""));
+ assertEquals(" ", WordUtils.capitalize(" "));
+
+ assertEquals("I", WordUtils.capitalize("I") );
+ assertEquals("I", WordUtils.capitalize("i") );
+ assertEquals("I Am Here 123", WordUtils.capitalize("i am here 123") );
+ assertEquals("I Am Here 123", WordUtils.capitalize("I Am Here 123") );
+ assertEquals("I Am HERE 123", WordUtils.capitalize("i am HERE 123") );
+ assertEquals("I AM HERE 123", WordUtils.capitalize("I AM HERE 123") );
+ }
+
+ public void testUncapitalize_String() {
+ assertEquals(null, WordUtils.uncapitalize(null));
+ assertEquals("", WordUtils.uncapitalize(""));
+ assertEquals(" ", WordUtils.uncapitalize(" "));
+
+ assertEquals("i", WordUtils.uncapitalize("I") );
+ assertEquals("i", WordUtils.uncapitalize("i") );
+ assertEquals("i am here 123", WordUtils.uncapitalize("i am here 123") );
+ assertEquals("i am here 123", WordUtils.uncapitalize("I Am Here 123") );
+ assertEquals("i am hERE 123", WordUtils.uncapitalize("i am HERE 123") );
+ assertEquals("i aM hERE 123", WordUtils.uncapitalize("I AM HERE 123") );
+ }
+
+ public void testSwapCase_String() {
+ assertEquals(null, WordUtils.swapCase(null));
+ assertEquals("", WordUtils.swapCase(""));
+ assertEquals(" ", WordUtils.swapCase(" "));
+
+ assertEquals("i", WordUtils.swapCase("I") );
+ assertEquals("I", WordUtils.swapCase("i") );
+ assertEquals("I AM HERE 123", WordUtils.swapCase("i am here 123") );
+ assertEquals("i aM hERE 123", WordUtils.swapCase("I Am Here 123") );
+ assertEquals("I AM here 123", WordUtils.swapCase("i am HERE 123") );
+ assertEquals("i am here 123", WordUtils.swapCase("I AM HERE 123") );
+ }
+
}
1.50 +14 -26 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.49
retrieving revision 1.50
diff -u -r1.49 -r1.50
--- StringUtilsTest.java 14 Aug 2003 01:16:30 -0000 1.49
+++ StringUtilsTest.java 17 Aug 2003 22:56:11 -0000 1.50
@@ -162,11 +162,8 @@
public void testCaseFunctions() {
assertEquals(null, StringUtils.upperCase(null));
assertEquals(null, StringUtils.lowerCase(null));
- assertEquals(null, StringUtils.swapCase(null));
assertEquals(null, StringUtils.capitalize(null));
assertEquals(null, StringUtils.uncapitalize(null));
- assertEquals(null, StringUtils.capitalizeAllWords(null));
- assertEquals(null, StringUtils.uncapitalizeAllWords(null));
assertEquals("capitalize(String) failed",
FOO_CAP, StringUtils.capitalize(FOO_UNCAP) );
@@ -174,36 +171,20 @@
"", StringUtils.capitalize("") );
assertEquals("capitalize(single-char-string) failed",
"X", StringUtils.capitalize("x") );
- assertEquals("capitalizeAllWords(String) failed",
- "Foo Bar Baz", StringUtils.capitalizeAllWords(SENTENCE_UNCAP) );
- assertEquals("capitalizeAllWords(empty-string) failed",
- "", StringUtils.capitalizeAllWords("") );
assertEquals("uncapitalize(String) failed",
FOO_UNCAP, StringUtils.uncapitalize(FOO_CAP) );
assertEquals("uncapitalize(empty-string) failed",
"", StringUtils.uncapitalize("") );
assertEquals("uncapitalize(single-char-string) failed",
"x", StringUtils.uncapitalize("X") );
- assertEquals("uncapitalizeAllWords(String) failed",
- SENTENCE_UNCAP, StringUtils.uncapitalizeAllWords("Foo Bar Baz") );
- assertEquals("uncapitalizeAllWords(empty-string) failed",
- "", StringUtils.uncapitalizeAllWords("") );
// reflection type of tests: Sentences.
- assertEquals("uncapitalizeAllWords(capitalizeAllWords(String)) failed",
- SENTENCE_UNCAP, StringUtils.uncapitalizeAllWords(StringUtils.capitalizeAllWords(SENTENCE_UNCAP)) );
- assertEquals("capitalizeAllWords(uncapitalizeAllWords(String)) failed",
- SENTENCE_CAP, StringUtils.capitalizeAllWords(StringUtils.uncapitalizeAllWords(SENTENCE_CAP)) );
assertEquals("uncapitalize(capitalize(String)) failed",
SENTENCE_UNCAP, StringUtils.uncapitalize(StringUtils.capitalize(SENTENCE_UNCAP)) );
assertEquals("capitalize(uncapitalize(String)) failed",
SENTENCE_CAP, StringUtils.capitalize(StringUtils.uncapitalize(SENTENCE_CAP)) );
// reflection type of tests: One word.
- assertEquals("uncapitalizeAllWords(capitalizeAllWords(String)) failed",
- FOO_UNCAP, StringUtils.uncapitalizeAllWords(StringUtils.capitalizeAllWords(FOO_UNCAP)) );
- assertEquals("capitalizeAllWords(uncapitalizeAllWords(String)) failed",
- FOO_CAP, StringUtils.capitalizeAllWords(StringUtils.uncapitalizeAllWords(FOO_CAP)) );
assertEquals("uncapitalize(capitalize(String)) failed",
FOO_UNCAP, StringUtils.uncapitalize(StringUtils.capitalize(FOO_UNCAP)) );
assertEquals("capitalize(uncapitalize(String)) failed",
@@ -217,15 +198,22 @@
"foo test thing", StringUtils.lowerCase("fOo test THING") );
assertEquals("lowerCase(empty-string) failed",
"", StringUtils.lowerCase("") );
+ }
- assertEquals("swapCase(empty-string) failed",
- "", StringUtils.swapCase("") );
- assertEquals("swapCase(String-with-numbers) failed",
- "a123RgYu", StringUtils.swapCase("A123rGyU") );
- assertEquals("swapCase(String) failed",
- "Hello aPACHE", StringUtils.swapCase("hELLO Apache") );
+ public void testSwapCase_String() {
+ assertEquals(null, StringUtils.swapCase(null));
+ assertEquals("", StringUtils.swapCase(""));
+ assertEquals(" ", StringUtils.swapCase(" "));
+
+ assertEquals("i", WordUtils.swapCase("I") );
+ assertEquals("I", WordUtils.swapCase("i") );
+ assertEquals("I AM HERE 123", StringUtils.swapCase("i am here 123") );
+ assertEquals("i aM hERE 123", StringUtils.swapCase("I Am Here 123") );
+ assertEquals("I AM here 123", StringUtils.swapCase("i am HERE 123") );
+ assertEquals("i am here 123", StringUtils.swapCase("I AM HERE 123") );
}
+ //-----------------------------------------------------------------------
public void testJoin_Objectarray() {
assertEquals(null, StringUtils.join(null));
assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST));
1.2 +139 -1 jakarta-commons/lang/src/java/org/apache/commons/lang/WordUtils.java
Index: WordUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/WordUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- WordUtils.java 17 Aug 2003 21:57:37 -0000 1.1
+++ WordUtils.java 17 Aug 2003 22:56:11 -0000 1.2
@@ -155,6 +155,8 @@
// return (stringBuffer.toString());
// }
+ // Wrapping
+ //-----------------------------------------------------------------------
/**
* <p>Wraps a single line of text, identifying words by <code>' '</code>.</p>
*
@@ -250,4 +252,140 @@
return wrappedLine.toString();
}
+ // Capitalizing
+ //-----------------------------------------------------------------------
+ /**
+ * <p>Capitalizes all the whitespace separated words in a String.
+ * Only the first letter of each word is changed.</p>
+ *
+ * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
+ * A <code>null</code> input String returns <code>null</code>.
+ * Capitalization uses the unicode title case, normally equivalent to
+ * upper case.</p>
+ *
+ * <pre>
+ * WordUtils.capitalize(null) = null
+ * WordUtils.capitalize("") = ""
+ * WordUtils.capitalize("i am FINE") = "I Am FINE"
+ * </pre>
+ *
+ * @param str the String to capitalize, may be null
+ * @return capitalized String, <code>null</code> if null String input
+ */
+ public static String capitalize(String str) {
+ int strLen;
+ if (str == null || (strLen = str.length()) == 0) {
+ return str;
+ }
+ StringBuffer buffer = new StringBuffer(strLen);
+ boolean whitespace = true;
+ for (int i = 0; i < strLen; i++) {
+ char ch = str.charAt(i);
+ if (Character.isWhitespace(ch)) {
+ buffer.append(ch);
+ whitespace = true;
+ } else if (whitespace) {
+ buffer.append(Character.toTitleCase(ch));
+ whitespace = false;
+ } else {
+ buffer.append(ch);
+ }
+ }
+ return buffer.toString();
+ }
+
+ /**
+ * <p>Uncapitalizes all the whitespace separated words in a String.
+ * Only the first letter of each word is changed.</p>
+ *
+ * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
+ * A <code>null</code> input String returns <code>null</code>.</p>
+ *
+ * <pre>
+ * WordUtils.uncapitalize(null) = null
+ * WordUtils.uncapitalize("") = ""
+ * WordUtils.uncapitalize("I Am FINE") = "i am fINE"
+ * </pre>
+ *
+ * @param str the String to uncapitalize, may be null
+ * @return uncapitalized String, <code>null</code> if null String input
+ * @see #uncapitalize(String)
+ * @see #capitalizeAllWords(String)
+ */
+ public static String uncapitalize(String str) {
+ int strLen;
+ if (str == null || (strLen = str.length()) == 0) {
+ return str;
+ }
+ StringBuffer buffer = new StringBuffer(strLen);
+ boolean whitespace = true;
+ for (int i = 0; i < strLen; i++) {
+ char ch = str.charAt(i);
+ if (Character.isWhitespace(ch)) {
+ buffer.append(ch);
+ whitespace = true;
+ } else if (whitespace) {
+ buffer.append(Character.toLowerCase(ch));
+ whitespace = false;
+ } else {
+ buffer.append(ch);
+ }
+ }
+ return buffer.toString();
+ }
+
+ /**
+ * <p>Swaps the case of a String using a word based algorithm.</p>
+ *
+ * <ul>
+ * <li>Upper case character converts to Lower case</li>
+ * <li>Title case character converts to Lower case</li>
+ * <li>Lower case character after Whitespace or at start converts to Title case</li>
+ * <li>Other Lower case character converts to Upper case</li>
+ * </ul>
+ *
+ * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
+ * A <code>null</code> input String returns <code>null</code>.</p>
+ *
+ * <pre>
+ * StringUtils.swapCase(null) = null
+ * StringUtils.swapCase("") = ""
+ * StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
+ * </pre>
+ *
+ * @param str the String to swap case, may be null
+ * @return the changed String, <code>null</code> if null String input
+ */
+ public static String swapCase(String str) {
+ int strLen;
+ if (str == null || (strLen = str.length()) == 0) {
+ return str;
+ }
+ StringBuffer buffer = new StringBuffer(strLen);
+
+ boolean whitespace = true;
+ char ch = 0;
+ char tmp = 0;
+
+ for (int i = 0; i < strLen; i++) {
+ ch = str.charAt(i);
+ if (Character.isUpperCase(ch)) {
+ tmp = Character.toLowerCase(ch);
+ } else if (Character.isTitleCase(ch)) {
+ tmp = Character.toLowerCase(ch);
+ } else if (Character.isLowerCase(ch)) {
+ if (whitespace) {
+ tmp = Character.toTitleCase(ch);
+ } else {
+ tmp = Character.toUpperCase(ch);
+ }
+ } else {
+ tmp = ch;
+ }
+ buffer.append(tmp);
+ whitespace = Character.isWhitespace(ch);
+ }
+ return buffer.toString();
+ }
+
}
1.101 +22 -105 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.100
retrieving revision 1.101
diff -u -r1.100 -r1.101
--- StringUtils.java 16 Aug 2003 10:36:00 -0000 1.100
+++ StringUtils.java 17 Aug 2003 22:56:11 -0000 1.101
@@ -3414,7 +3414,8 @@
* <p>Capitalizes a String changing the first letter to title case as
* per {@link Character#toTitleCase(char)}. No other letters are changed.</p>
*
- * <p>A <code>null</code> input String returns <code>null</code>.</p>
+ * <p>For a word based alorithm, see {@link WordUtils#capitalize(String)}.
+ * A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* StringUtils.capitalize(null) = null
@@ -3425,7 +3426,7 @@
*
* @param str the String to capitalize, may be null
* @return the capitalized String, <code>null</code> if null String input
- * @see #capitalizeAllWords(String)
+ * @see WordUtils#capitalize(String)
* @see #uncapitalize(String)
*/
public static String capitalize(String str) {
@@ -3456,7 +3457,8 @@
* <p>Uncapitalizes a String changing the first letter to title case as
* per {@link Character#toLowerCase(char)}. No other letters are changed.</p>
*
- * <p>A <code>null</code> input String returns <code>null</code>.</p>
+ * <p>For a word based alorithm, see {@link WordUtils#uncapitalize(String)}.
+ * A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* StringUtils.uncapitalize(null) = null
@@ -3467,7 +3469,7 @@
*
* @param str the String to uncapitalize, may be null
* @return the uncapitalized String, <code>null</code> if null String input
- * @see #uncapitalizeAllWords(String)
+ * @see WordUtils#uncapitalize(String)
* @see #capitalize(String)
*/
public static String uncapitalize(String str) {
@@ -3495,16 +3497,16 @@
}
/**
- * <p>Swaps the case of a String using a word based algorithm.</p>
+ * <p>Swaps the case of a String changing upper and title case to
+ * lower case, and lower case to upper case.</p>
*
* <ul>
* <li>Upper case character converts to Lower case</li>
* <li>Title case character converts to Lower case</li>
- * <li>Lower case character after Whitespace or at start converts to Title case</li>
- * <li>Other Lower case character converts to Upper case</li>
+ * <li>Lower case character converts to Upper case</li>
* </ul>
*
- * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
+ * <p>For a word based alorithm, see {@link WordUtils#swapCase(String)}.
* A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
@@ -3513,6 +3515,11 @@
* StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
* </pre>
*
+ * <p>NOTE: This method changed in Lang version 2.0.
+ * It no longer performs a word based alorithm.
+ * If you only use ASCII, you will notice no change.
+ * That functionality is available in WordUtils.</p>
+ *
* @param str the String to swap case, may be null
* @return the changed String, <code>null</code> if null String input
*/
@@ -3523,27 +3530,17 @@
}
StringBuffer buffer = new StringBuffer(strLen);
- boolean whitespace = true;
char ch = 0;
- char tmp = 0;
-
for (int i = 0; i < strLen; i++) {
ch = str.charAt(i);
if (Character.isUpperCase(ch)) {
- tmp = Character.toLowerCase(ch);
+ ch = Character.toLowerCase(ch);
} else if (Character.isTitleCase(ch)) {
- tmp = Character.toLowerCase(ch);
+ ch = Character.toLowerCase(ch);
} else if (Character.isLowerCase(ch)) {
- if (whitespace) {
- tmp = Character.toTitleCase(ch);
- } else {
- tmp = Character.toUpperCase(ch);
- }
- } else {
- tmp = ch;
+ ch = Character.toUpperCase(ch);
}
- buffer.append(tmp);
- whitespace = Character.isWhitespace(ch);
+ buffer.append(ch);
}
return buffer.toString();
}
@@ -3555,93 +3552,13 @@
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
* A <code>null</code> input String returns <code>null</code>.</p>
*
- * <pre>
- * StringUtils.capitalizeAllWords(null) = null
- * StringUtils.capitalizeAllWords("") = ""
- * StringUtils.capitalizeAllWords("i am FINE") = "I Am FINE"
- * </pre>
- *
* @param str the String to capitalize, may be null
* @return capitalized String, <code>null</code> if null String input
- * @see #capitalize(String)
- * @see #uncapitalizeAllWords(String)
- */
- public static String capitalizeAllWords(String str) {
- int strLen;
- if (str == null || (strLen = str.length()) == 0) {
- return str;
- }
- StringBuffer buffer = new StringBuffer(strLen);
- boolean whitespace = true;
- for (int i = 0; i < strLen; i++) {
- char ch = str.charAt(i);
- if (Character.isWhitespace(ch)) {
- buffer.append(ch);
- whitespace = true;
- } else if (whitespace) {
- buffer.append(Character.toTitleCase(ch));
- whitespace = false;
- } else {
- buffer.append(ch);
- }
- }
- return buffer.toString();
- }
-
- /**
- * <p>Capitalizes all the whitespace separated words in a String.
- * Only the first letter of each word is changed.</p>
- *
- * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
- * A <code>null</code> input String returns <code>null</code>.</p>
- *
- * @param str the String to capitalize, may be null
- * @return capitalized String, <code>null</code> if null String input
- * @deprecated Use the standardly named {@link #capitalizeAllWords(String)}.
+ * @deprecated Use the relocated {@link WordUtils#capitalize(String)}.
* Method will be removed in Commons Lang 3.0.
*/
public static String capitaliseAllWords(String str) {
- return capitalizeAllWords(str);
- }
-
- /**
- * <p>Uncapitalizes all the whitespace separated words in a String.
- * Only the first letter of each word is changed.</p>
- *
- * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
- * A <code>null</code> input String returns <code>null</code>.</p>
- *
- * <pre>
- * StringUtils.uncapitalizeAllWords(null) = null
- * StringUtils.uncapitalizeAllWords("") = ""
- * StringUtils.uncapitalizeAllWords("I Am FINE") = "i am fINE"
- * </pre>
- *
- * @param str the String to uncapitalize, may be null
- * @return uncapitalized String, <code>null</code> if null String input
- * @see #uncapitalize(String)
- * @see #capitalizeAllWords(String)
- */
- public static String uncapitalizeAllWords(String str) {
- int strLen;
- if (str == null || (strLen = str.length()) == 0) {
- return str;
- }
- StringBuffer buffer = new StringBuffer(strLen);
- boolean whitespace = true;
- for (int i = 0; i < strLen; i++) {
- char ch = str.charAt(i);
- if (Character.isWhitespace(ch)) {
- buffer.append(ch);
- whitespace = true;
- } else if (whitespace) {
- buffer.append(Character.toLowerCase(ch));
- whitespace = false;
- } else {
- buffer.append(ch);
- }
- }
- return buffer.toString();
+ return WordUtils.capitalize(str);
}
// Count matches
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org