Java/Data Type/boolean — различия между версиями
(нет различий)
|
Версия 21: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.
<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 returnsfalse
* @returntrue
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
* @returntrue
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 returnsfalse
* @returntrue
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
* @returntrue
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 returnsfalse
* @returntrue
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 returnsfalse
* @returntrue
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 ifnull
* @returntrue
orfalse
*/ 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 * @returntrue
orfalse
, *null
returnsfalse
*/ 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
ifnull
*/ 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 iftrue
, * may benull
* @param falseValue the value to return iffalse
, * may benull
* @param nullValue the value to return ifnull
, * may benull
* @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 iftrue
, zero iffalse
*/ 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 iftrue
* @param falseValue the value to return iffalse
* @param nullValue the value to return ifnull
* @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 iftrue
, zero iffalse
*/ 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 iftrue
, * may benull
* @param falseString the String to return iffalse
, * may benull
* @param nullString the String to return ifnull
, * may benull
* @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"
, * ornull
*/ 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 iftrue
, * may benull
* @param falseString the String to return iffalse
, * may benull
* @param nullString the String to return ifnull
, * may benull
* @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"
, * ornull
*/ 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 iftrue
, * may benull
* @param falseString the String to return iffalse
, * may benull
* @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"
, * ornull
*/ 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 iftrue
, * may benull
* @param falseString the String to return iffalse
, * may benull
* @param nullString the String to return ifnull
, * may benull
* @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"
, * ornull
*/ 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 iftrue
, * may benull
* @param falseString the String to return iffalse
, * may benull
* @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"
, * ornull
*/ 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 iftrue
, * may benull
* @param falseString the String to return iffalse
, * may benull
* @param nullString the String to return ifnull
, * may benull
* @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"
, * ornull
*/ 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 iftrue
, * may benull
* @param falseString the String to return iffalse
, * may benull
* @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 fortrue
, * may benull
* @param falseValue the value to match forfalse
, * may benull
* @param nullValue the value to to match fornull
, * may benull
* @return Boolean.TRUE, Boolean.FALSE, ornull
* @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
ifnull
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 ornull
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 fortrue
* (case sensitive), may benull
* @param falseString the String to match forfalse
* (case sensitive), may benull
* @param nullString the String to match fornull
* (case sensitive), may benull
* @return the Boolean value of the string, *null
if either the String matchesnullString
* or ifnull
input andnullString
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 fortrue
* (case sensitive), may benull
* @param falseString the String to match forfalse
* (case sensitive), may benull
* @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 ifarray
isnull
* @throws IllegalArgumentException ifarray
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 ifarray
isnull
* @throws IllegalArgumentException ifarray
is empty. * @throws IllegalArgumentException ifarray
contains anull
*/ 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 aBoolean
array, may benull
* @return aboolean
array,null
if null array input * @throws NullPointerException if array content isnull
*/ 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 ifarray
isnull
* @throws IllegalArgumentException ifarray
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 ifarray
isnull
* @throws IllegalArgumentException ifarray
is empty. * @throws IllegalArgumentException ifarray
contains anull
*/ 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 aBoolean
array, may benull
* @return aboolean
array,null
if null array input * @throws NullPointerException if array content isnull
*/ 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>