/**
     * @description 过滤null值,
     * 例如String类型过滤为 "" ,int类型 过滤为 0
     * @author yang
     * @date 2021-12-16 16:09:31
     * @return T
     */
    public static <T,V> V filterNullToBaseData(T t, CustomDataTypeEnum customDataTypeEnum)
    {
        switch (customDataTypeEnum)
        {
            case INT:
                Integer integerZero = new Integer(0);
                if(null==t)
                {
                    return (V)integerZero;
                }
                String intString = t.toString();
                if(!StringUtils.isNumeric(intString))
                {
                    return (V)integerZero;
                }

                if(10<intString.length())
                {
                    return (V)integerZero;
                }

                long numLong = Long.parseLong(intString);

                if(2147483647l<numLong)
                {
                    return (V)integerZero;
                }

                return (V)new Integer(intString);
            case BYTE:
                Byte byteZero = new Byte("0");
                if(null==t)
                {
                    return (V)byteZero;
                }
                String byteString = t.toString();
                if(!StringUtils.isNumeric(byteString))
                {
                    return (V)byteZero;
                }

                if(3<byteString.length())
                {
                    return (V)byteZero;
                }
                int i = Integer.parseInt(byteString);
                //最大127
                if(127<i)
                {
                    return (V)byteZero;
                }
                return (V)new Byte(t.toString());
            case BIGDECIMAL:
                if(null==t)
                {
                    return (V)BigDecimal.ZERO;
                }
                String bigDecimalString = t.toString();
                if(!NumberUtils.isNumber(bigDecimalString))
                {
                    return (V)BigDecimal.ZERO;
                }
                return (V)new BigDecimal(bigDecimalString);
            case DOUBLE:
                Double doubleZero = new Double(0d);
                if(null==t)
                {
                    return (V)doubleZero;
                }
                String doubleString = t.toString();
                if(!NumberUtils.isNumber(doubleString))
                {
                    return (V)doubleZero;
                }
                return (V)new Double(doubleString);
            case STRING:
                String stingEmpty="";
                if(null==t)
                {
                    return (V)stingEmpty;
                }
                return (V)t;
            default:
                throw new JeecgBootException("missing dataType!");
        }
    }
最后修改日期: 2021年12月17日

作者

留言

撰写回覆或留言