Added isWhitespace, isTrue and uncapitaliseAllWords methods and tests.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137103 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b556203db7
commit
04533a9268
|
@ -7,7 +7,7 @@
|
|||
|
||||
<div align="center">
|
||||
<h1>The Jakarta Commons <em>Lang</em> Component</h1>
|
||||
$Id: STATUS.html,v 1.23 2002/10/27 19:42:04 bayard Exp $<br />
|
||||
$Id: STATUS.html,v 1.24 2002/10/28 04:33:29 bayard Exp $<br />
|
||||
<a href="#Introduction">[Introduction]</a>
|
||||
<a href="#Dependencies">[Dependencies]</a>
|
||||
<a href="#Release Info">[Release Info]</a>
|
||||
|
@ -88,8 +88,6 @@ still under discussion, so please mail the list before actioning.</p>
|
|||
<li>CalendarUtils - possible addition of a calendar utility class (from Serge Knystautas)</li>
|
||||
<li>CloneUtils - utility class to enable cloning via various different mechanisms. This code exists in [pattern] at present.</li>
|
||||
<li>StringUtils truncateNicely method - A substring with some extra power to choose where to cut off. It was in Avalon and was added separately to String Taglib from a code submission. This suggests it may have some commonality. [CODED]</li>
|
||||
<li>StringUtils parseBoolean method - This method is in OSCore's TextUtils and seems very reusable. It could go in a BooleanUtils if such a need was seen. [CODED]</li>
|
||||
<li>StringUtils uncapitaliseAllWords method - String Taglib has shown that this method is missing from StringUtils. [CODED]</li>
|
||||
<li>StringUtils unescape method - String Taglib has shown that this method is missing from StringUtils. It would take a String with "\n" in and convert it to the Java character. unescape and escape should be symmetric. </li>
|
||||
<li>ArrayUtils - opinion seems to be that this belongs with [lang] and not [collections]
|
||||
<li>GUID and other Identifier generators - these may belong in [util], some code exists in [pattern] at the moment
|
||||
|
|
|
@ -73,7 +73,7 @@ import java.util.Iterator;
|
|||
* @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
|
||||
* @version $Id: StringUtils.java,v 1.20 2002/10/27 19:42:04 bayard Exp $
|
||||
* @version $Id: StringUtils.java,v 1.21 2002/10/28 04:33:29 bayard Exp $
|
||||
*/
|
||||
public class StringUtils {
|
||||
|
||||
|
@ -1255,6 +1255,36 @@ public class StringUtils {
|
|||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Uncapitalise all the words in a string. Uses
|
||||
* Character.isWhitespace
|
||||
* as a separator between words. Null will return null.
|
||||
*
|
||||
* @param str the string to uncapitalise
|
||||
* @return uncapitalised string
|
||||
*/
|
||||
public static String uncapitaliseAllWords(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
int sz = str.length();
|
||||
StringBuffer buffer = new StringBuffer(sz);
|
||||
boolean space = true;
|
||||
for (int i = 0; i < sz; i++) {
|
||||
char ch = str.charAt(i);
|
||||
if (Character.isWhitespace(ch)) {
|
||||
buffer.append(ch);
|
||||
space = true;
|
||||
} else if (space) {
|
||||
buffer.append(Character.toLowerCase(ch));
|
||||
space = false;
|
||||
} else {
|
||||
buffer.append(ch);
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
// Nested extraction
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
|
@ -1341,6 +1371,27 @@ public class StringUtils {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the String contains only whitespace.
|
||||
* <code>null</code> will return <code>false</code>. The empty String
|
||||
* will return <code>true</code>.
|
||||
*
|
||||
* @param str the String to check
|
||||
* @return true if only contains whitespace, and is non-null
|
||||
*/
|
||||
public static boolean isWhitespace(String str) {
|
||||
if (str == null) {
|
||||
return false;
|
||||
}
|
||||
int sz = str.length();
|
||||
for (int i = 0; i < sz; i++) {
|
||||
if ((Character.isWhitespace(str.charAt(i)) == false) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the String contains only unicode letters and space (' ').
|
||||
* <code>null</code> will return <code>false</code>. The empty String
|
||||
|
@ -1449,6 +1500,26 @@ public class StringUtils {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the String contains a 'true' value. These are defined
|
||||
* as the words 'true', 'on' and 'yes'.
|
||||
*
|
||||
* @param str the String to check
|
||||
* @return true if the string is 'true|on|yes'
|
||||
*/
|
||||
public static boolean isTrue(String str) {
|
||||
if("true".equals(str)) {
|
||||
return true;
|
||||
} else
|
||||
if("on".equals(str)) {
|
||||
return true;
|
||||
} else
|
||||
if("yes".equals(str)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Defaults
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ import junit.textui.TestRunner;
|
|||
* Unit tests {@link org.apache.commons.lang.StringUtils} - Substring methods
|
||||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @version $Id: StringUtilsIsTest.java,v 1.1 2002/07/19 03:35:55 bayard Exp $
|
||||
* @version $Id: StringUtilsIsTest.java,v 1.2 2002/10/28 04:33:29 bayard Exp $
|
||||
*/
|
||||
public class StringUtilsIsTest extends TestCase {
|
||||
|
||||
|
@ -118,6 +118,29 @@ public class StringUtilsIsTest extends TestCase {
|
|||
assertEquals(false, StringUtils.isAlphanumeric("hkHKHik*khbkuh"));
|
||||
}
|
||||
|
||||
public void testIsWhitespace() {
|
||||
assertEquals(false, StringUtils.isWhitespace(null));
|
||||
assertEquals(true, StringUtils.isWhitespace(""));
|
||||
assertEquals(true, StringUtils.isWhitespace(" "));
|
||||
assertEquals(true, StringUtils.isWhitespace("\t \n \t"));
|
||||
assertEquals(false, StringUtils.isWhitespace("\t aa\n \t"));
|
||||
assertEquals(true, StringUtils.isWhitespace(" "));
|
||||
assertEquals(false, StringUtils.isWhitespace(" a "));
|
||||
assertEquals(false, StringUtils.isWhitespace("a "));
|
||||
assertEquals(false, StringUtils.isWhitespace(" a"));
|
||||
assertEquals(false, StringUtils.isWhitespace("aba"));
|
||||
}
|
||||
|
||||
public void testIsTrue() {
|
||||
assertEquals(false, StringUtils.isTrue(null));
|
||||
assertEquals(false, StringUtils.isTrue(""));
|
||||
assertEquals(false, StringUtils.isTrue("off"));
|
||||
assertEquals(false, StringUtils.isTrue("oof"));
|
||||
assertEquals(true, StringUtils.isTrue("true"));
|
||||
assertEquals(true, StringUtils.isTrue("yes"));
|
||||
assertEquals(true, StringUtils.isTrue("on"));
|
||||
}
|
||||
|
||||
public void testIsAlphaspace() {
|
||||
assertEquals(false, StringUtils.isAlphaSpace(null));
|
||||
assertEquals(true, StringUtils.isAlphaSpace(""));
|
||||
|
|
|
@ -68,7 +68,7 @@ import junit.textui.TestRunner;
|
|||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
|
||||
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
|
||||
* @version $Id: StringUtilsTest.java,v 1.5 2002/09/19 07:02:30 bayard Exp $
|
||||
* @version $Id: StringUtilsTest.java,v 1.6 2002/10/28 04:33:29 bayard Exp $
|
||||
*/
|
||||
public class StringUtilsTest extends TestCase
|
||||
{
|
||||
|
@ -123,6 +123,10 @@ public class StringUtilsTest extends TestCase
|
|||
FOO, StringUtils.uncapitalise(CAP_FOO) );
|
||||
assertEquals("uncapitalise(empty-string) failed",
|
||||
"", StringUtils.uncapitalise("") );
|
||||
assertEquals("uncapitaliseAllWords(String) failed",
|
||||
SENTENCE, StringUtils.uncapitaliseAllWords("Foo Bar Baz") );
|
||||
assertEquals("uncapitaliseAllWords(empty-string) failed",
|
||||
"", StringUtils.uncapitaliseAllWords("") );
|
||||
|
||||
assertEquals("upperCase(String) failed",
|
||||
"FOO TEST THING", StringUtils.upperCase("fOo test THING") );
|
||||
|
|
Loading…
Reference in New Issue