Add isTrue and isFalse methods

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137962 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-10-08 21:27:00 +00:00
parent 49e1f137eb
commit ad794c4978
2 changed files with 55 additions and 2 deletions

View File

@ -28,7 +28,7 @@ import org.apache.commons.lang.math.NumberUtils;
* @author Matthew Hawthorne * @author Matthew Hawthorne
* @author Gary Gregory * @author Gary Gregory
* @since 2.0 * @since 2.0
* @version $Id: BooleanUtils.java,v 1.18 2004/02/18 22:59:50 ggregory Exp $ * @version $Id: BooleanUtils.java,v 1.19 2004/10/08 21:27:00 scolebourne Exp $
*/ */
public class BooleanUtils { public class BooleanUtils {
@ -67,6 +67,46 @@ public class BooleanUtils {
// boolean Boolean methods // boolean Boolean methods
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/**
* <p>Is a Boolean value <code>true</code>, handling <code>null</code>.</p>
*
* <pre>
* BooleanUtils.isTrue(Boolean.TRUE) = true
* BooleanUtils.isTrue(Boolean.FALSE) = false
* BooleanUtils.isTrue(null) = false
* </pre>
*
* @param bool the boolean to convert
* @return <code>true</code> only if the input is non-null and true
* @since 2.1
*/
public static boolean isTrue(Boolean bool) {
if (bool == null) {
return false;
}
return (bool.booleanValue() ? true : false);
}
/**
* <p>Is a Boolean value <code>false</code>, handling <code>null</code>.</p>
*
* <pre>
* BooleanUtils.isFalse(Boolean.TRUE) = false
* BooleanUtils.isFalse(Boolean.FALSE) = true
* BooleanUtils.isFalse(null) = false
* </pre>
*
* @param bool the boolean to convert
* @return <code>true</code> only if the input is non-null and false
* @since 2.1
*/
public static boolean isFalse(Boolean bool) {
if (bool == null) {
return false;
}
return (bool.booleanValue() ? false : true);
}
/** /**
* <p>Boolean factory that avoids creating new Boolean objecs all the time.</p> * <p>Boolean factory that avoids creating new Boolean objecs all the time.</p>
* *

View File

@ -28,7 +28,7 @@ import junit.textui.TestRunner;
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Matthew Hawthorne * @author Matthew Hawthorne
* @version $Id: BooleanUtilsTest.java,v 1.9 2004/02/18 23:06:19 ggregory Exp $ * @version $Id: BooleanUtilsTest.java,v 1.10 2004/10/08 21:27:00 scolebourne Exp $
*/ */
public class BooleanUtilsTest extends TestCase { public class BooleanUtilsTest extends TestCase {
@ -71,6 +71,19 @@ public class BooleanUtilsTest extends TestCase {
assertSame(Boolean.FALSE, BooleanUtils.negate(Boolean.TRUE)); assertSame(Boolean.FALSE, BooleanUtils.negate(Boolean.TRUE));
} }
//-----------------------------------------------------------------------
public void test_isTrue_Boolean() {
assertEquals(true, BooleanUtils.isTrue(Boolean.TRUE));
assertEquals(false, BooleanUtils.isTrue(Boolean.FALSE));
assertEquals(false, BooleanUtils.isTrue((Boolean) null));
}
public void test_isFalse_Boolean() {
assertEquals(false, BooleanUtils.isFalse(Boolean.TRUE));
assertEquals(true, BooleanUtils.isFalse(Boolean.FALSE));
assertEquals(false, BooleanUtils.isFalse((Boolean) null));
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public void test_toBooleanObject_boolean() { public void test_toBooleanObject_boolean() {
assertSame(Boolean.TRUE, BooleanUtils.toBooleanObject(true)); assertSame(Boolean.TRUE, BooleanUtils.toBooleanObject(true));