BackendBench
Coffee

Unravel › Java concepts › Libraries and tooling

Java language

Apache Commons helpers

Apache Commons (Lang, Collections, IO, CSV, Codec) offers small null-safe helpers: StringUtils.isBlank(s), CollectionUtils.isEmpty(list), FileUtils.readFileToString, IOUtils.copy, EqualsBuilder, and a CSV parser.

Learn these first

At a glance

Mental model

The drawer of kitchen gadgets every Java project ends up with: none is essential, but each handles an awkward case (null, empty, blank, encoding) in one call.

What happens, in order
  1. 1StringUtils.isBlank(s) is true for null, empty or only spaces; StringUtils.isEmpty, trimToEmpty, capitalize, join, leftPad, substringBetween, defaultIfBlank, abbreviate avoid null checks
  2. 2CollectionUtils.isEmpty(c) handles null; MapUtils.isNotEmpty(m); ArrayUtils.contains(arr, x); ObjectUtils.defaultIfNull; NumberUtils.toInt("x", 0) parses with a default instead of throwing
  3. 3FileUtils.readFileToString(f, UTF_8), FileUtils.copyDirectory, IOUtils.toString(in, UTF_8), IOUtils.closeQuietly cover file and stream chores
  4. 4EqualsBuilder, HashCodeBuilder, ToStringBuilder build equals, hashCode and toString from fields; Pair.of(a, b) is a simple pair; commons-csv (CSVFormat.DEFAULT.parse(reader)) reads CSV with quotes and escapes correctly
Remember
Watch out
Parsing CSV with splitUsing reflection-based equals/hashCode in hot pathsAdding the whole library for one helper that the JDK now has
Example (highlighted lines explain it)
import org.apache.commons.lang3.StringUtils;import org.apache.commons.lang3.math.NumberUtils;import org.apache.commons.collections4.CollectionUtils;import java.util.List;public class Clean {  static String name(String raw) { return StringUtils.capitalize(StringUtils.trimToEmpty(raw).toLowerCase()); }  static boolean missing(String s) { return StringUtils.isBlank(s); }  static int qty(String s) { return NumberUtils.toInt(s, 1); }  static boolean empty(List<String> items) { return CollectionUtils.isEmpty(items); }}
  1. line 1 uses StringUtils, the core of Apache Commons helpers
  2. line 2 uses NumberUtils, the core of Apache Commons helpers
  3. line 3 uses CollectionUtils, the core of Apache Commons helpers
  4. line 6 uses StringUtils, the core of Apache Commons helpers
  5. line 7 uses StringUtils, the core of Apache Commons helpers
  6. line 8 uses NumberUtils, the core of Apache Commons helpers
Read the full explanation (about 1 min)

Apache Commons (Lang, Collections, IO, CSV, Codec) offers small null-safe helpers: StringUtils.isBlank(s), CollectionUtils.isEmpty(list), FileUtils.readFileToString, IOUtils.copy, EqualsBuilder, and a CSV parser.

A mental model

The drawer of kitchen gadgets every Java project ends up with: none is essential, but each handles an awkward case (null, empty, blank, encoding) in one call.

How it works

StringUtils.isBlank(s) is true for null, empty or only spaces; StringUtils.isEmpty, trimToEmpty, capitalize, join, leftPad, substringBetween, defaultIfBlank, abbreviate avoid null checks.

CollectionUtils.isEmpty(c) handles null; MapUtils.isNotEmpty(m); ArrayUtils.contains(arr, x); ObjectUtils.defaultIfNull; NumberUtils.toInt("x", 0) parses with a default instead of throwing.

FileUtils.readFileToString(f, UTF_8), FileUtils.copyDirectory, IOUtils.toString(in, UTF_8), IOUtils.closeQuietly cover file and stream chores.

EqualsBuilder, HashCodeBuilder, ToStringBuilder build equals, hashCode and toString from fields; Pair.of(a, b) is a simple pair; commons-csv (CSVFormat.DEFAULT.parse(reader)) reads CSV with quotes and escapes correctly.

Going deeper

Many helpers now have JDK equivalents: String.isBlank(), strip(), Objects.equals, Objects.hash, Files.readString, records and Lombok instead of the *Builder classes. Use the JDK where it exists to avoid a dependency.

Do not parse CSV with line.split(","): quoted fields containing commas and line breaks break it. Use a CSV library.

Null-hiding helpers can hide bugs: a method that returns "" for null spreads the problem. Decide where nulls are allowed, and use the helpers at that boundary.

Under the hood

They are plain static methods written for null safety and edge cases; the reflection-based builders (EqualsBuilder.reflectionEquals) are slow and best avoided in hot code.

Common mistakes

  • Parsing CSV with split.
  • Using reflection-based equals/hashCode in hot paths.
  • Adding the whole library for one helper that the JDK now has.

How Unravel recognises it

When you paste code, Unravel looks for these signs of apache commons helpers. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Apache Commons helpers?

Apache Commons (Lang, Collections, IO, CSV, Codec) offers small null-safe helpers: StringUtils.isBlank(s), CollectionUtils.isEmpty(list), FileUtils.readFileToString, IOUtils.copy, EqualsBuilder, and a CSV parser.

What are the most common mistakes with Apache Commons helpers?

Parsing CSV with split. Using reflection-based equals/hashCode in hot paths. Adding the whole library for one helper that the JDK now has.

How can I see Apache Commons helpers in real code?

Paste your own Java into Unravel and it will point out where apache commons helpers appears, explain how it works at the detail level you choose, and draw the flow of the code around it.