Search code examples
javagenericsguava

Guava Optional type, when transformation returns another Optional


I have a method that procudes an Optional<String>

But this String must be parsed at another application level as Integer or Long.

This I have a Function<String, Integer> that can be applied on the String, to produce an Integer. This transformation can fail because the String may not be an Integer parsable value.


I would like to return Optional when the transformation fails, instead of throwing a parsing exception.

I can't make the STRING_TO_INTEGER_FUNCTION return null, because it is not allowed by Guava:

Exception in thread "main" java.lang.NullPointerException: Transformation function cannot return null.

Thus the only thing I can do is having a Function<String,Optional<Integer>> but then I get as final result an Optional<Optional<Integer>> which isn't really cool because I may have another transformations to apply on it.


Does someone know how can I do something like that in Guava?

Optional.of("Toto").transform(STRING_TO_INTEGER_FUNCTION) = // Optional<Integer> ?

Thanks


Solution

  • I guess you can do:

    public static void main(final String[] args) {
      final Optional<Integer> valid = Optional.of("42")
          .transform(STR_TO_INT_FUNCTION)
          .or(Optional.<Integer>absent());
      System.out.println(valid); // Optional.of(42)
      final Optional<Integer> invalid = Optional.of("Toto")
          .transform(STR_TO_INT_FUNCTION)
          .or(Optional.<Integer>absent());
      System.out.println(invalid); // Optional.absent()
      final Optional<Integer> absent = Optional.<String>absent()
          .transform(STR_TO_INT_FUNCTION)
          .or(Optional.<Integer>absent());
      System.out.println(absent); // Optional.absent()
    }
    
    private static final Function<String, Optional<Integer>> STR_TO_INT_FUNCTION =
        new Function<String, Optional<Integer>>() {
          @Override
          public Optional<Integer> apply(final String input) {
            return Optional.fromNullable(Ints.tryParse(input));
          }
        };
    

    Usage isn't that clumsy when you use Optional -> transform -> or in one line (assigning transformed optional integer would produce Optional<Optional<Integer>>).