「工具」Apache Commons


Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动。我选了一些比较常用的项目做简单介绍。

Commons BeanUtils

针对Bean的一个工具集。由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装。 

一个比较常用的功能是Bean Copy,也就是copy bean的属性。如果做分层架构开发的话就会用到,比如从PO(Persistent Object)拷贝数据到VO(Value Object)。

pom依赖:


    commons-beanutils
    commons-beanutils
    1.9.4

常用类:

  • BeanUtils

Commons Codec

这个工具是用来编码和解码的,包括Base64,URL,Soundx等等。

pom.xml依赖:



    commons-codec
    commons-codec
    1.15

常用类:

  • Base64编码和解码:Base64
  • Hex编码和解码:Hex
  • 加密工具类:DigestUtils
  • URL编码和解码:URLCodec 

Commons Collections

可以把这个工具看成是java.util的扩展,主要是集合的处理、条件判断等。

pom.xml依赖:



    org.apache.commons
    commons-collections4
    4.4

常用类:

  • CollectionUtils:

常用方法说明:

/**
 * 1、除非元素为null,否则向集合添加元素
 */
CollectionUtils.addIgnoreNull(personList,null);
/**
 * 2、将两个已排序的集合a和b合并为一个已排序的列表,以便保留元素的自然顺序
 */
CollectionUtils.collate(Iterable<? extends O> a, Iterable<? extends O> b)
/**
 * 3、将两个已排序的集合a和b合并到一个已排序的列表中,以便保留根据Comparator c的元素顺序。
 */
CollectionUtils.collate(Iterable<? extends O> a, Iterable<? extends O> b, Comparator<? super O> c)
/**
 * 4、返回该个集合中是否含有至少有一个元素
 */
CollectionUtils.containsAny(Collection<?> coll1, T... coll2)
/**
 * 5、如果参数是null,则返回不可变的空集合,否则返回参数本身。(很实用 ,最终返回List EMPTY_LIST = new EmptyList<>())
 */
CollectionUtils.emptyIfNull(Collection collection)
/**
 * 6、空安全检查指定的集合是否为空
 */
CollectionUtils.isEmpty(Collection<?> coll)
/**
 * 7、 空安全检查指定的集合是否为空。
 */
CollectionUtils.isNotEmpty(Collection<?> coll)
/**
 * 8、反转给定数组的顺序。
 */
CollectionUtils.reverseArray(Object[] array);
/**
 * 9、差集
 */
CollectionUtils.subtract(Iterable<? extends O> a, Iterable<? extends O> b)
/**
 * 10、并集
 */
CollectionUtils.union(Iterable<? extends O> a, Iterable<? extends O> b)
/**
 * 11、交集
 */
CollectionUtils.intersection(Collection a, Collection b)
/**
 *12、 交集的补集(析取)
 */
CollectionUtils.disjunction(Collection a, Collection b)

Commons Configuration

这个工具是用来帮助处理配置文件的,支持很多种存储方式。

pom.xml依赖:



    org.apache.commons
    commons-configuration2
    2.7

常用类:

  • PropertiesConfiguration
  • Configuration 

Commons FileUpload

文件上传功能。

pom.xml依赖:



    commons-fileupload
    commons-fileupload
    1.4

常用类:

  • ServletFileUpload
  • DiskFileItemFactory
  • FileItem

Commons IO

IO操作。

pom.xml依赖:


    commons-io
    commons-io
    2.11.0

常用类:

  • IOUtils 包含一些工具类,用于处理读,写和拷贝,这些方法基于 InputStream, OutputStream, Reader 和 Writer工作;
  • FileUtils 包含一些工具类,它们基于File对象工作,包括读,写,拷贝和比较文件;
  • FilenameUtils包含一些工具类,它们基于文件名工作而不是File对象。这个类旨在 在Unix和Windows环境下保持一致,帮助在两个环境下过渡(如从开发环境到生成环境);
  • FileSystemUtils包含一些工具类,基于文件系统访问功能不被JDK支持。目前,只有一个方法就是得到驱动器空余空间。注意这使用命令行而不是 native code;
  • EndianUtils 包含静态方法来交换Java基本类型和流的字节序;
  • SwappedDataInputStream实现了DataInput接口。可以从文件中读取非本地字节序;

Commons Lang

这个工具包可以看成是对java.lang的扩展。提供了诸如StringUtils, StringEscapeUtils, RandomStringUtils, Tokenizer, WordUtils等工具类。

pom.xml依赖:


    org.apache.commons
    commons-lang3
    3.12.0

常用类:

  • StringUtils 字符串的处理类
  • RandomStringUtils 随机数生成类
  • NumberUtils 数字类
  • ArrayUtils 数组类
  • DateUtils 日期类

Commons Net

封装了很多网络协议。

pom.xml依赖:


    commons-net
    commons-net
    3.8.0

Commons Validator

用来帮助进行验证的工具。比如验证Email字符串,日期字符串等是否合法。

pom.xml依赖:



    commons-validator
    commons-validator
    1.7

Commons Virtual File System

提供对各种资源的访问接口。支持的资源类型包括。

这个包的功能很强大,极大的简化了程序对资源的访问。

pom.xml依赖:



    org.apache.commons
    commons-vfs2
    2.9.0

相关