Here you can find the source of toBoolean(final String source)
true
if the source string evaulates (case insensative and trimmed) to true
, yes
, or on
.
Parameter | Description |
---|---|
source | the string to check |
true
if the source string evaulates to true
or similar value, false
otherwise.
public static boolean toBoolean(final String source)
//package com.java2s; /*!/*from w w w. ja v a 2 s . c o m*/ * This program is free software; you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software * Foundation. * * You should have received a copy of the GNU Lesser General Public License along with this * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html * or from the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * Copyright (c) 2002-2013 Pentaho Corporation.. All rights reserved. */ public class Main { private static final String TRUE = "true"; private static final String YES = "yes"; private static final String ON = "on"; /** * Returns <code>true</code> if the source string evaulates (case insensative and trimmed) to <code>true</code>, * <code>yes</code>, or <code>on</code>. It will return <code>false</code> otherwise (including <code>null</code>). * * @param source the string to check * @return <code>true</code> if the source string evaulates to <code>true</code> or similar value, <code>false</code> * otherwise. */ public static boolean toBoolean(final String source) { return toBoolean(source, false); } /** * Returns <code>true</code> if the source string evaulates (case insensative and trimmed) to <code>true</code>, * <code>yes</code>, or <code>on</code>. It will return <code>false otherwise. If the source string is * <code>null</code>, it will return the value of the default. * * @param source the string to check * @param nullDefault to value to return if the source string is <code>null</code> * @return <code>true</code> if the source string evaulates to <code>true</code> or similar value, <code>false</code> * otherwise. */ public static boolean toBoolean(String source, final boolean nullDefault) { // If the source is null, use the default if (source == null) { return nullDefault; } // Check for valid values source = source.trim().toLowerCase(); return (TRUE.equals(source) || YES.equals(source) || ON.equals(source)); } /** * Trims a string: removes the leading and trailing spaces of a String. * * @param str The string to trim * @return The trimmed string. */ public static final String trim(String str) { if (str == null) { return null; } int max = str.length() - 1; int min = 0; while (min <= max && isSpace(str.charAt(min))) { min++; } while (max >= 0 && isSpace(str.charAt(max))) { max--; } if (max < min) { return ""; } return str.substring(min, max + 1); } /** * Determines if the two Strings are equals (taking nulls into account). * * @param s1 the first string to compare. * @param s2 the second string to compare. * @return <code>true</code> if both string are null or the contain the same value, <code>false</code>otherwise */ public static boolean equals(final String s1, final String s2) { return ((s1 == null && s2 == null) || (s1 != null && s1.equals(s2))); } /** * Determines whether or not a character is considered a space. A character is considered a space in Kettle if it is a * space, a tab, a newline or a cariage return. * * @param c The character to verify if it is a space. * @return true if the character is a space. false otherwise. */ public static final boolean isSpace(char c) { return c == ' ' || c == '\t' || c == '\r' || c == '\n' || Character.isWhitespace(c); } }