ACMNexus / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

RFP: Predicates.instanceOf #115

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Bonjour,

We frequently use the following predicate designed to check if the input is
an instance of the specified class.
In that such a predicate probably exists among other projects using Google
Collections, would it be possible to add it to the
com.google.common.base.Predicates class?

    public static <T> Predicate<T> instanceOf(final Class<?> klass) {
        return new Predicate<T>() {
            public boolean apply(final T input) {
                return klass.isInstance(input);
            }
        };
    }

Thanks a lot,
Regis.

Original issue reported on code.google.com by rdesgrop...@gmail.com on 9 Jan 2009 at 9:17

GoogleCodeExporter commented 8 years ago
Just wondering: what do you use this predicate for?  Do you use it to filter?  
You
can use the Itera*s.filter(Itera*, Class) overload for that.  But maybe you use 
it
for something else.

I can see how you might find it useful, but it may or may not need to be in the
library given the ease of implementing it.

Original comment by kevin...@gmail.com on 9 Jan 2009 at 4:03

GoogleCodeExporter commented 8 years ago
Hello Kevin,

We do use Iterables.filter(iterable, class) as soon as we have to iterate on a 
subset
of the original iterable.

But, we have pieces of code based on boolean expressions where other
Iterables.functions are used, such as:

if (Iterables.any(changes, instanceOf(InvalidChange.class))) {
    //...
}

static boolean canCommit(final Iterable<Change> changes) {
    return Iterables.all(changes, instanceOf(ValidChange.class));
}

Regards,
Regis.

Original comment by rdesgrop...@gmail.com on 9 Jan 2009 at 5:27

GoogleCodeExporter commented 8 years ago
Thanks for the examples!

I'm still on the fence as to whether this needs to be a pre-fab Predicate.  
Thinking
aloud, I am concerned that if it is, people will use it to filter(), instead of 
using
the right version of filter(), then they'll find their resulting Iterable isn't 
of
the "right" type, then they'll start casting and getting warnings, etc.

Original comment by kevin...@gmail.com on 9 Jan 2009 at 5:58

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
I understand your point of view: you would like that Google Collections' users 
use
them the right way. It's all credit to you.
Nevertheless, I think other misuses of Google Collections' functions exist here 
and
there... so what?
When you provide an API, you give the corresponding documentation, but 
unfortunately
not the corresponding good sense.

So... it's up to you,
Regis.

Original comment by rdesgrop...@gmail.com on 11 Jan 2009 at 5:57

GoogleCodeExporter commented 8 years ago
We got several more requests for this, and we will be adding it soon.

Original comment by kevin...@gmail.com on 14 Feb 2009 at 3:27

GoogleCodeExporter commented 8 years ago

Original comment by kevin...@gmail.com on 18 Mar 2009 at 2:21

GoogleCodeExporter commented 8 years ago

Original comment by kevin...@gmail.com on 24 Mar 2009 at 10:07