Providing a Predicate form of Optional.isPresent() to use with Function composition

1,247 views
Skip to first unread message

amertum

unread,
Jan 20, 2014, 11:25:04 AM1/20/14
to guava-...@googlegroups.com
Hi,

What do you think of adding this static util method in Optional ?

public static Predicate<Optional<?>> Optional.isPresent() {...}

which returns the result of optional.isPresent.

I have many use cases of filtering a list of entities by some optional value, such as :

FluentIterable.from(entities).filter(Predicates.compose(Optional.isPresent(), Entity.toSomeValue()));

where
 
public class Entity {
    public Optional<String> getSomeValue(){...}
    public static Function<Member, Optional<String>> toSomeValue() {...}
    private Optional<String> someValue;
}

Of course, I can provide static Predicate method to each of my business entities but generally I prefer to provide static Function method to convert to field as we can compose Function with Predicate to Predicate more easily.

Regards,
Amertum.

Kurt Alfred Kluever

unread,
Jan 20, 2014, 1:54:35 PM1/20/14
to amertum, guava-discuss
Hi Amertum,

Could you use this instead?

FluentIterable.from(Optional.presentInstances(entities)).transform(Entity.toSomeValue());

That being said, I do see a handful of internal users (googlers) who have written an IS_PRESENT Predicate<Optional<T>>. I haven't dug into their code to see if they could be re-written like above.




--
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/dc8e7002-9369-4ca0-b4ad-ccf34f772db0%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
kak

amertum

unread,
Jan 20, 2014, 6:31:18 PM1/20/14
to guava-...@googlegroups.com, amertum
Hi Kurt,

Using Optional.presentInstances(entities), means that the entities is an Iterable of Optional<Entity>. But in my case, it is not the entity that is Optional but some of its fields.
So it means that I should be able to construct an Iterable<Optional<Entity>> from its Optional<FieldType>, which is not easier as filtering entities from a Predicate.

As for your related link, I read it during my search about this subject but I didn't see a solution in it, except for the proposition of the present() Predicate.

I think it is kind of the same solution provided by the method com.google.common.collect.Ordering#onResultOf, ie, a transformation on the behalf of. We sort entities using transformation form of it.

For the present problem, we want to filter entities using transformation form of it, ie, Optional.isPresent Predicate.

A real business use case of my problem is to filter a List of Order, given that order.getMatchingOrder() return an Optional<Order> when there is a matching Order only.
So I expect retrieving orders with matching using : from(orders).filter(compose(Optional.isPresent(), Order.toMatchingOrder())).

Hope it helps. 
Amertum

Kurt Alfred Kluever

unread,
Jan 21, 2014, 5:56:18 PM1/21/14
to amertum, guava-discuss
Thanks for the response, Amertum. We're going to discuss adding this at our next API review (1 week from now).

I'll make sure to circle back once we have that discussion.



For more options, visit https://groups.google.com/groups/opt_out.



--
kak

Kurt Alfred Kluever

unread,
Mar 26, 2014, 7:45:03 PM3/26/14
to amertum, guava-discuss
Sorry for the delay...Kevin communicated the response on the linked bug, but I forgot to update this thread:

--
kak

amertum

unread,
Mar 26, 2014, 7:45:57 PM3/26/14
to guava-...@googlegroups.com, amertum
Hi Krut,

Have you spoke and decided something about this feature ?

Amertum

amertum

unread,
Mar 26, 2014, 7:56:00 PM3/26/14
to guava-...@googlegroups.com, amertum
That was a pretty fast answer. Thanks. Going to migrate to Java 8 now.
Reply all
Reply to author
Forward
0 new messages