Here you can find the source of toBoolean(Object propValue, boolean defaultValue)
defaultValue
if the parameter is null
.
Parameter | Description |
---|---|
propValue | the property value or <code>null</code> |
defaultValue | the default boolean value |
public static boolean toBoolean(Object propValue, boolean defaultValue)
//package com.java2s; /*//from w w w.ja va 2 s . c o m * 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. */ import java.util.Collection; public class Main { /** * Returns the boolean value of the parameter or the * <code>defaultValue</code> if the parameter is <code>null</code>. * If the parameter is not a <code>Boolean</code> it is converted * by calling <code>Boolean.valueOf</code> on the string value of the * object. * @param propValue the property value or <code>null</code> * @param defaultValue the default boolean value * @return Boolean value */ public static boolean toBoolean(Object propValue, boolean defaultValue) { propValue = toObject(propValue); if (propValue instanceof Boolean) { return (Boolean) propValue; } else if (propValue != null) { return Boolean.parseBoolean(String.valueOf(propValue)); } return defaultValue; } /** * Returns the parameter as a single value. If the * parameter is neither an array nor a <code>java.util.Collection</code> the * parameter is returned unmodified. If the parameter is a non-empty array, * the first array element is returned. If the property is a non-empty * <code>java.util.Collection</code>, the first collection element is returned. * Otherwise <code>null</code> is returned. * @param propValue the parameter to convert. * @return Object value */ public static Object toObject(Object propValue) { if (propValue == null) { return null; } else if (propValue.getClass().isArray()) { Object[] prop = (Object[]) propValue; return prop.length > 0 ? prop[0] : null; } else if (propValue instanceof Collection<?>) { Collection<?> prop = (Collection<?>) propValue; return prop.isEmpty() ? null : prop.iterator().next(); } else { return propValue; } } private static boolean isEmpty(String str) { return str == null || str.length() == 0; } }