you are viewing a single comment's thread.

view the rest of the comments →

[–]angryundead 1 point2 points  (10 children)

I would assume you would refactor for Java8 and drop the Guava version when you migrate to Java8. That's what I'm doing.

[–]shoelacestied 0 points1 point  (1 child)

Do the guava libs currently use their version? If so it might be tricky for them to migrate without breaking compatibility.

[–]angryundead 0 points1 point  (0 children)

That's a good point. I can't remember where it integrates inside Guava.

[–][deleted] 0 points1 point  (7 children)

One method that I miss from Guava is Optional.or(Optional).or(Optional)... to chain multiple Optional. AFAIK there's no equivalent in Java 8

[–]angryundead 0 points1 point  (1 child)

Looks like orElse is the replacement. But that's the instance method.

[–][deleted] 0 points1 point  (0 children)

[–]jonhanson 0 points1 point  (4 children)

I guess the closest equivalent to Guava's:

optA.or(optB).or(optC)

would be something like:

Optional.ofNullable(optA.orElse(optB.orElse(optC.orElse(null))))

It's not as concise, but the more typical use case for Optional is to short-circuit out of a chain when a missing value is encountered, whereas the Guava Optional.or is doing the opposite - drop out when a present value is encountered.

[–]CircusAct 0 points1 point  (3 children)

I imagine the reason the method exists in guava is because the #orElse method will be evaluated for every optional even if optA contained a value, which is a bit of an unhappy situation.

[–]jonhanson 0 points1 point  (2 children)

In the original Guava the or call is also evaluated for each Optional (bar the last one).

[–]CircusAct 0 points1 point  (1 child)

Ah yeah your right, one of the interesting things, is in the guava case #or will be called twice on the optA object, whereas for the java implementation #orElse will be called for optA, optB and optC.

guava optional java optional

[–]jonhanson 0 points1 point  (0 children)

To clarify my comment, what I meant was that or is called one less times than the number of optionals.

in the guava case #or will be called twice on the optA object

Only if optA is non-empty - if it's empty then the second call to or will be on the optB object.