zoukankan      html  css  js  c++  java
  • 字符串工具类、数组工具类、集合工具类、转型操作工具类、编码与解码操作工具类

    package hjp.smart4j.framework.util;
    
    import org.apache.commons.lang3.StringUtils;
    
    /**
     * 字符串工具类
     */
    public final class StringUtil {
    
        /**
         * 字符串分隔符
         */
        public static final String SEPARATOR=String.valueOf((char)29);
    
        /**
         * 判断字符串是否为空
         */
        public static boolean isEmpty(String str) {
            if (str != null) {
                str = str.trim();
            }
            return StringUtils.isEmpty(str);
        }
    
        /**
         * 判断字符串是否非空
         */
        public static boolean isNotEmpty(String str) {
            return !isEmpty(str);
        }
    
        /**
         * 分割固定格式字符串
         */
        public static String[] splitString(String str,String separator){
            return StringUtils.splitByWholeSeparator(str,separator);
        }
    }
    StringUtil
    package hjp.smart4j.framework.util;
    
    import org.apache.commons.lang3.ArrayUtils;
    
    /**
     * 数组工具类
     */
    public final class ArrayUtil {
    
    /**
     * 判断数组是否非空
     */
        public static boolean isNotEmpty(Object[] array){
            return !ArrayUtils.isEmpty(array);
        }
        /**
         * 判断数组是否为空
         */
        public static boolean isEmpty(Object[] array){
            return ArrayUtils.isEmpty(array);
        }
    
    }
    ArrayUtil
    package hjp.smart4j.framework.util;
    
    import org.apache.commons.collections4.CollectionUtils;
    import org.apache.commons.collections4.MapUtils;
    
    import java.util.Collection;
    import java.util.Map;
    
    /**
     * 集合工具类
     */
    public final class CollectionUtil {
        /**
         * 判断Collection是否为空
         */
        public static boolean isEmpty(Collection<?> collection) {
            return CollectionUtils.isEmpty(collection);
        }
    
        /**
         * 判断Collection是否非空
         */
        public static boolean isNotEmpty(Collection<?> collection) {
            return !isEmpty(collection);
        }
    
        /**
         * 判断Map是否为空
         */
        public static boolean isEmpty(Map<?, ?> map) {
            return MapUtils.isEmpty(map);
        }
    
        /**
         * 判断Map是否非空
         */
        public static boolean isNotEmpty(Map<?, ?> map) {
            return !isEmpty(map);
        }
    }
    CollectionUtil
    package hjp.smart4j.framework.util;
    
    /**
     * 转型操作工具类
     */
    public final class CastUtil {
        /**
         * 转为String型
         */
        public static String castString(Object obj) {
            return CastUtil.castString(obj, "");
        }
    
        /**
         * 转为String型(提供默认值)
         */
        public static String castString(Object obj, String defaultValue) {
            return obj != null ? String.valueOf(obj) : defaultValue;
        }
    
        /**
         * 转为double型
         */
        public static double castDouble(Object obj) {
            return CastUtil.castDouble(obj, 0);
        }
    
        /**
         * 转为double型(指定默认值)
         */
        public static double castDouble(Object obj, double defaultValue) {
            double doubleValue = defaultValue;
            if (obj != null) {
                String strVlaue = castString(obj);
                if (StringUtil.isNotEmpty(strVlaue)) {
                    try {
                        doubleValue = Double.parseDouble(strVlaue);
                    } catch (NumberFormatException e) {
                        doubleValue = defaultValue;
                    }
                }
            }
            return doubleValue;
        }
    
        /**
         * 转为long型
         */
        public static long castLong(Object obj) {
            return castLong(obj, 0);
        }
    
        /**
         * 转为long型(提供默认值)
         */
        public static long castLong(Object obj, long defaultValue) {
            long longValue = defaultValue;
            if (obj != null) {
                String strValue = castString(obj);
                if (StringUtil.isNotEmpty(strValue)) {
                    try {
                        longValue = Long.parseLong(strValue);
                    } catch (NumberFormatException e) {
                        longValue = defaultValue;
                    }
                }
            }
            return longValue;
        }
    
        /**
         * 转为int型
         */
        public static int castInt(Object obj) {
            return castInt(obj, 0);
        }
    
        /**
         * 转为int型(提供默认值)
         */
        public static int castInt(Object obj, int defaultValue) {
            int intValue = defaultValue;
            if (obj != null) {
                String strValue = castString(obj);
                if (StringUtil.isNotEmpty(strValue)) {
                    try {
                        intValue = Integer.parseInt(strValue);
                    } catch (NumberFormatException e) {
                        intValue = defaultValue;
                    }
                }
            }
            return intValue;
        }
    
        /**
         * 转为boolean型
         */
        public static boolean castBoolean(Object obj) {
            return castBoolean(obj, false);
        }
    
        /**
         * 转为boolean型(提供默认值)
         */
        public static boolean castBoolean(Object obj, boolean defaultValue) {
            boolean booleanValue = defaultValue;
            if (obj != null) {
                booleanValue = Boolean.parseBoolean(castString(obj));
            }
            return booleanValue;
        }
    }
    CastUtil
    package hjp.smart4j.framework.util;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.net.URLDecoder;
    import java.net.URLEncoder;
    
    /**
     * 编码与解码操作工具类
     */
    public final class CodecUtil {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(CodecUtil.class);
    
        /**
         * 将URL编码
         */
        public static String encodeURL(String source) {
            String target;
            try {
                target = URLEncoder.encode(source, "UTF-8");
            } catch (Exception e) {
                LOGGER.error("encode url failure", e);
                throw new RuntimeException(e);
            }
            return target;
        }
    
        /**
         * 将URL解码
         */
        public static String decodeURL(String source) {
            String target;
            try {
                target = URLDecoder.decode(source, "UTF-8");
            } catch (Exception e) {
                LOGGER.error("decode url failure", e);
                throw new RuntimeException(e);
            }
            return target;
        }
    
    }
    CodecUtil
  • 相关阅读:
    超级简单:一步一步教你创建一小型的asp.net mvc 应用程序
    asp.net AJAX 验证用户名是否存在 Jquery
    生成缩略图、为图片添加文字水印、图片水印的类
    图Graph
    [转]Implementing a Generic Binary Tree in C#
    .net C#数据结构
    Why HTML5 is worth your time
    跳跃表SkipList
    C# LockFreeStack类
    [转]泛型弱引用
  • 原文地址:https://www.cnblogs.com/hujiapeng/p/5583894.html
Copyright © 2011-2022 走看看