字符串工具类、数组工具类、集合工具类、转型操作工具类、编码与解码操作工具类

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

时间: 2024-12-03 16:38:11

字符串工具类、数组工具类、集合工具类、转型操作工具类、编码与解码操作工具类的相关文章

Javascript 类数组(Array-like)对象

Javascript中的类数组对象(Array-like object)指的是一些看起来像数组但又不是数组的对象.Javascript中的arguments变量.document.getElementsByTagName()返回值就是典型的类数组对象. 类数组特性 类数组对象具有一个length属性且length的值为非负整数. 类数组对象不具有数组对象的方法.例如:push. pop等方法. 类数组对象可以像数组一样遍历,但不支持数组对象的方法. function test1() { for(

jQuery类数组

1.什么是类数组 jQuery对象封装的多个DOM对象 类:指的是类似 具备自己特有的操作方法 2.类数组的操作 length属性 each(fn)遍历类数组,fn用来处理DOM对象.在fn中this表示正在被遍历的那个DOM对象.fn函数可以添加一个参数i用于表示正在被遍历的DOM对象的下标(从0开始) eq(index):将下标等于index的DOM对象取出来 get():返回一个DOM对象组成的数组 index(obj):返回DOM或jQuery对象在类数组中的下标. <ul> <

JavaScript之jQuery-6 jQuery 类数组的操作

一.jQuery 类数组的操作 类数组简介 - jQuery 对象封装的多个 DOM 对象 - 类: 指的是类似 - 具备自己特有的操作方法 类数组的操作 - length属性 - each(fn)遍历类数组,fn用来处理DOM对象.在fn中this表示正在被遍历的那个DOM对象.fn函数正在被遍历的那个DOM对象.fn函数可以添加一个参数i用于表示正在被遍历的DOM对象的下标(从0开始) - eq(index):将下标等于index的DOM对象取出来 - get(): 返回一个DOM对象组成的

[Javascript]类数组对象为什么不能用for in进行遍历

上来说重点,再废话 类数组对象不能使用for(var key in  elements)进行遍历,这是因为类数组的最后一个哈希的键值对是length:n   n代表数组长度 在Javascript中,我们在DOM树上面获取页面的元素, 例如使用var emls=getElementsByTagName () var emls=querySelectedAll() 获得的elements包含多个dom元素,在学习的过程中,我没有使用for(var i=0;i<elements.length;i++

工具类Arrays.asList()方法把数组转换成集合

工具类Arrays.asList()方法把数组转换成集合 不能使用其修改集合相关的方法,它的add/remove/clear方法会抛出UnsupportedOperationException() 问题分析: 1.测试 被注释的三行可以分别解开注释,运行后确实出现了上述所说的异常 2.看源码 似乎没有问题.往下看 然而实际上我们点进到ArrayList发现,其实ArrayList并不是我们平时用的ArrayList.而是Arrays里面的一个内部类.而且这个内部类没有add,clear,remo

黑马程序员-集合工具类和1.5新特性

集合框架的工具类:collecttions Collections 的方法全是静态的 List没有排序功能,所以java定义了Collections工具类. 比较器无法传给list,只能传给Set.但是集合工具类有此方法 1.排序: comparable:    sort(List<T> list) 根据元素的自然顺序 对指定列表按升序进行排序. comparator:    sort(List<T> list, Comparator<? super T> c) 根据比

集合工具类 - CollectionUtil.java

集合工具类,提供数组转LIST.数组转SET.合并集合.计算笛卡儿积等方法. 源码如下:(点击下载 -  CollectionUtil.java.ArrayUtil.java.commons-lang-2.6.jar.commons-collections4-4.0.jar) 1 import java.util.ArrayList; 2 import java.util.Arrays; 3 import java.util.Collection; 4 import java.util.Link

java集合工具类---Collections/Arrays

/* *Collections用于操作List/Set的工具类 *Arrays用于操作数组的工具类 */ package pack; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.TreeSet; public class Main { pu

Map集合以及Collections集合工具类

一.Collection集合主要特点与Map集合的区别 Collection: 单列集合:有两个子接口 List集合元素是有序的,可以重复的 Set集合元素是无序的,不可以重复 List:元素可重复,有序 ArrayList:底层数据结构是数组,查询快,增删慢,不同步,线程不安全,效率高:没有特殊说明一般使用ArrayList集合: Vector:底层数据结构是数组,查询快,增删慢,同步,线程安全,效率低:有一个elements()特有迭代方法: LinkedList:底层数据结构是链表,查询慢