Java BigDecimal bigDecimalObjectValue(Object input)

Here you can find the source of bigDecimalObjectValue(Object input)

Description

Convert an object to a byte, allow nulls

License

Apache License

Parameter

Parameter Description
input a parameter

Return

the boolean object value

Declaration

public static BigDecimal bigDecimalObjectValue(Object input) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Internet2/*  w ww  .  ja  v  a2  s  .  c  om*/
 * 
 * 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.lang.reflect.Array;

import java.math.BigDecimal;

import java.util.Collection;

import java.util.Map;

public class Main {
    /**
     * Convert an object to a byte, allow nulls
     * @param input
     * @return the boolean object value
     */
    public static BigDecimal bigDecimalObjectValue(Object input) {
        if (input instanceof BigDecimal) {
            return (BigDecimal) input;
        }
        if (isBlank(input)) {
            return null;
        }
        return BigDecimal.valueOf(doubleValue(input));
    }

    /**
     * See if the input is null or if string, if it is empty or blank (whitespace)
     * @param input
     * @return true if blank
     */
    public static boolean isBlank(Object input) {
        if (null == input) {
            return true;
        }
        return (input instanceof String && isBlank((String) input));
    }

    /**
     * <p>Checks if a String is whitespace, empty ("") or null.</p>
     *
     * <pre>
     * isBlank(null)      = true
     * isBlank("")        = true
     * isBlank(" ")       = true
     * isBlank("bob")     = false
     * isBlank("  bob  ") = false
     * </pre>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is null, empty or whitespace
     * @since 2.0
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    /**
     * get the double value of an object
     * 
     * @param input
     *          is a number or String
     * 
     * @return the double equivalent
     */
    public static double doubleValue(Object input) {
        if (input instanceof String) {
            String string = (String) input;
            return Double.parseDouble(string);
        }
        if (input instanceof Number) {
            return ((Number) input).doubleValue();
        }
        throw new RuntimeException("Cannot convert to double: "
                + className(input));
    }

    /**
     * Null safe array length or map
     * 
     * @param arrayOrCollection
     * @return the length of the array (0 for null)
     */
    public static int length(Object arrayOrCollection) {
        if (arrayOrCollection == null) {
            return 0;
        }
        if (arrayOrCollection.getClass().isArray()) {
            return Array.getLength(arrayOrCollection);
        }
        if (arrayOrCollection instanceof Collection) {
            return ((Collection) arrayOrCollection).size();
        }
        if (arrayOrCollection instanceof Map) {
            return ((Map) arrayOrCollection).size();
        }
        // simple non array non collection object
        return 1;
    }

    /**
     * null safe classname method, gets the unenhanced name
     * 
     * @param object
     * @return the classname
     */
    public static String className(Object object) {
        return object == null ? null : object.getClass().getName();
    }
}

Related

  1. areEqual(BigDecimal first, BigDecimal second)
  2. asArray(final BigDecimal... elements)
  3. average(BigDecimal... values)
  4. average(final BigDecimal... numbers)
  5. bigDecimalListToArray(List list)
  6. bigDecimalParse(String value)
  7. bigDecimalPrint(BigDecimal value)
  8. bigDecimalProperty(ObjectNode node, String propertyName, BigDecimal propertyValue)
  9. byteOverflow(BigDecimal value)