Another interesting API to use is the Google Guava . It has a number of features for this type of task.
An example usage 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 features for primitives, and other concepts such as the use of:
Precontitions:
Treatment of Boolean state 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 const such as:
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'))
Stop many other features on a lib of only 2.1KB. That even had contribution of @Josh Block.
More information:
InfoQ Br - google-guava