Please add the following function to Strings in order to allow testing for non-empty strings in Streams or Optionals:
public static boolean nonEmpty(@Nullable String s) {
return s != null && !s.isEmpty();
}
This follows the pattern of Objects.nonNull.
I believe that Strings.isNullOrEmpty() does what you want, although the sense is reversed: re/api/docs/com/google/common/base/Strings.html#isNullOrEmpty-java.lang.String-
True, semantically the proposed function is the negation of Strings.isNullOrEmpty, but I'd like to .filter(Strings::nonEmpty) (using the :: shorthand notation).
I can see the desire for a single method reference in the filter, but I'd be concerned about naming. Guava has been pretty firm about not _conflating_ nullness and emptiness...which would mean, what, neitherNullNorEmpty? A simpler possibility might be a notEmpty method, in which case you could do .filter(Objects::nonNull).filter(Strings::nonEmpty), or the like.
I can understand wanting to do that, but I think it's highly unlikely that the Guava team is going to introduce a new method whose semantics are the negation of an existing method to save people from having to do this:
filter(s -> !isNullOrEmpty(s))
I mean, method references are nifty but lambdas make this pretty painless IMO.
Duplicate of #3064?
Also note that since Java 11 there is Predicate.not(Predicate), so you can negate/filter like this too:
filter(Objects::nonNull).filter(not(String::empty))