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
I guess you can do:
Usage isn’t that clumsy when you use Optional -> transform -> or in one line (assigning transformed optional integer would produce
Optional<Optional<Integer>>).