Java开发常用Util工具类-StringUtil、CastUtil、CollectionUtil、PropsUtil

字符串工具类StringUtil.java

package com.***.util;

/**
 * StringUtil
 * @description: 字符串工具类
 **/
public class StringUtil {

    /**
     * 判断是否为空字符串最优代码
     * @param str
     * @return 如果为空,则返回true
     */
    public static boolean isEmpty(String str){
        return str == null || str.trim().length() == 0;
    }

    /**
     * 判断字符串是否非空
     * @param str 如果不为空,则返回true
     * @return
     */
    public static boolean isNotEmpty(String str){
        return !isEmpty(str);
    }
}

数据类型转换类CastUtil.java

package com.***.util;

/**
 * CastUtil
 * @description: 数据转型工具类
 **/
public class CastUtil {
    /** 
    * @Description: 转为String类型
    * @Param: [obj] 
    * @return: java.lang.String 如果参数为null则转为空字符串
    */ 
    public static String castString(Object obj){
        return CastUtil.castString(obj,"");
    }

    /** 
    * @Description: 转为String类型(提供默认值)
    * @Param: [obj, defaultValue] 将obj转为string,如果obj为null则返回default
    * @return: String
    */ 
    public static String castString(Object obj,String defaultValue){
        return obj!=null?String.valueOf(obj):defaultValue;
    }

    /** 
    * @Description: 转为double类型,如果为null或者空字符串或者格式不对则返回0
    * @Param: [obj] 
    * @return: String
    */ 
    public static double castDouble(Object obj){
        return CastUtil.castDouble(obj,0);
    }

    /** 
    * @Description: 转为double类型 ,如果obj为null或者空字符串或者格式不对则返回defaultValue
    * @Param: [obj, defaultValue] 
    * @return: String obj为null或者空字符串或者格式不对返回defaultValue
    */ 
    public static double castDouble(Object obj,double defaultValue){
        double value = defaultValue;  //声明结果,把默认值赋给结果
        if (obj!=null){   //判断是否为null
            String strValue = castString(obj);  //转换为String
            if (StringUtil.isNotEmpty(strValue)){   //判断字符串是否为空(是否为空只能判断字符串,不能判断Object)
                try{
                    value = Double.parseDouble(strValue);  //不为空则把值赋给value
                }catch (NumberFormatException e){
                    value = defaultValue;  //格式不对把默认值赋给value
                }

            }
        }
        return value;
    }

    /**
     * 转为long型,如果obj为null或者空字符串或者格式不对则返回0
     * @param obj
     * @return
     */
    public static long castLong(Object obj){
        return CastUtil.castLong(obj,0);
    }

    /**
     * 转为long型(提供默认数值),如果obj为null或者空字符串或者格式不对则返回defaultValue
     * @param obj
     * @param defaultValue
     * @return obj为null或者空字符串或者格式不对返回defaultValue
     */
    public static long castLong(Object obj,long defaultValue){
        long value = defaultValue;  //声明结果,把默认值赋给结果
        if (obj!=null){   //判断是否为null
            String strValue = castString(obj);  //转换为String
            if (StringUtil.isNotEmpty(strValue)){   //判断字符串是否为空(是否为空只能判断字符串,不能判断Object)
                try{
                    value = Long.parseLong(strValue);  //不为空则把值赋给value
                }catch (NumberFormatException e){
                    value = defaultValue;  //格式不对把默认值赋给value
                }

            }
        }
        return value;
    }

    /**
     * 转为int型
     * @param obj
     * @return 如果obj为null或者空字符串或者格式不对则返回0
     */
    public static int castInt(Object obj){
        return CastUtil.castInt(obj,0);
    }

    /**
     * 转为int型(提供默认值)
     * @param obj
     * @param defaultValue
     * @return 如果obj为null或者空字符串或者格式不对则返回defaultValue
     */
    public static int castInt(Object obj,int defaultValue){
        int value = defaultValue;  //声明结果,把默认值赋给结果
        if (obj!=null){   //判断是否为null
            String strValue = castString(obj);  //转换为String
            if (StringUtil.isNotEmpty(strValue)){   //判断字符串是否为空(是否为空只能判断字符串,不能判断Object)
                try{
                    value = Integer.parseInt(strValue);  //不为空则把值赋给value
                }catch (NumberFormatException e){
                    value = defaultValue;  //格式不对把默认值赋给value
                }

            }
        }
        return value;
    }

    /**
     * 转为boolean型,不是true的返回为false
     * @param obj
     * @return
     */
    public static boolean castBoolean(Object obj){
        return CastUtil.castBoolean(obj,false);
    }


    /**
     * 转为boolean型(提供默认值)
     * @param obj
     * @param defaultValue
     * @return
     */
    public static boolean castBoolean(Object obj,boolean defaultValue){
        boolean value = defaultValue;
        if (obj!=null){  //为null则返回默认值
            value = Boolean.parseBoolean(castString(obj));  //底层会把字符串和true对比,所以不用判断是否为空字符串
        }
        return value;
    }
}

集合工具类CollectionUtil.java

package com.***.util;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import java.util.Collection;
import java.util.Map;

/**
 * CollectionUtil
 * @description: 集合工具类
 **/
public class CollectionUtil {
    /**
     * 判断collection是否为空
     * @param collection
     * @return
     */
    public static boolean isEmpty(Collection<?> collection){
        //return CollectionUtils.isEmpty(collection);
        return collection == null || collection.isEmpty();
    }

    /**
     * 判断Collection是否非空
     * @return
     */
    public static boolean isNotEmpty(Collection<?> collection){
        return !isEmpty(collection);
    }

    /**
     * 判断map是否为空
     * @param map
     * @return
     */
    public static boolean isEmpty(Map<?,?> map){
        //return MapUtils.isEmpty(map);
        return map == null || map.isEmpty();
    }

    /**
     * 判断map是否非
     * @param map
     * @return
     */
    public static boolean isNotEmpty(Map<?,?> map){
        return !isEmpty(map);
    }
}

Properties文件操作类PropsUtil.java

package com.***.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 属性文件工具类
 */
public class PropsUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(PropsUtil.class);

    /**
     * 加载属性文件
     * @param fileName fileName一定要在class下面及java根目录或者resource跟目录下
     * @return
     */
    public static Properties loadProps(String fileName){
        Properties props = null;
        InputStream is = null;
        try {
            //将资源文件加载为流
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            if(is==null){
               throw new FileNotFoundException(fileName+"file is not Found");
            }
        } catch (FileNotFoundException e) {
            LOGGER.error("load properties file filure",e);
        }finally {
            if(is !=null){
                try {
                    is.close();
                } catch (IOException e) {
                    LOGGER.error("close input stream failure",e);
                }
            }
        }
        return props;
    }

    /**
     * 获取字符型属性(默认值为空字符串)
     * @param props
     * @param key
     * @return
     */
    public static String getString(Properties props,String key){
        return getString(props,key,"");
    }

    /**
     * 获取字符型属性(可制定默认值)
     * @param props
     * @param key
     * @param defaultValue 当文件中无此key对应的则返回defaultValue
     * @return
     */
    public static String getString(Properties props,String key,String defaultValue){
        String value = defaultValue;
        if (props.containsKey(key)){
            value = props.getProperty(key);
        }
        return value;
    }

    /**
     * 获取数值型属性(默认值为0)
     * @param props
     * @param key
     * @return
     */
    public static int getInt(Properties props,String key){
        return getInt(props,key,0);
    }

    /**
     * 获取数值型属性(可指定默认值)
     * @param props
     * @param key
     * @param defaultValue
     * @return
     */
    public static int getInt(Properties props,String key,int defaultValue){
        int value = defaultValue;
        if (props.containsKey(key)){
            value = CastUtil.castInt(props.getProperty(key));
        }
        return value;
    }

    /**
     * 获取布尔型属性(默认值为false)
     * @param props
     * @param key
     * @return
     */
    public static boolean getBoolean(Properties props,String key){
        return getBoolean(props,key,false);
    }

    /**
     * 获取布尔型属性(可指定默认值)
     * @param props
     * @param key
     * @param defaultValue
     * @return
     */
    public static boolean getBoolean(Properties props,String key,Boolean defaultValue){
        boolean value = defaultValue;
        if (props.containsKey(key)){
            value = CastUtil.castBoolean(props.getProperty(key));
        }
        return value;
    }
}

猜你喜欢

转载自www.cnblogs.com/aeolian/p/9484247.html