/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.common.utils;

import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 通用工具类
 *
 * @author Chill
 */
public class CommonUtil {

    /**
     * 判断参数是否是数字
     * @param value 需要判断数据
     * @return 判断结果，数字则为true，反之false
     */
    public static boolean checkIsBigDecimal(Object value){
        try{
            if(StringUtils.isNotEmpty(String.valueOf(value))){
                new BigDecimal(String.valueOf(value));
                return true;
            } else {
                return false;
            }
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 根据每页信息分组
     */
    public static <T> List<List<T>> getBatchSize(List<T> allIds, int size) {
        List<List<T>> batchIds = new ArrayList<>();
        if (allIds == null || allIds.size() == 0 ||  size <= 0) {
            return batchIds;
        }

        int i = 0;
        List<T> tmp = new ArrayList<>();
        for (T map : allIds) {
            tmp.add(map);
            i++;
            if (i % size == 0 || i ==  allIds.size()) {
                batchIds.add(tmp);
                tmp =  new ArrayList<>();
            }
        }
        return batchIds;
    }

}
