
package com.heyu.api.data.utils;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @description: 数字类型工具类
 * @author: panhao
 * @version: V1.0
 * @date: 2019-10-24 17:20
 **/
@Slf4j
public class NumberUtils {
    //long类型 0 1 -1
    public static final Long LONG_ZERO = new Long(0L);
    public static final Long LONG_ONE = new Long(1L);
    public static final Long LONG_MINUS_ONE = new Long(-1L);

    //Integer类型 0 1 -1
    public static final Integer INTEGER_ZERO = new Integer(0);
    public static final Integer INTEGER_ONE = new Integer(1);
    public static final Integer INTEGER_MINUS_ONE = new Integer(-1);

    //Short类型 0 1 -1
    public static final Short SHORT_ZERO = new Short((short) 0);
    public static final Short SHORT_ONE = new Short((short) 1);
    public static final Short SHORT_MINUS_ONE = new Short((short) -1);

    //Byte类型 0 1 -1
    public static final Byte BYTE_ZERO = new Byte((byte) 0);
    public static final Byte BYTE_ONE = new Byte((byte) 1);
    public static final Byte BYTE_MINUS_ONE = new Byte((byte) -1);

    //Double类型 0 1 -1
    public static final Double DOUBLE_ZERO = new Double(0.0d);
    public static final Double DOUBLE_ONE = new Double(1.0d);
    public static final Double DOUBLE_MINUS_ONE = new Double(-1.0d);

    //Float类型 0 1 -1
    public static final Float FLOAT_ZERO = new Float(0.0f);
    public static final Float FLOAT_ONE = new Float(1.0f);
    public static final Float FLOAT_MINUS_ONE = new Float(-1.0f);

    /**
     * 功能描述:
     * 〈String转int类型〉
     *
     * @params : [str]
     * @return : int
     * @author : cwl
     * @date : 2019/6/6 16:32
     */
    public static int stringToInt(String str) {
        return toInt(str);
    }

    /**
     * 功能描述:
     * 〈str转int,当str为空,则返回默认值0〉
     *
     * @params : [str]
     * @return : int
     * @author : cwl
     * @date : 2019/6/6 16:33
     */
    public static int toInt(String str) {
        return toInt(str, 0);
    }

    /**
     * 功能描述:
     * 〈str转int,当str为空,则返回默认值〉
     *
     * @params : [str, defaultValue]
     * @return : int
     * @author : cwl
     * @date : 2019/6/6 16:34
     */
    public static int stringToInt(String str, int defaultValue) {
        return toInt(str, defaultValue);
    }

    /**
     * 功能描述:
     * 〈str转int,当str为空,则返回默认值〉
     *
     * @params : [str, defaultValue]
     * @return : int
     * @author : cwl
     * @date : 2019/6/6 16:34
     */
    public static int toInt(String str, int defaultValue) {
        if(str == null) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }

    /**
     * 功能描述:
     * 〈str转成int,不是字符串的使用0替换〉
     *
     * @params : [str, defaultValue]
     * @return : int
     * @author : cwl
     * @date : 2019/6/6 16:36
     */
    public static int toIntDefaultZero(String str){
        char[] chars = str.toCharArray();
        for (int i=0;i< chars.length;i++) {
            //如果字符不是数字,则记录它的索引位置
            if(chars[i] >= 48 && chars[i] <= 57){
                continue;
            }
            chars[i]=48;
        }
        return Integer.valueOf(new String(chars));
    }

    /**
     * 功能描述:
     * 〈字符串转long类型〉
     *
     * @params : [str]
     * @return : long
     * @author : cwl
     * @date : 2019/6/6 17:24
     */
    public static long toLong(String str) {
        return toLong(str, 0L);
    }

    /**
     * 功能描述:
     * 〈字符串转long类型,如果字符串为空,则返回默认值〉
     *
     * @params : [str, defaultValue]
     * @return : long
     * @author : cwl
     * @date : 2019/6/6 17:24
     */
    public static long toLong(String str, long defaultValue) {
        if (str == null) {
            return defaultValue;
        }
        try {
            return Long.parseLong(str);
        } catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }

    /**
     * 功能描述:
     * 〈字符串转float类型〉
     *
     * @params : [str]
     * @return : float
     * @author : cwl
     * @date : 2019/6/6 17:25
     */
    public static float toFloat(String str) {
        return toFloat(str, 0.0f);
    }

    /**
     * 功能描述:
     * 〈字符串转float类型,如果字符串为空,则返回默认值〉
     *
     * @params : [str, defaultValue]
     * @return : float
     * @author : cwl
     * @date : 2019/6/6 17:25
     */
    public static float toFloat(String str, float defaultValue) {
        if (str == null) {
            return defaultValue;
        }
        try {
            return Float.parseFloat(str);
        } catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }

    /**
     * 功能描述:
     * 〈字符串转double类型〉
     *
     * @params : [str]
     * @return : double
     * @author : cwl
     * @date : 2019/6/6 17:26
     */
    public static double toDouble(String str) {
        return toDouble(str, 0.0d);
    }

    /**
     * 功能描述:
     * 〈字符串转double类型,如果字符串为空,则返回默认值〉
     *
     * @params : [str, defaultValue]
     * @return : double
     * @author : cwl
     * @date : 2019/6/6 17:26
     */
    public static double toDouble(String str, double defaultValue) {
        if (str == null) {
            return defaultValue;
        }
        try {
            return Double.parseDouble(str);
        } catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }

    /**
     * 功能描述:
     * 〈string数组转long集合〉
     * @param arr
     * @return
     */
    public static List<Long> stringToLong(String[] arr) {
        return Arrays.stream(arr).mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
    }

    /**
     * 功能描述:
     * 〈string数组转long集合〉
     * @param arr
     * @return
     */
    public static List<Integer> stringToInt(String[] arr) {
        return Arrays.stream(arr).mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());
    }

    /**
     * 功能描述:
     * 〈string数组转Double集合〉
     * @param arr
     * @return
     */
    public static List<Double> stringToDouble(String[] arr) {
        return Arrays.stream(arr).mapToDouble(Double::parseDouble).boxed().collect(Collectors.toList());
    }

    /**
     * 功能描述:
     * 〈创建一个Number〉
     *
     * @params : [str]
     * @return : java.lang.Number
     * @author : cwl
     * @date : 2019/6/6 17:33
     */
    public static Number createNumber(String str) throws NumberFormatException {
        if (str == null) {
            return null;
        }
        if (null == str || str.length() == 0 || " ".equals(str)) {
            throw new NumberFormatException("A blank string is not a valid number");
        }
        if (str.startsWith("--")) {
            //对于Bigdecimal值的一种处理
            return null;
        }
        if (str.startsWith("0x") || str.startsWith("-0x")) {
            return createInteger(str);
        }
        char lastChar = str.charAt(str.length() - 1);
        String mant;
        String dec;
        String exp;
        int decPos = str.indexOf('.');
        int expPos = str.indexOf('e') + str.indexOf('E') + 1;

        if (decPos > -1) {

            if (expPos > -1) {
                if (expPos < decPos) {
                    throw new NumberFormatException(str + " is not a valid number.");
                }
                dec = str.substring(decPos + 1, expPos);
            } else {
                dec = str.substring(decPos + 1);
            }
            mant = str.substring(0, decPos);
        } else {
            if (expPos > -1) {
                mant = str.substring(0, expPos);
            } else {
                mant = str;
            }
            dec = null;
        }
        if (!Character.isDigit(lastChar)) {
            if (expPos > -1 && expPos < str.length() - 1) {
                exp = str.substring(expPos + 1, str.length() - 1);
            } else {
                exp = null;
            }
            String numeric = str.substring(0, str.length() - 1);
            boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
            switch (lastChar) {
                case 'l' :
                case 'L' :
                    if (dec == null
                            && exp == null
                            && (numeric.charAt(0) == '-' && isDigits(numeric.substring(1)) || isDigits(numeric))) {
                        try {
                            return createLong(numeric);
                        } catch (NumberFormatException nfe) {
                            //Too big for a long
                        }
                        return createBigInteger(numeric);

                    }
                    throw new NumberFormatException(str + " is not a valid number.");
                case 'f' :
                case 'F' :
                    try {
                        Float f = NumberUtils.createFloat(numeric);
                        if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
                            //如果浮点数太大，或者浮点值= 0，并且字符串中有非零，那么浮点数就没有我们想要的精度
                            return f;
                        }

                    } catch (NumberFormatException nfe) {
                        // 忽略非数字类型
                    }
                    // 顺序执行,无需break
                case 'd' :
                case 'D' :
                    try {
                        Double d = NumberUtils.createDouble(numeric);
                        if (!(d.isInfinite() || (d.floatValue() == 0.0D && !allZeros))) {
                            return d;
                        }
                    } catch (NumberFormatException nfe) {
                        // 忽略非数字类型
                    }
                    try {
                        return createBigDecimal(numeric);
                    } catch (NumberFormatException e) {
                        // 忽略非数字类型
                    }
                    // 顺序执行,无需break
                default :
                    throw new NumberFormatException(str + " is not a valid number.");

            }
        } else {
            if (expPos > -1 && expPos < str.length() - 1) {
                exp = str.substring(expPos + 1, str.length());
            } else {
                exp = null;
            }
            if (dec == null && exp == null) {
                //必须是int或者是long类型
                try {
                    return createInteger(str);
                } catch (NumberFormatException nfe) {
                    // ignore the bad number
                }
                try {
                    return createLong(str);
                } catch (NumberFormatException nfe) {
                    // ignore the bad number
                }
                return createBigInteger(str);

            } else {
                //必须是float或者double或者bigdecmail
                boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
                try {
                    Float f = createFloat(str);
                    if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
                        return f;
                    }
                } catch (NumberFormatException nfe) {
                    // ignore the bad number
                }
                try {
                    Double d = createDouble(str);
                    if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) {
                        return d;
                    }
                } catch (NumberFormatException nfe) {
                    // ignore the bad number
                }

                return createBigDecimal(str);

            }
        }
    }

    /**
     * 功能描述:
     * 〈判断该字符串是否都是0〉
     *
     * @params : [str]
     * @return : boolean
     * @author : cwl
     * @date : 2019/6/6 17:41
     */
    private static boolean isAllZeros(String str) {
        if (str == null) {
            return true;
        }
        for (int i = str.length() - 1; i >= 0; i--) {
            if (str.charAt(i) != '0') {
                return false;
            }
        }
        return str.length() > 0;
    }

    /**
     * 功能描述:
     * 〈str转float〉
     *
     * @params : [str]
     * @return : java.lang.Float
     * @author : cwl
     * @date : 2019/6/6 17:42
     */
    public static Float createFloat(String str) {
        if (str == null) {
            return null;
        }
        return Float.valueOf(str);
    }

    /**
     * 功能描述:
     * 〈str转double类型〉
     *
     * @params : [str]
     * @return : java.lang.Double
     * @author : cwl
     * @date : 2019/6/6 17:42
     */
    public static Double createDouble(String str) {
        if (str == null) {
            return null;
        }
        return Double.valueOf(str);
    }

    /**
     * 功能描述:
     * 〈str转Integer类型〉
     *
     * @params : [str]
     * @return : java.lang.Integer
     * @author : cwl
     * @date : 2019/6/6 17:42
     */
    public static Integer createInteger(String str) {
        if (str == null) {
            return null;
        }
        // decode() handles 0xAABD and 0777 (hex and octal) as well.
        return Integer.decode(str);
    }

    /**
     * 功能描述:
     * 〈str转Long类型〉
     *
     * @params : [str]
     * @return : java.lang.Long
     * @author : cwl
     * @date : 2019/6/6 17:43
     */
    public static Long createLong(String str) {
        if (str == null) {
            return null;
        }
        return Long.valueOf(str);
    }

    /**
     * 功能描述:
     * 〈str转BigInteger〉
     *
     * @params : [str]
     * @return : java.math.BigInteger
     * @author : cwl
     * @date : 2019/6/6 17:44
     */
    public static BigInteger createBigInteger(String str) {
        if (str == null) {
            return null;
        }
        return new BigInteger(str);
    }

    /**
     * 功能描述:
     * 〈str转BigDecimal〉
     *
     * @params : [str]
     * @return : java.math.BigDecimal
     * @author : cwl
     * @date : 2019/6/6 17:47
     */
    public static BigDecimal createBigDecimal(String str) {
        if (str == null) {
            return null;
        }
        // 处理JDK1.3.1当中空字符串也会抛出索引越界异常问题
        if (" ".equals(str.trim()) || str.length() == 0) {
            throw new NumberFormatException("A blank string is not a valid number");
        }
        return new BigDecimal(str);
    }

    /**
     * 功能描述:
     * 〈返回long数组当中的最小值〉
     *
     * @params : [array]
     * @return : long
     * @author : cwl
     * @date : 2019/6/6 17:47
     */
    public static long min(long[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns min
        long min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }

        return min;
    }

    /**
     * 功能描述:
     * 〈返回int数组当中的最小值〉
     *
     * @params : [array]
     * @return : int
     * @author : cwl
     * @date : 2019/6/6 17:49
     */
    public static int min(int[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns min
        int min = array[0];
        for (int j = 1; j < array.length; j++) {
            if (array[j] < min) {
                min = array[j];
            }
        }

        return min;
    }

    /**
     * 功能描述:
     * 〈返回short数组当中的最小值〉
     *
     * @params : [array]
     * @return : short
     * @author : cwl
     * @date : 2019/6/6 17:49
     */
    public static short min(short[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns min
        short min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }

        return min;
    }

    /**
     * 功能描述:
     * 〈返回byte数组当中的最小值〉
     *
     * @params : [array]
     * @return : byte
     * @author : cwl
     * @date : 2019/6/6 17:50
     */
    public static byte min(byte[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns min
        byte min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }

        return min;
    }

    /**
     * 功能描述:
     * 〈返回double数组中的最小值〉
     *
     * @params : [array]
     * @return : double
     * @author : cwl
     * @date : 2019/6/6 17:50
     */
    public static double min(double[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns min
        double min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (Double.isNaN(array[i])) {
                return Double.NaN;
            }
            if (array[i] < min) {
                min = array[i];
            }
        }

        return min;
    }

    /**
     * 功能描述:
     * 〈返回float数组中的最小值〉
     *
     * @params : [array]
     * @return : float
     * @author : cwl
     * @date : 2019/6/6 17:50
     */
    public static float min(float[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns min
        float min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (Float.isNaN(array[i])) {
                return Float.NaN;
            }
            if (array[i] < min) {
                min = array[i];
            }
        }

        return min;
    }

    /**
     * 功能描述:
     * 〈返回long数组的最大值〉
     *
     * @params : [array]
     * @return : long
     * @author : cwl
     * @date : 2019/6/6 17:52
     */
    public static long max(long[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns max
        long max = array[0];
        for (int j = 1; j < array.length; j++) {
            if (array[j] > max) {
                max = array[j];
            }
        }

        return max;
    }

    /**
     * 功能描述:
     * 〈返回int数组最大值〉
     *
     * @params : [array]
     * @return : int
     * @author : cwl
     * @date : 2019/6/6 17:53
     */
    public static int max(int[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns max
        int max = array[0];
        for (int j = 1; j < array.length; j++) {
            if (array[j] > max) {
                max = array[j];
            }
        }

        return max;
    }

    /**
     * 功能描述:
     * 〈返回short数组最大值〉
     *
     * @params : [array]
     * @return : short
     * @author : cwl
     * @date : 2019/6/6 17:53
     */
    public static short max(short[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns max
        short max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }

        return max;
    }

    /**
     * 功能描述:
     * 〈返回byte数组最大值〉
     *
     * @params : [array]
     * @return : byte
     * @author : cwl
     * @date : 2019/6/6 17:54
     */
    public static byte max(byte[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns max
        byte max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }

        return max;
    }

    /**
     * 功能描述:
     * 〈获取double数组最大值〉
     *
     * @params : [array]
     * @return : double
     * @author : cwl
     * @date : 2019/6/6 17:54
     */
    public static double max(double[] array) {
        // Validates input
        if (array== null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns max
        double max = array[0];
        for (int j = 1; j < array.length; j++) {
            if (Double.isNaN(array[j])) {
                return Double.NaN;
            }
            if (array[j] > max) {
                max = array[j];
            }
        }

        return max;
    }

    /**
     * 功能描述:
     * 〈获取float数组最大值〉
     *
     * @params : [array]
     * @return : float
     * @author : cwl
     * @date : 2019/6/6 17:55
     */
    public static float max(float[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }

        // Finds and returns max
        float max = array[0];
        for (int j = 1; j < array.length; j++) {
            if (Float.isNaN(array[j])) {
                return Float.NaN;
            }
            if (array[j] > max) {
                max = array[j];
            }
        }

        return max;
    }

    /**
     * 功能描述:
     * 〈判断字符是否是纯数字〉
     *
     * @params : [str]
     * @return : boolean
     * @author : cwl
     * @date : 2019/6/6 17:58
     */
    public static boolean isDigits(String str) {
        if (null == str || str.length() ==0 || " ".equals(str) ) {
            return false;
        }
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 功能描述:
     * 〈判断是否是一个数字类型〉
     *
     * @params : [str]
     * @return : boolean
     * @author : cwl
     * @date : 2019/6/6 17:59
     */
    public static boolean isNumber(String str) {
        if (null == str || str.length() ==0 || " ".equals(str) ) {
            return false;
        }
        char[] chars = str.toCharArray();
        int sz = chars.length;
        boolean hasExp = false;
        boolean hasDecPoint = false;
        boolean allowSigns = false;
        boolean foundDigit = false;
        // deal with any possible sign up front
        int start = (chars[0] == '-') ? 1 : 0;
        if (sz > start + 1) {
            if (chars[start] == '0' && chars[start + 1] == 'x') {
                int i = start + 2;
                if (i == sz) {
                    return false; // str == "0x"
                }
                // checking hex (it can't be anything else)
                for (; i < chars.length; i++) {
                    if ((chars[i] < '0' || chars[i] > '9')
                            && (chars[i] < 'a' || chars[i] > 'f')
                            && (chars[i] < 'A' || chars[i] > 'F')) {
                        return false;
                    }
                }
                return true;
            }
        }
        sz--; // don't want to loop to the last char, check it afterwords
        // for type qualifiers
        int i = start;
        // loop to the next to last char or to the last char if we need another digit to
        // make a valid number (e.g. chars[0..5] = "1234E")
        while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) {
            if (chars[i] >= '0' && chars[i] <= '9') {
                foundDigit = true;
                allowSigns = false;

            } else if (chars[i] == '.') {
                if (hasDecPoint || hasExp) {
                    // two decimal points or dec in exponent
                    return false;
                }
                hasDecPoint = true;
            } else if (chars[i] == 'e' || chars[i] == 'E') {
                // we've already taken care of hex.
                if (hasExp) {
                    // two E's
                    return false;
                }
                if (!foundDigit) {
                    return false;
                }
                hasExp = true;
                allowSigns = true;
            } else if (chars[i] == '+' || chars[i] == '-') {
                if (!allowSigns) {
                    return false;
                }
                allowSigns = false;
                foundDigit = false; // we need a digit after the E
            } else {
                return false;
            }
            i++;
        }
        if (i < chars.length) {
            if (chars[i] >= '0' && chars[i] <= '9') {
                // no type qualifier, OK
                return true;
            }
            if (chars[i] == 'e' || chars[i] == 'E') {
                // can't have an E at the last byte
                return false;
            }
            if (!allowSigns
                    && (chars[i] == 'd'
                    || chars[i] == 'D'
                    || chars[i] == 'f'
                    || chars[i] == 'F')) {
                return foundDigit;
            }
            if (chars[i] == 'l'
                    || chars[i] == 'L') {
                // not allowing L with an exponent
                return foundDigit && !hasExp;
            }
            // last character is illegal
            return false;
        }
        // allowSigns is true iff the val ends in 'E'
        // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass
        return !allowSigns && foundDigit;
    }


    private static Pattern numberPattern = Pattern.compile("[0-9]*");


    public static Long objToLongDefault(Object obj, long defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            return Long.parseLong(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static Long objToLongDefault(Object obj, Long defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            return Long.parseLong(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static Boolean objToBooleanDefault(Object obj, Boolean defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            return Boolean.parseBoolean(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static Character objToCharacterDefault(Object obj, Character defaultValue) {
        if (null == obj) return defaultValue;
        try {
            char[] chars = obj.toString().toCharArray();
            return chars[0];
        } catch (Exception e) {
            return defaultValue;
        }
    }


    /**
     * 方法说明: 判断输入的数值是否为空或者0
     *
     * @param num
     * @return
     */
    public static boolean isNullOrZero(Integer num) {
        if (num == null || num == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 无默认返回
     * <p>
     * 若obj为null，则返回null
     *
     * @param obj
     * @return
     */
    public static Long objToLong(Object obj) {
        if (null == obj) {
            return null;
        }
        try {
            return Long.parseLong(obj.toString());
        } catch (Exception e) {
            return null;
        }
    }

    public static int objToIntDefault(Object obj, int defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static Integer objToIntDefault(Object obj, Integer defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static Short objToShortDefault(Object obj, Short defaultValue) {
        if (null == obj) return defaultValue;
        try {
            return Short.parseShort(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static Byte objToByteDefault(Object obj, Byte defaultValue) {
        if (null == obj) return defaultValue;
        try {
            return Byte.parseByte(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static int objToPageIntDefault(Object obj, int defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            int pageNum = Integer.parseInt(obj.toString());
            return pageNum == 0 ? 1 : pageNum;
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static Long objToPageLongDefault(Object obj, Long defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            Long pageNum = Long.parseLong(obj.toString());
            return pageNum == 0L ? 1L : pageNum;
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static float objToFloatDefault(Object obj, Float defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            return Float.parseFloat(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static double objToDoubleDefault(Object obj, double defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            return Double.parseDouble(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static Double objToDoubleDefault(Object obj, Double defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            return Double.parseDouble(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static BigDecimal objToBigDecimalDefault(Object obj, BigDecimal defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            return new BigDecimal(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }



    public static BigDecimal objToBigDecimalZeroToDefault(Object obj, BigDecimal defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            BigDecimal objValue = new BigDecimal(obj.toString());
            if (objValue.compareTo(BigDecimal.ZERO) == 0) {
                return defaultValue;
            }
            return objValue;
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * 无默认返回
     * <p>
     * 若obj为null，则返回null
     *
     * @param obj
     * @return
     */
    public static Integer objToInteger(Object obj) {
        if (null == obj) {
            return null;
        }
        try {
            return Integer.parseInt(obj.toString());
        } catch (Exception e) {
            log.error("格式转换异常", e);
            return null;
        }
    }


    /**
     * 保留两位小数
     *
     * @param d
     * @return
     */
    public static String format2Str(BigDecimal d) {
        if (d == null) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat("0.00");
        String ds = df.format(d);
        return ds;
    }

    /**
     * 比较数字是否在指定两个数字范围呢
     *
     * @param compareNum 需要转换的数字
     * @param minNumber  最小范围
     * @param maxNumber  最大范围
     * @return 成功返回Ineger，失败返回null
     */
    public static boolean between2Number(Number compareNum, Number minNumber, Number maxNumber) {
        if (compareNum.longValue() > maxNumber.longValue() || compareNum.longValue() < minNumber.longValue()) {
            return false;
        }
        return true;
    }


    /**
     * Determine whether the given value String indicates a hex number, i.e. needs to be passed into
     * {@code Integer.decode} instead of {@code Integer.valueOf} (etc).
     */
    private static boolean isHexNumber(String value) {
        int index = (value.startsWith("-") ? 1 : 0);
        return (value.startsWith("0x", index) || value.startsWith("0X", index) || value.startsWith("#", index));
    }

    /**
     * Decode a {@link BigInteger} from a {@link String} value. Supports decimal, hex and octal notation.
     *
     * @see BigInteger#BigInteger(String, int)
     */
    private static BigInteger decodeBigInteger(String value) {
        int radix = 10;
        int index = 0;
        boolean negative = false;

        // Handle minus sign, if present.
        if (value.startsWith("-")) {
            negative = true;
            index++;
        }

        // Handle radix specifier, if present.
        if (value.startsWith("0x", index) || value.startsWith("0X", index)) {
            index += 2;
            radix = 16;
        } else if (value.startsWith("#", index)) {
            index++;
            radix = 16;
        } else if (value.startsWith("0", index) && value.length() > 1 + index) {
            index++;
            radix = 8;
        }

        BigInteger result = new BigInteger(value.substring(index), radix);
        return (negative ? result.negate() : result);
    }

    private static String trimAllWhitespace(String str) {
        if (!(str != null && str.length() > 0)) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        int index = 0;
        while (sb.length() > index) {
            if (Character.isWhitespace(sb.charAt(index))) {
                sb.deleteCharAt(index);
            } else {
                index++;
            }
        }
        return sb.toString();
    }

    public static boolean isValidRangeForInteger(Integer obj, int min, int max) {
        return (obj != null && obj >= min && obj <= max);
    }

    public static boolean isNotValidRangeForInteger(Integer obj, int min, int max) {
        return (obj == null || obj < min || obj > max);
    }

    public static boolean isValidForLong(Long obj) {
        return (obj != null && obj >= 0);
    }

    public static boolean isNotValidForLong(Long obj) {
        return (obj == null || obj < 0);
    }

    public static boolean isValidForInteger(Integer obj) {
        return (obj != null && obj >= 0);
    }

    public static boolean isNotValidForInteger(Integer obj) {
        return (obj == null || obj < 0);
    }


    public static BigDecimal objToBigDecimalDivideOnehundredDefault(Object obj, BigDecimal defaultValue) {
        if (null == obj) {
            return defaultValue;
        }
        try {
            return new BigDecimal(obj.toString()).divide(BigDecimal.valueOf(100.00), 2, BigDecimal.ROUND_DOWN);
        } catch (Exception e) {
            return defaultValue;
        }
    }
    /**
     * 判断数字类型
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {

        Matcher isNum = numberPattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static String toChinese(String string) {
        String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };

        String result = "";

        int n = string.length();
        for (int i = 0; i < n; i++) {

            int num = string.charAt(i) - '0';

            if (i != n - 1 && num != 0) {
                result += s1[num] + s2[n - 2 - i];
            } else {
                result += s1[num];
            }
        }

        return result;

    }

    /**
     * 取最大值
     * @param v1
     * @param v2
     * @param v3
     * @return
     */
    public static BigDecimal compare(BigDecimal v1,BigDecimal v2,BigDecimal v3){
        BigDecimal max = v1.compareTo(v2)>=0 ? v1:v2;
        max = max.compareTo(v3)>=0 ? max:v3;
        return max;

    }

    public static boolean notEquals(Integer a,Integer ... b ){
        return !equals(a,b);
    }

    public static boolean equals(Integer a,Integer ... b ){
        if(a == null && b==null){
            return true;
        }else if (a == null ){
            return false;
        }else if (b == null){
            return false;
        }
        for(Integer c : b ){
            if(c.equals(a)){
                return true;
            }
        }
        return false;
    }




    public static Long convertActivityId(Long activityId){
        if(new Long(-1).equals(activityId) || new Long(0).equals(activityId)){
            return 1l;
        }
        return activityId;
    }



    public static boolean equals(Long a,Long ... b ){
        if(a == null && b==null){
            return true;
        }else if (a == null ){
            return false;
        }else if (b == null){
            return false;
        }
        for(Long c : b ){
            if(c.equals(a)){
                return true;
            }
        }
        return false;
    }



    public static boolean randomM_N(Integer max,int rand){
        Random random = new Random();
        int i = random.nextInt(max);
        if(i < rand){
            return true;
        }
        return false;
    }




    public static BigDecimal getConfigRandom(String ramdom ){
        String configs [] = ramdom.split(",");
        List<BigDecimal> bigDecimals = new ArrayList<>();
        for(BigDecimal i = NumberUtils.objToBigDecimalDefault(configs[0],BigDecimal.ZERO);
            i.compareTo( NumberUtils.objToBigDecimalDefault(configs[1],BigDecimal.ZERO)) <=0;
            i = i .add(NumberUtils.objToBigDecimalDefault(configs[2],BigDecimal.ZERO))){
            bigDecimals.add(i);
        }
        Random random = new Random();
        int r = random.nextInt(bigDecimals.size());
        return bigDecimals.get(r);
    }


    public static Integer getRandomInt(Integer min, Integer max, Integer step) {
        if(max == null || min == null || step == null){
            return 0 ;
        }
        if(min.equals(max)){
            return max;
        }
        List<Integer> list = new ArrayList<>();
        for (int i = min; i < max; i = i + step) {
            list.add(i);
        }
        Random random = new Random();
        return list.get(random.nextInt(list.size()));
    }


    public static Date objToDate(Long date ) {
        if(date == null){
            return new Date();
        }
        return new Date(date);
    }

    /**
     * 获取目标im房间号
     * @return
     */
    public static Long getGoalImNumber(Long activityId ,Integer runningGoal) {
        if (Objects.isNull(runningGoal)) {
            return activityId;
        }
        if (runningGoal < 100) {
            return Long.valueOf(activityId+""+runningGoal);
        }
        runningGoal = runningGoal/100;
        return Long.valueOf(activityId+""+runningGoal);
    }
}
