Another interesting API to use is the Google Guava. It has a number of features for this type of task.
An example of use would be:
import com.google.common.base.Strings;
Strings.isNullOrEmpty(""); // retorna true para vazia
Strings.isNullOrEmpty(" ".trim()); // retorna true para string em branco
There are several other functionalities for primitives, and other concepts such as the use of:
Precontitions:
Boolean state treatment of some condition without Guava:
if (estado!= Estado.INCOMPLETO) {
throw new IllegalStateException(
"Esse Objeto está em um estado " + estado);
}
It would be simpler with Guava, without using ifs:
import com.google.common.base.Preconditions;
Preconditions.checkState(
estado == Estado.PLAYABLE, "Esse Objeto está em um estado %s", estado
);
Charmatcher:
Determines whether a character is a:
CharMatcher.WHITESPACE.matches(' ');
CharMatcher.JAVA_DIGIT.matches('1');
Or using a specific Factory method like:
CharMatcher.is('x')
CharMatcher.isNot('_')
CharMatcher.oneOf("aeiou").negate()
CharMatcher.inRange('a', 'z').or(inRange('A', 'Z'))
Detre many other features in a lib of only 2.1KB. That even had contribution from @Josh Block.
More information:
Infoq Br - google-Guava
is worth reading: Avoiding "!=null" comparison in Java
– Math