摘要 工具类是Java开发过程中使用广泛,而且还能提高开发效率的一种方法。
集合操作工具类: 1.判断是否为空:isEmpty() 2.判断是否非空:isNotEmpty()
import org.apache.commons.collections.CollectionUtils;
import java.util.Collection;
public class CollectionUtil {
/**
* 判断集合是否非空
*/
public static boolean isNotEmpty(Collection<?> collection) {
return CollectionUtils.isNotEmpty(collection);
}
/**
* 判断集合是否为空
*/
public static boolean isEmpty(Collection<?> collection) {
return CollectionUtils.isEmpty(collection);
}
}
类加载器工具类: 1.获取类加载器:Thread.currentThread().getContextClassLoader() 2.获取类路径: getClassPath() 3.加载类文件 4.异常处理 5.判断类型码
import java.net.URL;
public class ClassUtil {
/**
* 获取类加载器:<code>Thread.currentThread().getContextClassLoader()</code>
*/
public static ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
/**
* 获取类路径
*/
public static String getClassPath() {
String classpath = "";
URL resource = getClassLoader().getResource("");
if (resource != null) {
classpath = resource.getPath();
}
return classpath;
}
/**
* 加载类(将自动初始化)
*/
public static Class<?> loadClass(String className) {
return loadClass(className, true);
}
/**
* 加载类
*/
public static Class<?> loadClass(String className, boolean isInitialized) {
try {
return Class.forName(className, isInitialized, getClassLoader());
} catch (ClassNotFoundException e) {
throw new RuntimeException("加载类出错:" + e.getMessage(), e);
}
}
/**
* 是否为 int 类型(包括 Integer 类型)
*/
public static boolean isInt(Class<?> type) {
return type.equals(int.class) || type.equals(Integer.class);
}
/**
* 是否为 long 类型(包括 Long 类型)
*/
public static boolean isLong(Class<?> type) {
return type.equals(long.class) || type.equals(Long.class);
}
/**
* 是否为 double 类型(包括 Double 类型)
*/
public static boolean isDouble(Class<?> type) {
return type.equals(double.class) || type.equals(Double.class);
}
/**
* 是否为 String 类型
*/
public static boolean isString(Class<?> type) {
return type.equals(String.class);
}
}