HelenBlack / lambdaj

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

Create class Predicate to make implementing "anonymous closures" easier #58

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Currently if I want to provide "predicate" to Lambda.select or 
LambdaCollections.retain as an anonymous class I must implement two methods 
like this:

{{{
new TypeSafeMatcher<String>() {

    @Override
    public boolean matchesSafely(String arg) {
        return some check;
    }

    @Override
    public void describeTo(Description description) {
        throw new UnsupportedOperationException("Don't care about this method!");
    };
}}}

This is very verbose + matchesSafely is really bad name for predicate check ;-) 
+ makes me feel bad leaving describeTo unimplemented every time :-)

What I propose is to add the following (or similar) class so using "anonymous 
closures" would be easier:

{{{
public abstract class Predicate<T> extends TypeSafeMatcher<T> {

    @Override
    public boolean matchesSafely(T arg) {
        return isTrueFor(arg);
    }

    @Override
    public void describeTo(Description description) {
        throw new UnsupportedOperationException("Don't care about this method!");
    }

    public abstract boolean isTrueFor(T arg);

}
}}}

If isTrueFor is too fancy I suggest apply as in Google Guava :-)

Original issue reported on code.google.com by kopperni...@gmail.com on 28 Dec 2010 at 2:22

GoogleCodeExporter commented 8 years ago
The best way to implement your own predicate is to write an anonymous inner 
class extending ch.lambdaj.function.matcher.LambdaJMatcher. In this way you are 
only required to implement matches method. Moreover by extending that class you 
can combine two or more predicates by using the or(Matcher<T> matcher) and 
and(Matcher<T> matcher) provided by that class. 

Original comment by mario.fu...@gmail.com on 9 Jan 2011 at 4:43

GoogleCodeExporter commented 8 years ago
Well, LambdaJMatcher.matches is not type safe, so this is not optimal choice 
for me. I really don't want to write additional cast I implement a predicate. I 
use code folding in IntelliJ to fold verbose anonymous class syntax. Making it 
more verbose by adding cast every time strikes out all the benefits of cold 
folding and using "poor man's" lambda expressions.

Maybe you could make LambdaJMatcher to extend TypeSafeMatcher instead of 
BaseMatcher?

Thx,
koppernickus

Original comment by adamcze...@gmail.com on 9 Jan 2011 at 5:11

GoogleCodeExporter commented 8 years ago

Original comment by mario.fu...@gmail.com on 9 Jan 2011 at 6:54

GoogleCodeExporter commented 8 years ago
I didn't change the LambdaJMatcher in order to keep backward compatibility. I 
added a class Predicate extending it (basically a type safe LambdaJMatcher), 
having an apply() abstract method that has to be overridden in order to define 
the matching condition.

It will be available starting form release 2.3.2

Original comment by mario.fu...@gmail.com on 11 Jan 2011 at 1:23