Java AtomicBoolean getBoolean(Object value)

Here you can find the source of getBoolean(Object value)

Description

Returns the boolean value of the given Object .

License

Apache License

Declaration

public static boolean getBoolean(Object value) 

Method Source Code


//package com.java2s;
/*/*from   w w  w . j  a v a2s.  c  o  m*/
 * Copyright 2010-2012 Roger Kapsi
 *
 *   Licensed 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.concurrent.atomic.AtomicBoolean;

public class Main {
    /**
     * Returns the {@code boolean} value of the given {@link Object}.
     */
    public static boolean getBoolean(Object value) {
        return getBoolean(value, false, false);
    }

    /**
     * Returns the {@code boolean} value of the given {@link Object}.
     */
    public static boolean getBoolean(Object value, boolean defaultValue) {
        return getBoolean(value, defaultValue, true);
    }

    /**
     * Returns the {@code boolean} value of the given {@link Object}.
     */
    private static boolean getBoolean(Object value, boolean defaultValue, boolean hasDefault) {

        if (value instanceof Boolean) {
            return ((Boolean) value).booleanValue();
        } else if (value instanceof AtomicBoolean) {
            return ((AtomicBoolean) value).get();
        }

        if (hasDefault) {
            return defaultValue;
        }

        throw new IllegalArgumentException("value=" + value);
    }
}

Related

  1. getUnifiedCity(@Nullable final String sCity, @Nonnull final Locale aSortLocale)
  2. isActivated()
  3. isBoolean(Class returnType)
  4. isCaseSensitive()