Introduction to Apache Commons

Of course, let's start with history!

It all started in 1999 with the registration of the "Apache Group" on behalf of the Apache Software Foundation (ASF). The project supported by the foundation was the Apache HTTPD Web Server created between 1995 and 1999.

The same was the Jakarta Project (Jakarta project), which appeared as a result of the collaboration of Sun Microsystems, IBM, Oracle and the guys from Apache. And in 2001, during the work, the development team noticed that they often write the same functionality, sometimes they simply copy it from each other. Such code is called boilerplate. They managed to collect a large amount of code that helped developers, but there was no library to store it.

This is how the Jakarta Commons project was born, in which Java components were added (mostly based on existing code). The project was later renamed to Apache Commons.

More broadly, Apache Commons is “a big collection of little Java utilities”. It is used in many open source projects.

Apache Commons utilities are at the heart of projects such as Apache Tomcat, Struts, Hibernate, and others.

Of course, all this can be connected manually, without a build system (Maven, Gradle), but we will not do this and just add them to our project.

To work with Maven, first add the appropriate dependency:

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>${apache.common.version}</version>
</dependency>

Where ${apache.common.version} is the version of this library.

For Gradle (Groovy):

implementation 'org.apache.commons:commons-lang3:3.12.0'

Popular Apache Commons Libraries

Here is a list of the most used classes and methods:

Apache Commons:Lang

This library contains the following packages:


Packages
org.apache.commons.lang
org.apache.commons.lang.builder
org.apache.commons.lang.enum
org.apache.commons.lang.enums
org.apache.commons.lang.exception
org.apache.commons.lang.math
org.apache.commons.lang.mutable
org.apache.commons.lang.reflect
org.apache.commons.lang.text
org.apache.commons.lang.time

Here you can conveniently and quickly work with strings, reflection, serialization, objects and the system. Let's note the most used methods:

StringUtils

A huge number of methods for manipulating strings.

  • is(Not)Blank/Empty(String) - it's time to forget about this type of check: if (s!=null && s.trim().length()>0) , and there is a good replacement here

StringEscapeUtils

  • (un)escapeSql(String) - Replace PreparedStatment
  • (un)escapeHtml(String) - to process values ​​from HTML

ToStringBuilder

  • reflectionToString(Object) is an implementation of toString() based on reflection. When you remove a field using reflection, the result of the method will change.

EqualsBuilder & HashCodeBuilder

  • reflectionEquals/HashCode(Object) is a good replacement for automatic generation with its own advantage: these two methods take into account structural changes in the object during operation, for example, adding fields

ExceptionUtils

  • getFullStackTrace(Throwable) - output the entire StackTrace as a string

Apache Commons: Collections

Packages
org.apache.commons.collections4
org.apache.commons.collections4.bag
org.apache.commons.collections4.bidimap
org.apache.commons.collections4.collection
org.apache.commons.collections4.comparators
org.apache.commons.collections4.functors
org.apache.commons.collections4.iterators
org.apache.commons.collections4.keyvalue
org.apache.commons.collections4.list
org.apache.commons.collections4.map
org.apache.commons.collections4.multimap
org.apache.commons.collections4.multiset
org.apache.commons.collections4.properties
org.apache.commons.collections4.queue
org.apache.commons.collections4.sequence
org.apache.commons.collections4.set
org.apache.commons.collections4.splitmap
org.apache.commons.collections4.trie
org.apache.commons.collections4.trie.analyzer

A library that perfectly complements the Java SE Collections Framework.

CollectionUtils is a class for convenient work with collections:

    filter/find(Collection, Predicate) - filtering and searching by predicate forAllDo(Collection, Closure) - performs Closure for each element, but this method is deprecated , use Iterator.forEach() is(Not)Empty(Collection) - allows you not to check to null before calling isEqualCollection(Collection, Collection) - helps to compare two collections

There are also many other classes of varying levels of utility. Here and below I list the most commonly used purely in my case.

Apache Commons:IO

Packages
org.apache.commons.io
org.apache.commons.io.comparator
org.apache.commons.io.file
org.apache.commons.io.file.spi
org.apache.commons.io.filefilter
org.apache.commons.io.function
org.apache.commons.io.input
org.apache.commons.io.input.buffer
org.apache.commons.io.monitor
org.apache.commons.io.output
org.apache.commons.io.serialization

Additionally, it helps to work with files in Java:

FileUtils

  • copyDirectory(File, File) - copy directories
  • copyFile(File, File) - copy files
  • listFiles(File, String[], boolean) - list files by extension and recursively
  • readFileToString(File, String)
  • writeStringToFile(File, String)

IOUtils

  • closeQuietly(Reader/Writer/InputStream/OutputStream) - closes the data stream
  • copy(InputStream, OutputStream) - copy from one stream to another