3

Is there in Cactoos framework some Text decorator (or maybe some other way), which replace null string with empty string? Like Strings.nullToEmpty function in Google Guava.

I have found NoNulls decorator, but I need just replacement without throwing exception.

So it must look like this:

String someNullString = null; 
new StrictEmptyText(
 new TextOf(someNullString) // this row produces NPE for now
).asString(); // ""

Thanks a lot for helping.

maximusKon
  • 136
  • 9

2 Answers2

3

No, there's no Text implementation that does this for you in a direct way.

Using pure cactoos:

new TextOf(
  new UncheckedScalar<>(
    new Ternary<>(
      someNullString != null,
      someNullString,
      ""
    )
  ).value()
)
George Aristy
  • 1,373
  • 15
  • 17
0

You can use Optional::ofNullable from Java 8 like so :

String str = Optional.ofNullable(someNullString)
        .orElse(""); // return empty if someNullString is null or someNullString if not null
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140