Guava: CharMatcher.countIn should account for null sequence

Created on 24 Dec 2018  路  1Comment  路  Source: google/guava

The documentation for the CharMatcher.countIn method does not state the expected behaviour if the input sequence is null. The user may expect the method to return 0 for such a case but the implementation throws NullPointerException (which could also be considered as a valid output). This ambiguity should be explicitly clarified in the documentation or else handled well in the code.

status=working-as-intended

Most helpful comment

"Guava's utilities are designed to fail fast in the presence of null rather than allow nulls to be used, so long as there is a null-friendly workaround available" (wiki/UsingAndAvoidingNullExplained)

I would say that NPE is expected by default.

As for workaround, you can use Strings.nullToEmpty to normalize your string before passing it to CharMatcher.countIn.

>All comments

"Guava's utilities are designed to fail fast in the presence of null rather than allow nulls to be used, so long as there is a null-friendly workaround available" (wiki/UsingAndAvoidingNullExplained)

I would say that NPE is expected by default.

As for workaround, you can use Strings.nullToEmpty to normalize your string before passing it to CharMatcher.countIn.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gissuebot picture gissuebot  路  5Comments

thecoop picture thecoop  路  4Comments

dongritengfei picture dongritengfei  路  3Comments

gissuebot picture gissuebot  路  3Comments

Lysergid picture Lysergid  路  4Comments