Java/Data Type/boolean

Материал из Java эксперт
Перейти к: навигация, поиск

Содержание

Autoboxing/unboxing a Boolean and Character.

   <source lang="java">
    
  

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);
 }

}



 </source>
   
  
 
  



Boolean And Or

   <source lang="java">
   

/*

* 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();
   }
 }

}




 </source>
   
  
 
  



Boolean class creates primitives that wrap themselves around data items of the boolean data type

   <source lang="java">
    
    

class MainClass {

 public static void main(String[] args) {
   boolean b = true;
   Boolean b2 = new Boolean(b);
   System.out.println(b2.booleanValue());
 }

}



 </source>
   
  
 
  



Boolean Data Type

   <source lang="java">
   

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);
 }

}



 </source>
   
  
 
  



Boolean factory that avoids creating new Boolean objecs all the time.

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Boolean factory that avoids creating new Boolean objecs all the time.

  * 
*

This method was added to JDK1.4 but is available here for earlier JDKs.

  *
*
   *   BooleanUtils.toBooleanObject(false) = Boolean.FALSE
   *   BooleanUtils.toBooleanObject(true)  = Boolean.TRUE
   * 
  *
  * @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;
 }

}



 </source>
   
  
 
  



Checks if a Boolean value is false, handling null by returning false

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Checks if a Boolean value is false, * handling null by returning false.

  *
*
   *   BooleanUtils.isFalse(Boolean.TRUE)  = false
   *   BooleanUtils.isFalse(Boolean.FALSE) = true
   *   BooleanUtils.isFalse(null)          = false
   * 
  *
  * @param bool  the boolean to check, null returns false
  * @return true 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;
 }

}



 </source>
   
  
 
  



Checks if a Boolean value is not false, handling null by returning true

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Checks if a Boolean value is not false, * handling null by returning true.

  *
*
   *   BooleanUtils.isNotFalse(Boolean.TRUE)  = true
   *   BooleanUtils.isNotFalse(Boolean.FALSE) = false
   *   BooleanUtils.isNotFalse(null)          = true
   * 
  *
  * @param bool  the boolean to check, null returns true
  * @return true if the input is null or true
  * @since 2.3
  */
 public static boolean isNotFalse(Boolean bool) {
     return !isFalse(bool);
 }
 /**
*

Checks if a Boolean value is false, * handling null by returning false.

  *
*
   *   BooleanUtils.isFalse(Boolean.TRUE)  = false
   *   BooleanUtils.isFalse(Boolean.FALSE) = true
   *   BooleanUtils.isFalse(null)          = false
   * 
  *
  * @param bool  the boolean to check, null returns false
  * @return true 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;
 }

}



 </source>
   
  
 
  



Checks if a Boolean value is not true, handling null by returning true

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Checks if a Boolean value is not true, * handling null by returning true.

  *
*
   *   BooleanUtils.isNotTrue(Boolean.TRUE)  = false
   *   BooleanUtils.isNotTrue(Boolean.FALSE) = true
   *   BooleanUtils.isNotTrue(null)          = true
   * 
  *
  * @param bool  the boolean to check, null returns true
  * @return true if the input is null or false
  * @since 2.3
  */
 public static boolean isNotTrue(Boolean bool) {
     return !isTrue(bool);
 }
 /**
*

Checks if a Boolean value is true, * handling null by returning false.

  *
*
   *   BooleanUtils.isTrue(Boolean.TRUE)  = true
   *   BooleanUtils.isTrue(Boolean.FALSE) = false
   *   BooleanUtils.isTrue(null)          = false
   * 
  *
  * @param bool  the boolean to check, null returns false
  * @return true 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;
 }

}



 </source>
   
  
 
  



Checks if a Boolean value is true, handling null by returning false

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Checks if a Boolean value is true, * handling null by returning false.

  *
*
   *   BooleanUtils.isTrue(Boolean.TRUE)  = true
   *   BooleanUtils.isTrue(Boolean.FALSE) = false
   *   BooleanUtils.isTrue(null)          = false
   * 
  *
  * @param bool  the boolean to check, null returns false
  * @return true 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;
 }

}



 </source>
   
  
 
  



Compare Two Java boolean Arrays Example

   <source lang="java">
   

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));
 }

}



 </source>
   
  
 
  



Convert boolean to integer

   <source lang="java">
   

public class Main {

 public static void main(String[] args) throws Exception {
   boolean b = true;
   int i = (b) ? 1 : 0;
 }

}



 </source>
   
  
 
  



Convert Boolean to String

   <source lang="java">
   

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



 </source>
   
  
 
  



Convert boolean value to Boolean

   <source lang="java">
   

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);
 }

}



 </source>
   
  
 
  



Converting a string to a boolean

   <source lang="java">
  

/*

* 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;
           }
       }
   }

}


 </source>
   
  
 
  



Convert integer to boolean

   <source lang="java">
   

public class Main {

 public static void main(String[] args) throws Exception {
   int i=10;
   boolean b = (i != 0);
   System.out.println(b);
 }

} //true



 </source>
   
  
 
  



Convert Java boolean Primitive to Boolean object

   <source lang="java">
   

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);
 }

}



 </source>
   
  
 
  



Convert Java String Object to Boolean Object

   <source lang="java">
   

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);
 }

}



 </source>
   
  
 
  



Converts a Boolean to a boolean handling null

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a Boolean to a boolean handling null.

  * 
*
   *   BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true
   *   BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false
   *   BooleanUtils.toBooleanDefaultIfNull(null, true)          = true
   * 
  *
  * @param bool  the boolean to convert
  * @param valueIfNull  the boolean value to return if null
  * @return true or false
  */
 public static boolean toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull) {
     if (bool == null) {
         return valueIfNull;
     }
     return bool.booleanValue() ? true : false;
 }

}



 </source>
   
  
 
  



Converts a Boolean to a boolean handling null by returning false

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a Boolean to a boolean handling null * by returning false.

  *
*
   *   BooleanUtils.toBoolean(Boolean.TRUE)  = true
   *   BooleanUtils.toBoolean(Boolean.FALSE) = false
   *   BooleanUtils.toBoolean(null)          = false
   * 
  *
  * @param bool  the boolean to convert
  * @return true or false, 
  *  null returns false
  */
 public static boolean toBoolean(Boolean bool) {
     if (bool == null) {
         return false;
     }
     return bool.booleanValue() ? true : false;
 }

}



 </source>
   
  
 
  



Converts a Boolean to a Integer using the convention that zero is false

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a Boolean to a Integer using the convention that * zero is false.

  *
*

null will be converted to null.

  *
*
   *   BooleanUtils.toIntegerObject(Boolean.TRUE)  = new Integer(1)
   *   BooleanUtils.toIntegerObject(Boolean.FALSE) = new Integer(0)
   * 
  *
  * @param bool  the Boolean to convert
  * @return one if Boolean.TRUE, zero if Boolean.FALSE, null if null
  */
 public static Integer toIntegerObject(Boolean bool) {
     if (bool == null) {
         return null;
     }
     return bool.booleanValue() ? 1 : 0;
 }
 

}



 </source>
   
  
 
  



Converts a Boolean to an Integer specifying the conversion values.

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a Boolean to an Integer specifying the conversion values.

  * 
*
   *   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)
   * 
  *
  * @param bool  the Boolean to convert
  * @param trueValue  the value to return if true,
  *  may be null
  * @param falseValue  the value to return if false,
  *  may be null
  * @param nullValue  the value to return if null,
  *  may be null
  * @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;
 }

}



 </source>
   
  
 
  



Converts a boolean to an Integer using the convention that zero is false

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a boolean to an Integer using the convention that * zero is false.

  * 
*
   *   BooleanUtils.toIntegerObject(true)  = new Integer(1)
   *   BooleanUtils.toIntegerObject(false) = new Integer(0)
   * 
  *
  * @param bool  the boolean to convert
  * @return one if true, zero if false
  */
 public static Integer toIntegerObject(boolean bool) {
     return bool ? 1 : 0;
 }
 

}



 </source>
   
  
 
  



Converts a Boolean to an int specifying the conversion values.

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a Boolean to an int specifying the conversion values.

  * 
*
   *   BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2)  = 1
   *   BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0
   *   BooleanUtils.toInteger(null, 1, 0, 2)          = 2
   * 
  *
  * @param bool  the Boolean to convert
  * @param trueValue  the value to return if true
  * @param falseValue  the value to return if false
  * @param nullValue  the value to return if null
  * @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;
 }
 

}



 </source>
   
  
 
  



Converts a boolean to an int using the convention that zero is false

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a boolean to an int using the convention that * zero is false.

  *
*
   *   BooleanUtils.toInteger(true)  = 1
   *   BooleanUtils.toInteger(false) = 0
   * 
  *
  * @param bool  the boolean to convert
  * @return one if true, zero if false
  */
 public static int toInteger(boolean bool) {
     return bool ? 1 : 0;
 }

}



 </source>
   
  
 
  



Converts a Boolean to a String returning one of the input Strings.

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a Boolean to a String returning one of the input Strings.

  * 
*
   *   BooleanUtils.toString(Boolean.TRUE, "true", "false", null)   = "true"
   *   BooleanUtils.toString(Boolean.FALSE, "true", "false", null)  = "false"
   *   BooleanUtils.toString(null, "true", "false", null)           = null;
   * 
  *
  * @param bool  the Boolean to check
  * @param trueString  the String to return if true,
  *  may be null
  * @param falseString  the String to return if false,
  *  may be null
  * @param nullString  the String to return if null,
  *  may be null
  * @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;
 }

}



 </source>
   
  
 
  



Converts a Boolean to a String returning "on", "off", or null.

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {


 /**
*

Converts a Boolean to a String returning "on", * "off", or null.

  * 
*
   *   BooleanUtils.toStringOnOff(Boolean.TRUE)  = "on"
   *   BooleanUtils.toStringOnOff(Boolean.FALSE) = "off"
   *   BooleanUtils.toStringOnOff(null)          = null;
   * 
  *
  * @param bool  the Boolean to check
  * @return "on", "off",
  *  or null
  */
 public static String toStringOnOff(Boolean bool) {
     return toString(bool, "on", "off", null);
 }
 /**
*

Converts a Boolean to a String returning one of the input Strings.

  * 
*
   *   BooleanUtils.toString(Boolean.TRUE, "true", "false", null)   = "true"
   *   BooleanUtils.toString(Boolean.FALSE, "true", "false", null)  = "false"
   *   BooleanUtils.toString(null, "true", "false", null)           = null;
   * 
  *
  * @param bool  the Boolean to check
  * @param trueString  the String to return if true,
  *  may be null
  * @param falseString  the String to return if false,
  *  may be null
  * @param nullString  the String to return if null,
  *  may be null
  * @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;
 }

}



 </source>
   
  
 
  



Converts a boolean to a String returning "on" or "off"

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a boolean to a String returning "on" * or "off".

  * 
*
   *   BooleanUtils.toStringOnOff(true)   = "on"
   *   BooleanUtils.toStringOnOff(false)  = "off"
   * 
  *
  * @param bool  the Boolean to check
  * @return "on", "off",
  *  or null
  */
 public static String toStringOnOff(boolean bool) {
     return toString(bool, "on", "off");
 }
 
 
 /**
*

Converts a boolean to a String returning one of the input Strings.

  * 
*
   *   BooleanUtils.toString(true, "true", "false")   = "true"
   *   BooleanUtils.toString(false, "true", "false")  = "false"
   * 
  *
  * @param bool  the Boolean to check
  * @param trueString  the String to return if true,
  *  may be null
  * @param falseString  the String to return if false,
  *  may be null
  * @return one of the two input Strings
  */
 public static String toString(boolean bool, String trueString, String falseString) {
     return bool ? trueString : falseString;
 }

}



 </source>
   
  
 
  



Converts a Boolean to a String returning "true", "false", or null

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a Boolean to a String returning "true", * "false", or null.

  * 
*
   *   BooleanUtils.toStringTrueFalse(Boolean.TRUE)  = "true"
   *   BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false"
   *   BooleanUtils.toStringTrueFalse(null)          = null;
   * 
  *
  * @param bool  the Boolean to check
  * @return "true", "false",
  *  or null
  */
 public static String toStringTrueFalse(Boolean bool) {
     return toString(bool, "true", "false", null);
 }
 /**
*

Converts a Boolean to a String returning one of the input Strings.

  * 
*
   *   BooleanUtils.toString(Boolean.TRUE, "true", "false", null)   = "true"
   *   BooleanUtils.toString(Boolean.FALSE, "true", "false", null)  = "false"
   *   BooleanUtils.toString(null, "true", "false", null)           = null;
   * 
  *
  * @param bool  the Boolean to check
  * @param trueString  the String to return if true,
  *  may be null
  * @param falseString  the String to return if false,
  *  may be null
  * @param nullString  the String to return if null,
  *  may be null
  * @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;
 }

}



 </source>
   
  
 
  



Converts a boolean to a String returning "true" or "false"

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a boolean to a String returning "true" * or "false".

  * 
*
   *   BooleanUtils.toStringTrueFalse(true)   = "true"
   *   BooleanUtils.toStringTrueFalse(false)  = "false"
   * 
  *
  * @param bool  the Boolean to check
  * @return "true", "false",
  *  or null
  */
 public static String toStringTrueFalse(boolean bool) {
     return toString(bool, "true", "false");
 }
 
 
 /**
*

Converts a boolean to a String returning one of the input Strings.

  * 
*
   *   BooleanUtils.toString(true, "true", "false")   = "true"
   *   BooleanUtils.toString(false, "true", "false")  = "false"
   * 
  *
  * @param bool  the Boolean to check
  * @param trueString  the String to return if true,
  *  may be null
  * @param falseString  the String to return if false,
  *  may be null
  * @return one of the two input Strings
  */
 public static String toString(boolean bool, String trueString, String falseString) {
     return bool ? trueString : falseString;
 }

}



 </source>
   
  
 
  



Converts a Boolean to a String returning "yes", "no", or null

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a Boolean to a String returning "yes", * "no", or null.

  * 
*
   *   BooleanUtils.toStringYesNo(Boolean.TRUE)  = "yes"
   *   BooleanUtils.toStringYesNo(Boolean.FALSE) = "no"
   *   BooleanUtils.toStringYesNo(null)          = null;
   * 
  *
  * @param bool  the Boolean to check
  * @return "yes", "no",
  *  or null
  */
 public static String toStringYesNo(Boolean bool) {
     return toString(bool, "yes", "no", null);
 }
 /**
*

Converts a Boolean to a String returning one of the input Strings.

  * 
*
   *   BooleanUtils.toString(Boolean.TRUE, "true", "false", null)   = "true"
   *   BooleanUtils.toString(Boolean.FALSE, "true", "false", null)  = "false"
   *   BooleanUtils.toString(null, "true", "false", null)           = null;
   * 
  *
  * @param bool  the Boolean to check
  * @param trueString  the String to return if true,
  *  may be null
  * @param falseString  the String to return if false,
  *  may be null
  * @param nullString  the String to return if null,
  *  may be null
  * @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;
 }

}



 </source>
   
  
 
  



Converts a boolean to a String returning "yes" or "no"

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a boolean to a String returning "yes" * or "no".

  * 
*
   *   BooleanUtils.toStringYesNo(true)   = "yes"
   *   BooleanUtils.toStringYesNo(false)  = "no"
   * 
  *
  * @param bool  the Boolean to check
  * @return "yes", "no",
  *  or null
  */
 public static String toStringYesNo(boolean bool) {
     return toString(bool, "yes", "no");
 }
 
 /**
*

Converts a boolean to a String returning one of the input Strings.

  * 
*
   *   BooleanUtils.toString(true, "true", "false")   = "true"
   *   BooleanUtils.toString(false, "true", "false")  = "false"
   * 
  *
  * @param bool  the Boolean to check
  * @param trueString  the String to return if true,
  *  may be null
  * @param falseString  the String to return if false,
  *  may be null
  * @return one of the two input Strings
  */
 public static String toString(boolean bool, String trueString, String falseString) {
     return bool ? trueString : falseString;
 }

}



 </source>
   
  
 
  



Converts an Integer to a Boolean specifying the conversion values.

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts an Integer to a Boolean specifying the conversion values.

  * 
*
   *   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
   * 
  *
  * @param value  the Integer to convert
  * @param trueValue  the value to match for true,
  *  may be null
  * @param falseValue  the value to match for false,
  *  may be null
  * @param nullValue  the value to to match for null,
  *  may be null
  * @return Boolean.TRUE, Boolean.FALSE, or null
  * @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");
 }

}



 </source>
   
  
 
  



Converts an Integer to a Boolean using the convention that zero is false

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts an Integer to a Boolean using the convention that zero * is false.

  * 
*

null will be converted to null.

  *
*
   *   BooleanUtils.toBoolean(new Integer(0))    = Boolean.FALSE
   *   BooleanUtils.toBoolean(new Integer(1))    = Boolean.TRUE
   *   BooleanUtils.toBoolean(new Integer(null)) = null
   * 
  *
  * @param value  the Integer to convert
  * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero,
  *  null if null input
  */
 public static Boolean toBooleanObject(Integer value) {
     if (value == null) {
         return null;
     }
     return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
 }

}



 </source>
   
  
 
  



Converts a String to a Boolean.

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a String to a Boolean.

  * 
*

"true", "on" or "yes" * (case insensitive) will return true. * "false", "off" or "no" * (case insensitive) will return false. * Otherwise, null is returned.

  *
*
   *   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
   * 
  *
  * @param str  the String to check
  * @return the Boolean value of the string,
  *  null if no match or null 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;
 }

}



 </source>
   
  
 
  



Converts a String to a boolean (optimised for performance).

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a String to a boolean (optimised for performance).

  * 
*

"true", "on" or "yes" * (case insensitive) will return true. Otherwise, * false is returned.

  * 
*

This method performs 4 times faster (JDK1.4) than * Boolean.valueOf(String). However, this method accepts * "on" and "yes" as true values. * *

   *   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
   * 
  *
  * @param str  the String to check
  * @return the boolean value of the string, false 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;
 }

}



 </source>
   
  
 
  



Converts a String to a Boolean throwing an exception if no match.

   <source lang="java">
   

/*

* 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.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a String to a Boolean throwing an exception if no match.

  *
*
   *   BooleanUtils.toBooleanObject("true", "true", "false", "null")  = Boolean.TRUE
   *   BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE
   *   BooleanUtils.toBooleanObject("null", "true", "false", "null")  = null
   * 
  *
  * @param str  the String to check
  * @param trueString  the String to match for true
  *  (case sensitive), may be null
  * @param falseString  the String to match for false
  *  (case sensitive), may be null
  * @param nullString  the String to match for null
  *  (case sensitive), may be null
  * @return the Boolean value of the string,
  *  null if either the String matches nullString
  *  or if null input and nullString is
  *  null
  * @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");
 }

}



 </source>
   
  
 
  



Converts a String to a Boolean throwing an exception if no match found.

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Converts a String to a Boolean throwing an exception if no match found.

  * 
*

null is returned if there is no match.

  *
*
   *   BooleanUtils.toBoolean("true", "true", "false")  = true
   *   BooleanUtils.toBoolean("false", "true", "false") = false
   * 
  *
  * @param str  the String to check
  * @param trueString  the String to match for true
  *  (case sensitive), may be null
  * @param falseString  the String to match for false
  *  (case sensitive), may be null
  * @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");
 }

}



 </source>
   
  
 
  



Convert String to Boolean

   <source lang="java">
   

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



 </source>
   
  
 
  



Create a boolean variable from string

   <source lang="java">
   

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);
 }

}



 </source>
   
  
 
  



Create an Boolean object from boolean value

   <source lang="java">
   

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

  • /



 </source>
   
  
 
  



Java boolean value

   <source lang="java">
   

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

  • /



 </source>
   
  
 
  



Looping Forever with true value

   <source lang="java">
    

public class MainClass {

 public static void main(String[] args) {
   int number = 2;
   while (true) {
     System.out.print(number + " ");
     number += 2;
   }
 }

}



 </source>
   
  
 
  



Performs an xor on an array of Booleans.

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Performs an xor on a set of booleans.

  *
*
   *   BooleanUtils.xor(new boolean[] { true, true })   = false
   *   BooleanUtils.xor(new boolean[] { false, false }) = false
   *   BooleanUtils.xor(new boolean[] { true, false })  = true
   * 
  *
  * @param array  an array of boolean<code>s
  * @return <code>true if the xor is successful.
  * @throws IllegalArgumentException if array is null
  * @throws IllegalArgumentException if array 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;
 }
 /**
*

Performs an xor on an array of Booleans.

  * 
*
   *   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
   * 
  *
  * @param array  an array of Boolean<code>s
  * @return <code>true if the xor is successful.
  * @throws IllegalArgumentException if array is null
  * @throws IllegalArgumentException if array is empty.
  * @throws IllegalArgumentException if array contains a null
  */
 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;
 }
 /**
*

Converts an array of object Booleans to primitives.

  *
*

This method returns null for a null input array.

  * 
  * @param array  a Boolean array, may be null
  * @return a boolean array, null if null array input
  * @throws NullPointerException if array content is null
  */
 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;
 }

}



 </source>
   
  
 
  



Performs an xor on a set of booleans.

   <source lang="java">
   

/*

* 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.
*/

/**

*

Operations on boolean primitives and Boolean objects.

*
*

This class tries to handle null input gracefully. * An exception will not be thrown for a null input. * Each method documents its behaviour in more detail.

* 
* @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 {

 /**
*

Performs an xor on a set of booleans.

  *
*
   *   BooleanUtils.xor(new boolean[] { true, true })   = false
   *   BooleanUtils.xor(new boolean[] { false, false }) = false
   *   BooleanUtils.xor(new boolean[] { true, false })  = true
   * 
  *
  * @param array  an array of boolean<code>s
  * @return <code>true if the xor is successful.
  * @throws IllegalArgumentException if array is null
  * @throws IllegalArgumentException if array 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;
 }
 /**
*

Performs an xor on an array of Booleans.

  * 
*
   *   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
   * 
  *
  * @param array  an array of Boolean<code>s
  * @return <code>true if the xor is successful.
  * @throws IllegalArgumentException if array is null
  * @throws IllegalArgumentException if array is empty.
  * @throws IllegalArgumentException if array contains a null
  */
 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;
 }
 /**
*

Converts an array of object Booleans to primitives.

  *
*

This method returns null for a null input array.

  * 
  * @param array  a Boolean array, may be null
  * @return a boolean array, null if null array input
  * @throws NullPointerException if array content is null
  */
 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;
 }

}



 </source>
   
  
 
  



Use toString method of Boolean class to convert it into String

   <source lang="java">
   

public class Main {

 public static void main(String[] args) {
   Boolean blnObj = new Boolean("true");
   System.out.println(blnObj.toString());
 }

}



 </source>