Java/Data Type/boolean
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
- 1 Autoboxing/unboxing a Boolean and Character.
- 2 Boolean And Or
- 3 Boolean class creates primitives that wrap themselves around data items of the boolean data type
- 4 Boolean Data Type
- 5 Boolean factory that avoids creating new Boolean objecs all the time.
- 6 Checks if a Boolean value is false, handling null by returning false
- 7 Checks if a Boolean value is not false, handling null by returning true
- 8 Checks if a Boolean value is not true, handling null by returning true
- 9 Checks if a Boolean value is true, handling null by returning false
- 10 Compare Two Java boolean Arrays Example
- 11 Convert boolean to integer
- 12 Convert Boolean to String
- 13 Convert boolean value to Boolean
- 14 Converting a string to a boolean
- 15 Convert integer to boolean
- 16 Convert Java boolean Primitive to Boolean object
- 17 Convert Java String Object to Boolean Object
- 18 Converts a Boolean to a boolean handling null
- 19 Converts a Boolean to a boolean handling null by returning false
- 20 Converts a Boolean to a Integer using the convention that zero is false
- 21 Converts a Boolean to an Integer specifying the conversion values.
- 22 Converts a boolean to an Integer using the convention that zero is false
- 23 Converts a Boolean to an int specifying the conversion values.
- 24 Converts a boolean to an int using the convention that zero is false
- 25 Converts a Boolean to a String returning one of the input Strings.
- 26 Converts a Boolean to a String returning "on", "off", or null.
- 27 Converts a boolean to a String returning "on" or "off"
- 28 Converts a Boolean to a String returning "true", "false", or null
- 29 Converts a boolean to a String returning "true" or "false"
- 30 Converts a Boolean to a String returning "yes", "no", or null
- 31 Converts a boolean to a String returning "yes" or "no"
- 32 Converts an Integer to a Boolean specifying the conversion values.
- 33 Converts an Integer to a Boolean using the convention that zero is false
- 34 Converts a String to a Boolean.
- 35 Converts a String to a boolean (optimised for performance).
- 36 Converts a String to a Boolean throwing an exception if no match.
- 37 Converts a String to a Boolean throwing an exception if no match found.
- 38 Convert String to Boolean
- 39 Create a boolean variable from string
- 40 Create an Boolean object from boolean value
- 41 Java boolean value
- 42 Looping Forever with true value
- 43 Performs an xor on an array of Booleans.
- 44 Performs an xor on a set of booleans.
- 45 Use toString method of Boolean class to convert it into String
Autoboxing/unboxing a Boolean and Character.
class AutoBox5 {
public static void main(String args[]) {
Boolean b = true;
if (b)
System.out.println("b is true");
Character ch = "x"; // box a char
char ch2 = ch; // unbox a char
System.out.println("ch2 is " + ch2);
}
}
Boolean And Or
/*
* Test out combinations of Ands and Ors on Boolean values.
* Some of these will probably throw a NullPointerException: which one(s)?
* Pay attention to short-circuit evaluation; if the first subexpression
* is known, do you need to evaluation the second??
*/
public class BooleanAndOr {
public static void main(String[] a) {
String s = null;
// These use the conventional logical "and" (&&) and "or" (||).
try {
if ((s != null) && (s.length() > 0))
System.out.println("At Point One");
if ((s != null) || (s.length() > 0))
System.out.println("At Point Two");
} catch (Exception e) {
System.out.print("Logical section threw ");
e.printStackTrace();
}
// These use bitwise "and" (&) and "or" (|); is it valid? What results?
try {
if ((s == null) & (s.length() > 0))
System.out.println("At Point Three");
if ((s == null) | (s.length() > 0))
System.out.println("At Point Four");
} catch (Exception e) {
System.out.print("Bitwise section threw ");
e.printStackTrace();
}
}
}
Boolean class creates primitives that wrap themselves around data items of the boolean data type
class MainClass {
public static void main(String[] args) {
boolean b = true;
Boolean b2 = new Boolean(b);
System.out.println(b2.booleanValue());
}
}
Boolean Data Type
public class Main {
public static void main(String[] args) {
boolean t = true;
System.out.println("t is " + t);
int x = 10;
boolean y = (x > 15);
System.out.println("y is " + y);
}
}
Boolean factory that avoids creating new Boolean objecs all the time.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Boolean factory that avoids creating new Boolean objecs all the time.</p>
*
* <p>This method was added to JDK1.4 but is available here for earlier JDKs.</p>
*
* <pre>
* BooleanUtils.toBooleanObject(false) = Boolean.FALSE
* BooleanUtils.toBooleanObject(true) = Boolean.TRUE
* </pre>
*
* @param bool the boolean to convert
* @return Boolean.TRUE or Boolean.FALSE as appropriate
*/
public static Boolean toBooleanObject(boolean bool) {
return bool ? Boolean.TRUE : Boolean.FALSE;
}
}
Checks if a Boolean value is false, handling null by returning false
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Checks if a <code>Boolean</code> value is <code>false</code>,
* handling <code>null</code> by returning <code>false</code>.</p>
*
* <pre>
* BooleanUtils.isFalse(Boolean.TRUE) = false
* BooleanUtils.isFalse(Boolean.FALSE) = true
* BooleanUtils.isFalse(null) = false
* </pre>
*
* @param bool the boolean to check, null returns <code>false</code>
* @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;
}
}
Checks if a Boolean value is not false, handling null by returning true
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Checks if a <code>Boolean</code> value is <i>not</i> <code>false</code>,
* handling <code>null</code> by returning <code>true</code>.</p>
*
* <pre>
* BooleanUtils.isNotFalse(Boolean.TRUE) = true
* BooleanUtils.isNotFalse(Boolean.FALSE) = false
* BooleanUtils.isNotFalse(null) = true
* </pre>
*
* @param bool the boolean to check, null returns <code>true</code>
* @return <code>true</code> if the input is null or true
* @since 2.3
*/
public static boolean isNotFalse(Boolean bool) {
return !isFalse(bool);
}
/**
* <p>Checks if a <code>Boolean</code> value is <code>false</code>,
* handling <code>null</code> by returning <code>false</code>.</p>
*
* <pre>
* BooleanUtils.isFalse(Boolean.TRUE) = false
* BooleanUtils.isFalse(Boolean.FALSE) = true
* BooleanUtils.isFalse(null) = false
* </pre>
*
* @param bool the boolean to check, null returns <code>false</code>
* @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;
}
}
Checks if a Boolean value is not true, handling null by returning true
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Checks if a <code>Boolean</code> value is <i>not</i> <code>true</code>,
* handling <code>null</code> by returning <code>true</code>.</p>
*
* <pre>
* BooleanUtils.isNotTrue(Boolean.TRUE) = false
* BooleanUtils.isNotTrue(Boolean.FALSE) = true
* BooleanUtils.isNotTrue(null) = true
* </pre>
*
* @param bool the boolean to check, null returns <code>true</code>
* @return <code>true</code> if the input is null or false
* @since 2.3
*/
public static boolean isNotTrue(Boolean bool) {
return !isTrue(bool);
}
/**
* <p>Checks if a <code>Boolean</code> value is <code>true</code>,
* handling <code>null</code> by returning <code>false</code>.</p>
*
* <pre>
* BooleanUtils.isTrue(Boolean.TRUE) = true
* BooleanUtils.isTrue(Boolean.FALSE) = false
* BooleanUtils.isTrue(null) = false
* </pre>
*
* @param bool the boolean to check, null returns <code>false</code>
* @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;
}
}
Checks if a Boolean value is true, handling null by returning false
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Checks if a <code>Boolean</code> value is <code>true</code>,
* handling <code>null</code> by returning <code>false</code>.</p>
*
* <pre>
* BooleanUtils.isTrue(Boolean.TRUE) = true
* BooleanUtils.isTrue(Boolean.FALSE) = false
* BooleanUtils.isTrue(null) = false
* </pre>
*
* @param bool the boolean to check, null returns <code>false</code>
* @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;
}
}
Compare Two Java boolean Arrays Example
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
boolean[] a1 = new boolean[] { true, false, true };
boolean[] a2 = new boolean[] { true, false, true };
System.out.println(Arrays.equals(a1, a2));
}
}
Convert boolean to integer
public class Main {
public static void main(String[] args) throws Exception {
boolean b = true;
int i = (b) ? 1 : 0;
}
}
Convert Boolean to String
public class Main {
public static void main(String[] args) {
boolean theValue = true;
//boolean to String conversion
String theValueAsString = new Boolean(theValue).toString();
System.out.println(theValueAsString);
}
}
//true
Convert boolean value to Boolean
public class Main {
public static void main(String[] args) {
boolean b = true;
Boolean bool = Boolean.valueOf(b);
System.out.println("bool = " + bool);
if (bool.equals(Boolean.TRUE)) {
System.out.println("bool = " + bool);
}
String s = "false";
Boolean bools = Boolean.valueOf(s);
System.out.println("bools = " + bools);
String f = "abc";
Boolean abc = Boolean.valueOf(f);
System.out.println("abc = " + abc);
}
}
Converting a string to a boolean
/*
* Copyright (c) 2001, 2002 The XDoclet team
* All rights reserved.
*/
/**
* @author Ara Abrahamian (ara_e@email.ru)
* @created Oct 15, 2001
* @version $Revision: 1.5 $
*/
public final class TypeConversionUtil
{
/**
* A utility method for converting a string to a boolean. "yes", "no", "true", "false", "1", "0", "on" and "off" are
* valid values for a boolean string (ignoring case). If not one of then then the value of defaultValue parameter is
* returned.
*
* @param defaultValue Description of Parameter
* @param in The String to convert
* @return true or false
*/
public static boolean stringToBoolean(String in, boolean defaultValue)
{
if (in == null || in.trim().length() == 0) {
return defaultValue;
}
else {
if (in.equalsIgnoreCase("on")) {
return true;
}
if (in.equalsIgnoreCase("off")) {
return false;
}
switch (in.charAt(0)) {
case "1":
case "t":
case "T":
case "y":
case "Y":
return true;
case "0":
case "f":
case "F":
case "n":
case "N":
return false;
default:
return defaultValue;
}
}
}
}
Convert integer to boolean
public class Main {
public static void main(String[] args) throws Exception {
int i=10;
boolean b = (i != 0);
System.out.println(b);
}
}
//true
Convert Java boolean Primitive to Boolean object
public class Main {
public static void main(String[] args) {
boolean b = true;
// using constructor
Boolean blnObj1 = new Boolean(b);
// using valueOf method of Boolean class.
Boolean blnObj2 = Boolean.valueOf(b);
}
}
Convert Java String Object to Boolean Object
public class Main {
public static void main(String[] args) {
String str = "false";
// Convert using constructor
Boolean blnObj1 = new Boolean(str);
System.out.println(blnObj1);
// Use valueOf method of Boolean class. This is a static method.
Boolean blnObj2 = Boolean.valueOf(str);
System.out.println(blnObj2);
}
}
Converts a Boolean to a boolean handling null
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a Boolean to a boolean handling <code>null</code>.</p>
*
* <pre>
* BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true
* BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false
* BooleanUtils.toBooleanDefaultIfNull(null, true) = true
* </pre>
*
* @param bool the boolean to convert
* @param valueIfNull the boolean value to return if <code>null</code>
* @return <code>true</code> or <code>false</code>
*/
public static boolean toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull) {
if (bool == null) {
return valueIfNull;
}
return bool.booleanValue() ? true : false;
}
}
Converts a Boolean to a boolean handling null by returning false
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a Boolean to a boolean handling <code>null</code>
* by returning <code>false</code>.</p>
*
* <pre>
* BooleanUtils.toBoolean(Boolean.TRUE) = true
* BooleanUtils.toBoolean(Boolean.FALSE) = false
* BooleanUtils.toBoolean(null) = false
* </pre>
*
* @param bool the boolean to convert
* @return <code>true</code> or <code>false</code>,
* <code>null</code> returns <code>false</code>
*/
public static boolean toBoolean(Boolean bool) {
if (bool == null) {
return false;
}
return bool.booleanValue() ? true : false;
}
}
Converts a Boolean to a Integer using the convention that zero is false
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a Boolean to a Integer using the convention that
* <code>zero</code> is <code>false</code>.</p>
*
* <p><code>null</code> will be converted to <code>null</code>.</p>
*
* <pre>
* BooleanUtils.toIntegerObject(Boolean.TRUE) = new Integer(1)
* BooleanUtils.toIntegerObject(Boolean.FALSE) = new Integer(0)
* </pre>
*
* @param bool the Boolean to convert
* @return one if Boolean.TRUE, zero if Boolean.FALSE, <code>null</code> if <code>null</code>
*/
public static Integer toIntegerObject(Boolean bool) {
if (bool == null) {
return null;
}
return bool.booleanValue() ? 1 : 0;
}
}
Converts a Boolean to an Integer specifying the conversion values.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a Boolean to an Integer specifying the conversion values.</p>
*
* <pre>
* BooleanUtils.toIntegerObject(Boolean.TRUE, new Integer(1), new Integer(0), new Integer(2)) = new Integer(1)
* BooleanUtils.toIntegerObject(Boolean.FALSE, new Integer(1), new Integer(0), new Integer(2)) = new Integer(0)
* BooleanUtils.toIntegerObject(null, new Integer(1), new Integer(0), new Integer(2)) = new Integer(2)
* </pre>
*
* @param bool the Boolean to convert
* @param trueValue the value to return if <code>true</code>,
* may be <code>null</code>
* @param falseValue the value to return if <code>false</code>,
* may be <code>null</code>
* @param nullValue the value to return if <code>null</code>,
* may be <code>null</code>
* @return the appropriate value
*/
public static Integer toIntegerObject(Boolean bool, Integer trueValue, Integer falseValue, Integer nullValue) {
if (bool == null) {
return nullValue;
}
return bool.booleanValue() ? trueValue : falseValue;
}
}
Converts a boolean to an Integer using the convention that zero is false
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a boolean to an Integer using the convention that
* <code>zero</code> is <code>false</code>.</p>
*
* <pre>
* BooleanUtils.toIntegerObject(true) = new Integer(1)
* BooleanUtils.toIntegerObject(false) = new Integer(0)
* </pre>
*
* @param bool the boolean to convert
* @return one if <code>true</code>, zero if <code>false</code>
*/
public static Integer toIntegerObject(boolean bool) {
return bool ? 1 : 0;
}
}
Converts a Boolean to an int specifying the conversion values.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a Boolean to an int specifying the conversion values.</p>
*
* <pre>
* BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1
* BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0
* BooleanUtils.toInteger(null, 1, 0, 2) = 2
* </pre>
*
* @param bool the Boolean to convert
* @param trueValue the value to return if <code>true</code>
* @param falseValue the value to return if <code>false</code>
* @param nullValue the value to return if <code>null</code>
* @return the appropriate value
*/
public static int toInteger(Boolean bool, int trueValue, int falseValue, int nullValue) {
if (bool == null) {
return nullValue;
}
return bool.booleanValue() ? trueValue : falseValue;
}
}
Converts a boolean to an int using the convention that zero is false
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a boolean to an int using the convention that
* <code>zero</code> is <code>false</code>.</p>
*
* <pre>
* BooleanUtils.toInteger(true) = 1
* BooleanUtils.toInteger(false) = 0
* </pre>
*
* @param bool the boolean to convert
* @return one if <code>true</code>, zero if <code>false</code>
*/
public static int toInteger(boolean bool) {
return bool ? 1 : 0;
}
}
Converts a Boolean to a String returning one of the input Strings.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a Boolean to a String returning one of the input Strings.</p>
*
* <pre>
* BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true"
* BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false"
* BooleanUtils.toString(null, "true", "false", null) = null;
* </pre>
*
* @param bool the Boolean to check
* @param trueString the String to return if <code>true</code>,
* may be <code>null</code>
* @param falseString the String to return if <code>false</code>,
* may be <code>null</code>
* @param nullString the String to return if <code>null</code>,
* may be <code>null</code>
* @return one of the three input Strings
*/
public static String toString(Boolean bool, String trueString, String falseString, String nullString) {
if (bool == null) {
return nullString;
}
return bool.booleanValue() ? trueString : falseString;
}
}
Converts a Boolean to a String returning "on", "off", or null.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a Boolean to a String returning <code>"on"</code>,
* <code>"off"</code>, or <code>null</code>.</p>
*
* <pre>
* BooleanUtils.toStringOnOff(Boolean.TRUE) = "on"
* BooleanUtils.toStringOnOff(Boolean.FALSE) = "off"
* BooleanUtils.toStringOnOff(null) = null;
* </pre>
*
* @param bool the Boolean to check
* @return <code>"on"</code>, <code>"off"</code>,
* or <code>null</code>
*/
public static String toStringOnOff(Boolean bool) {
return toString(bool, "on", "off", null);
}
/**
* <p>Converts a Boolean to a String returning one of the input Strings.</p>
*
* <pre>
* BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true"
* BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false"
* BooleanUtils.toString(null, "true", "false", null) = null;
* </pre>
*
* @param bool the Boolean to check
* @param trueString the String to return if <code>true</code>,
* may be <code>null</code>
* @param falseString the String to return if <code>false</code>,
* may be <code>null</code>
* @param nullString the String to return if <code>null</code>,
* may be <code>null</code>
* @return one of the three input Strings
*/
public static String toString(Boolean bool, String trueString, String falseString, String nullString) {
if (bool == null) {
return nullString;
}
return bool.booleanValue() ? trueString : falseString;
}
}
Converts a boolean to a String returning "on" or "off"
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a boolean to a String returning <code>"on"</code>
* or <code>"off"</code>.</p>
*
* <pre>
* BooleanUtils.toStringOnOff(true) = "on"
* BooleanUtils.toStringOnOff(false) = "off"
* </pre>
*
* @param bool the Boolean to check
* @return <code>"on"</code>, <code>"off"</code>,
* or <code>null</code>
*/
public static String toStringOnOff(boolean bool) {
return toString(bool, "on", "off");
}
/**
* <p>Converts a boolean to a String returning one of the input Strings.</p>
*
* <pre>
* BooleanUtils.toString(true, "true", "false") = "true"
* BooleanUtils.toString(false, "true", "false") = "false"
* </pre>
*
* @param bool the Boolean to check
* @param trueString the String to return if <code>true</code>,
* may be <code>null</code>
* @param falseString the String to return if <code>false</code>,
* may be <code>null</code>
* @return one of the two input Strings
*/
public static String toString(boolean bool, String trueString, String falseString) {
return bool ? trueString : falseString;
}
}
Converts a Boolean to a String returning "true", "false", or null
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a Boolean to a String returning <code>"true"</code>,
* <code>"false"</code>, or <code>null</code>.</p>
*
* <pre>
* BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true"
* BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false"
* BooleanUtils.toStringTrueFalse(null) = null;
* </pre>
*
* @param bool the Boolean to check
* @return <code>"true"</code>, <code>"false"</code>,
* or <code>null</code>
*/
public static String toStringTrueFalse(Boolean bool) {
return toString(bool, "true", "false", null);
}
/**
* <p>Converts a Boolean to a String returning one of the input Strings.</p>
*
* <pre>
* BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true"
* BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false"
* BooleanUtils.toString(null, "true", "false", null) = null;
* </pre>
*
* @param bool the Boolean to check
* @param trueString the String to return if <code>true</code>,
* may be <code>null</code>
* @param falseString the String to return if <code>false</code>,
* may be <code>null</code>
* @param nullString the String to return if <code>null</code>,
* may be <code>null</code>
* @return one of the three input Strings
*/
public static String toString(Boolean bool, String trueString, String falseString, String nullString) {
if (bool == null) {
return nullString;
}
return bool.booleanValue() ? trueString : falseString;
}
}
Converts a boolean to a String returning "true" or "false"
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a boolean to a String returning <code>"true"</code>
* or <code>"false"</code>.</p>
*
* <pre>
* BooleanUtils.toStringTrueFalse(true) = "true"
* BooleanUtils.toStringTrueFalse(false) = "false"
* </pre>
*
* @param bool the Boolean to check
* @return <code>"true"</code>, <code>"false"</code>,
* or <code>null</code>
*/
public static String toStringTrueFalse(boolean bool) {
return toString(bool, "true", "false");
}
/**
* <p>Converts a boolean to a String returning one of the input Strings.</p>
*
* <pre>
* BooleanUtils.toString(true, "true", "false") = "true"
* BooleanUtils.toString(false, "true", "false") = "false"
* </pre>
*
* @param bool the Boolean to check
* @param trueString the String to return if <code>true</code>,
* may be <code>null</code>
* @param falseString the String to return if <code>false</code>,
* may be <code>null</code>
* @return one of the two input Strings
*/
public static String toString(boolean bool, String trueString, String falseString) {
return bool ? trueString : falseString;
}
}
Converts a Boolean to a String returning "yes", "no", or null
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a Boolean to a String returning <code>"yes"</code>,
* <code>"no"</code>, or <code>null</code>.</p>
*
* <pre>
* BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes"
* BooleanUtils.toStringYesNo(Boolean.FALSE) = "no"
* BooleanUtils.toStringYesNo(null) = null;
* </pre>
*
* @param bool the Boolean to check
* @return <code>"yes"</code>, <code>"no"</code>,
* or <code>null</code>
*/
public static String toStringYesNo(Boolean bool) {
return toString(bool, "yes", "no", null);
}
/**
* <p>Converts a Boolean to a String returning one of the input Strings.</p>
*
* <pre>
* BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true"
* BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false"
* BooleanUtils.toString(null, "true", "false", null) = null;
* </pre>
*
* @param bool the Boolean to check
* @param trueString the String to return if <code>true</code>,
* may be <code>null</code>
* @param falseString the String to return if <code>false</code>,
* may be <code>null</code>
* @param nullString the String to return if <code>null</code>,
* may be <code>null</code>
* @return one of the three input Strings
*/
public static String toString(Boolean bool, String trueString, String falseString, String nullString) {
if (bool == null) {
return nullString;
}
return bool.booleanValue() ? trueString : falseString;
}
}
Converts a boolean to a String returning "yes" or "no"
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a boolean to a String returning <code>"yes"</code>
* or <code>"no"</code>.</p>
*
* <pre>
* BooleanUtils.toStringYesNo(true) = "yes"
* BooleanUtils.toStringYesNo(false) = "no"
* </pre>
*
* @param bool the Boolean to check
* @return <code>"yes"</code>, <code>"no"</code>,
* or <code>null</code>
*/
public static String toStringYesNo(boolean bool) {
return toString(bool, "yes", "no");
}
/**
* <p>Converts a boolean to a String returning one of the input Strings.</p>
*
* <pre>
* BooleanUtils.toString(true, "true", "false") = "true"
* BooleanUtils.toString(false, "true", "false") = "false"
* </pre>
*
* @param bool the Boolean to check
* @param trueString the String to return if <code>true</code>,
* may be <code>null</code>
* @param falseString the String to return if <code>false</code>,
* may be <code>null</code>
* @return one of the two input Strings
*/
public static String toString(boolean bool, String trueString, String falseString) {
return bool ? trueString : falseString;
}
}
Converts an Integer to a Boolean specifying the conversion values.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts an Integer to a Boolean specifying the conversion values.</p>
*
* <pre>
* BooleanUtils.toBooleanObject(new Integer(0), new Integer(0), new Integer(2), new Integer(3)) = Boolean.TRUE
* BooleanUtils.toBooleanObject(new Integer(2), new Integer(1), new Integer(2), new Integer(3)) = Boolean.FALSE
* BooleanUtils.toBooleanObject(new Integer(3), new Integer(1), new Integer(2), new Integer(3)) = null
* </pre>
*
* @param value the Integer to convert
* @param trueValue the value to match for <code>true</code>,
* may be <code>null</code>
* @param falseValue the value to match for <code>false</code>,
* may be <code>null</code>
* @param nullValue the value to to match for <code>null</code>,
* may be <code>null</code>
* @return Boolean.TRUE, Boolean.FALSE, or <code>null</code>
* @throws IllegalArgumentException if no match
*/
public static Boolean toBooleanObject(Integer value, Integer trueValue, Integer falseValue, Integer nullValue) {
if (value == null) {
if (trueValue == null) {
return Boolean.TRUE;
} else if (falseValue == null) {
return Boolean.FALSE;
} else if (nullValue == null) {
return null;
}
} else if (value.equals(trueValue)) {
return Boolean.TRUE;
} else if (value.equals(falseValue)) {
return Boolean.FALSE;
} else if (value.equals(nullValue)) {
return null;
}
// no match
throw new IllegalArgumentException("The Integer did not match any specified value");
}
}
Converts an Integer to a Boolean using the convention that zero is false
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts an Integer to a Boolean using the convention that <code>zero</code>
* is <code>false</code>.</p>
*
* <p><code>null</code> will be converted to <code>null</code>.</p>
*
* <pre>
* BooleanUtils.toBoolean(new Integer(0)) = Boolean.FALSE
* BooleanUtils.toBoolean(new Integer(1)) = Boolean.TRUE
* BooleanUtils.toBoolean(new Integer(null)) = null
* </pre>
*
* @param value the Integer to convert
* @return Boolean.TRUE if non-zero, Boolean.FALSE if zero,
* <code>null</code> if <code>null</code> input
*/
public static Boolean toBooleanObject(Integer value) {
if (value == null) {
return null;
}
return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
}
}
Converts a String to a Boolean.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a String to a Boolean.</p>
*
* <p><code>"true"</code>, <code>"on"</code> or <code>"yes"</code>
* (case insensitive) will return <code>true</code>.
* <code>"false"</code>, <code>"off"</code> or <code>"no"</code>
* (case insensitive) will return <code>false</code>.
* Otherwise, <code>null</code> is returned.</p>
*
* <pre>
* BooleanUtils.toBooleanObject(null) = null
* BooleanUtils.toBooleanObject("true") = Boolean.TRUE
* BooleanUtils.toBooleanObject("false") = Boolean.FALSE
* BooleanUtils.toBooleanObject("on") = Boolean.TRUE
* BooleanUtils.toBooleanObject("ON") = Boolean.TRUE
* BooleanUtils.toBooleanObject("off") = Boolean.FALSE
* BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE
* BooleanUtils.toBooleanObject("blue") = null
* </pre>
*
* @param str the String to check
* @return the Boolean value of the string,
* <code>null</code> if no match or <code>null</code> input
*/
public static Boolean toBooleanObject(String str) {
if ("true".equalsIgnoreCase(str)) {
return Boolean.TRUE;
} else if ("false".equalsIgnoreCase(str)) {
return Boolean.FALSE;
} else if ("on".equalsIgnoreCase(str)) {
return Boolean.TRUE;
} else if ("off".equalsIgnoreCase(str)) {
return Boolean.FALSE;
} else if ("yes".equalsIgnoreCase(str)) {
return Boolean.TRUE;
} else if ("no".equalsIgnoreCase(str)) {
return Boolean.FALSE;
}
// no match
return null;
}
}
Converts a String to a boolean (optimised for performance).
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a String to a boolean (optimised for performance).</p>
*
* <p><code>"true"</code>, <code>"on"</code> or <code>"yes"</code>
* (case insensitive) will return <code>true</code>. Otherwise,
* <code>false</code> is returned.</p>
*
* <p>This method performs 4 times faster (JDK1.4) than
* <code>Boolean.valueOf(String)</code>. However, this method accepts
* "on" and "yes" as true values.
*
* <pre>
* BooleanUtils.toBoolean(null) = false
* BooleanUtils.toBoolean("true") = true
* BooleanUtils.toBoolean("TRUE") = true
* BooleanUtils.toBoolean("tRUe") = true
* BooleanUtils.toBoolean("on") = true
* BooleanUtils.toBoolean("yes") = true
* BooleanUtils.toBoolean("false") = false
* BooleanUtils.toBoolean("x gti") = false
* </pre>
*
* @param str the String to check
* @return the boolean value of the string, <code>false</code> if no match
*/
public static boolean toBoolean(String str) {
// Previously used equalsIgnoreCase, which was fast for interned "true".
// Non interned "true" matched 15 times slower.
//
// Optimisation provides same performance as before for interned "true".
// Similar performance for null, "false", and other strings not length 2/3/4.
// "true"/"TRUE" match 4 times slower, "tRUE"/"True" 7 times slower.
if (str == "true") {
return true;
}
if (str == null) {
return false;
}
switch (str.length()) {
case 2: {
char ch0 = str.charAt(0);
char ch1 = str.charAt(1);
return
(ch0 == "o" || ch0 == "O") &&
(ch1 == "n" || ch1 == "N");
}
case 3: {
char ch = str.charAt(0);
if (ch == "y") {
return
(str.charAt(1) == "e" || str.charAt(1) == "E") &&
(str.charAt(2) == "s" || str.charAt(2) == "S");
}
if (ch == "Y") {
return
(str.charAt(1) == "E" || str.charAt(1) == "e") &&
(str.charAt(2) == "S" || str.charAt(2) == "s");
}
return false;
}
case 4: {
char ch = str.charAt(0);
if (ch == "t") {
return
(str.charAt(1) == "r" || str.charAt(1) == "R") &&
(str.charAt(2) == "u" || str.charAt(2) == "U") &&
(str.charAt(3) == "e" || str.charAt(3) == "E");
}
if (ch == "T") {
return
(str.charAt(1) == "R" || str.charAt(1) == "r") &&
(str.charAt(2) == "U" || str.charAt(2) == "u") &&
(str.charAt(3) == "E" || str.charAt(3) == "e");
}
}
}
return false;
}
}
Converts a String to a Boolean throwing an exception if no match.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a String to a Boolean throwing an exception if no match.</p>
*
* <pre>
* BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE
* BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE
* BooleanUtils.toBooleanObject("null", "true", "false", "null") = null
* </pre>
*
* @param str the String to check
* @param trueString the String to match for <code>true</code>
* (case sensitive), may be <code>null</code>
* @param falseString the String to match for <code>false</code>
* (case sensitive), may be <code>null</code>
* @param nullString the String to match for <code>null</code>
* (case sensitive), may be <code>null</code>
* @return the Boolean value of the string,
* <code>null</code> if either the String matches <code>nullString</code>
* or if <code>null</code> input and <code>nullString</code> is
* <code>null</code>
* @throws IllegalArgumentException if the String doesn"t match
*/
public static Boolean toBooleanObject(String str, String trueString, String falseString, String nullString) {
if (str == null) {
if (trueString == null) {
return Boolean.TRUE;
} else if (falseString == null) {
return Boolean.FALSE;
} else if (nullString == null) {
return null;
}
} else if (str.equals(trueString)) {
return Boolean.TRUE;
} else if (str.equals(falseString)) {
return Boolean.FALSE;
} else if (str.equals(nullString)) {
return null;
}
// no match
throw new IllegalArgumentException("The String did not match any specified value");
}
}
Converts a String to a Boolean throwing an exception if no match found.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Converts a String to a Boolean throwing an exception if no match found.</p>
*
* <p>null is returned if there is no match.</p>
*
* <pre>
* BooleanUtils.toBoolean("true", "true", "false") = true
* BooleanUtils.toBoolean("false", "true", "false") = false
* </pre>
*
* @param str the String to check
* @param trueString the String to match for <code>true</code>
* (case sensitive), may be <code>null</code>
* @param falseString the String to match for <code>false</code>
* (case sensitive), may be <code>null</code>
* @return the boolean value of the string
* @throws IllegalArgumentException if the String doesn"t match
*/
public static boolean toBoolean(String str, String trueString, String falseString) {
if (str == null) {
if (trueString == null) {
return true;
} else if (falseString == null) {
return false;
}
} else if (str.equals(trueString)) {
return true;
} else if (str.equals(falseString)) {
return false;
}
// no match
throw new IllegalArgumentException("The String did not match either specified value");
}
}
Convert String to Boolean
public class Main {
public static void main(String[] args) {
String strBoolean = "true";
//String to boolean conversion
boolean theValue = Boolean.parseBoolean(strBoolean);
System.out.println(theValue);
}
}
//true
Create a boolean variable from string
public class Main {
public static void main(String[] args) {
// Parsing string "true" will result boolean true
boolean boolA = Boolean.parseBoolean("true");
System.out.println("boolA = " + boolA);
// Parsing string "TRUE" also resutl boolean true
boolean boolB = Boolean.parseBoolean("TRUE");
System.out.println("boolB = " + boolB);
}
}
Create an Boolean object from boolean value
public class Main {
public static void main(String[] args) {
Boolean blnObj1 = new Boolean(true);
Boolean blnObj2 = new Boolean("false");
System.out.println(blnObj1);
System.out.println(blnObj2);
}
}
/*
true
false
*/
Java boolean value
public class Main {
public static void main(String[] args) {
boolean b1 = true;
boolean b2 = false;
boolean b3 = (10 > 2) ? true : false;
System.out.println("Value of boolean variable b1 is :" + b1);
System.out.println("Value of boolean variable b2 is :" + b2);
System.out.println("Value of boolean variable b3 is :" + b3);
}
}
/*
Value of boolean variable b1 is :true
Value of boolean variable b2 is :false
Value of boolean variable b3 is :true
*/
Looping Forever with true value
public class MainClass {
public static void main(String[] args) {
int number = 2;
while (true) {
System.out.print(number + " ");
number += 2;
}
}
}
Performs an xor on an array of Booleans.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Performs an xor on a set of booleans.</p>
*
* <pre>
* BooleanUtils.xor(new boolean[] { true, true }) = false
* BooleanUtils.xor(new boolean[] { false, false }) = false
* BooleanUtils.xor(new boolean[] { true, false }) = true
* </pre>
*
* @param array an array of <code>boolean<code>s
* @return <code>true</code> if the xor is successful.
* @throws IllegalArgumentException if <code>array</code> is <code>null</code>
* @throws IllegalArgumentException if <code>array</code> is empty.
*/
public static boolean xor(boolean[] array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array is empty");
}
// Loops through array, comparing each item
int trueCount = 0;
for (int i = 0; i < array.length; i++) {
// If item is true, and trueCount is < 1, increments count
// Else, xor fails
if (array[i]) {
if (trueCount < 1) {
trueCount++;
} else {
return false;
}
}
}
// Returns true if there was exactly 1 true item
return trueCount == 1;
}
/**
* <p>Performs an xor on an array of Booleans.</p>
*
* <pre>
* BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE
* BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE
* BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE
* </pre>
*
* @param array an array of <code>Boolean<code>s
* @return <code>true</code> if the xor is successful.
* @throws IllegalArgumentException if <code>array</code> is <code>null</code>
* @throws IllegalArgumentException if <code>array</code> is empty.
* @throws IllegalArgumentException if <code>array</code> contains a <code>null</code>
*/
public static Boolean xor(Boolean[] array) {
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array is empty");
}
boolean[] primitive = null;
try {
primitive = toPrimitive(array);
} catch (NullPointerException ex) {
throw new IllegalArgumentException("The array must not contain any null elements");
}
return xor(primitive) ? Boolean.TRUE : Boolean.FALSE;
}
/**
* <p>Converts an array of object Booleans to primitives.</p>
*
* <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
*
* @param array a <code>Boolean</code> array, may be <code>null</code>
* @return a <code>boolean</code> array, <code>null</code> if null array input
* @throws NullPointerException if array content is <code>null</code>
*/
public static boolean[] toPrimitive(Boolean[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return new boolean[0];
}
final boolean[] result = new boolean[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i].booleanValue();
}
return result;
}
}
Performs an xor on a set of booleans.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>Operations on boolean primitives and Boolean objects.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Gary Gregory
* @since 2.0
* @version $Id: BooleanUtils.java 589050 2007-10-27 05:07:45Z bayard $
*/
public class Main {
/**
* <p>Performs an xor on a set of booleans.</p>
*
* <pre>
* BooleanUtils.xor(new boolean[] { true, true }) = false
* BooleanUtils.xor(new boolean[] { false, false }) = false
* BooleanUtils.xor(new boolean[] { true, false }) = true
* </pre>
*
* @param array an array of <code>boolean<code>s
* @return <code>true</code> if the xor is successful.
* @throws IllegalArgumentException if <code>array</code> is <code>null</code>
* @throws IllegalArgumentException if <code>array</code> is empty.
*/
public static boolean xor(boolean[] array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array is empty");
}
// Loops through array, comparing each item
int trueCount = 0;
for (int i = 0; i < array.length; i++) {
// If item is true, and trueCount is < 1, increments count
// Else, xor fails
if (array[i]) {
if (trueCount < 1) {
trueCount++;
} else {
return false;
}
}
}
// Returns true if there was exactly 1 true item
return trueCount == 1;
}
/**
* <p>Performs an xor on an array of Booleans.</p>
*
* <pre>
* BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE
* BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE
* BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE
* </pre>
*
* @param array an array of <code>Boolean<code>s
* @return <code>true</code> if the xor is successful.
* @throws IllegalArgumentException if <code>array</code> is <code>null</code>
* @throws IllegalArgumentException if <code>array</code> is empty.
* @throws IllegalArgumentException if <code>array</code> contains a <code>null</code>
*/
public static Boolean xor(Boolean[] array) {
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array is empty");
}
boolean[] primitive = null;
try {
primitive = toPrimitive(array);
} catch (NullPointerException ex) {
throw new IllegalArgumentException("The array must not contain any null elements");
}
return xor(primitive) ? Boolean.TRUE : Boolean.FALSE;
}
/**
* <p>Converts an array of object Booleans to primitives.</p>
*
* <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
*
* @param array a <code>Boolean</code> array, may be <code>null</code>
* @return a <code>boolean</code> array, <code>null</code> if null array input
* @throws NullPointerException if array content is <code>null</code>
*/
public static boolean[] toPrimitive(Boolean[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return new boolean[0];
}
final boolean[] result = new boolean[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i].booleanValue();
}
return result;
}
}
Use toString method of Boolean class to convert it into String
public class Main {
public static void main(String[] args) {
Boolean blnObj = new Boolean("true");
System.out.println(blnObj.toString());
}
}