Re: Refactoring into ravioli code



chris_a_brooks@xxxxxxxxxxxxx wrote:

You've just used the fact Smalltalk provides this kind of language
construct to help you.

I don't know too much about Java generics so I might be missing
something, but here are the beginnings of the refactoring that is
something like the SmallTalk example.

interface Pred {
boolean isTrue( Object o );
}

public class Selector
{
public static Collection select( Collection c, Pred pred )
throws Exception {
Collection result = collection.getClass().newInstance();
Iterator it = collection.iterator();
while ( it.hasNext() ) {
Object each = it.next();
if ( pred.isTrue( each ) ) {
result.add( each );
}
}
return result;
}
}

It would be used like this:

Collection matches = new Selector().select( allStrings, new Pred(){
public boolean isTrue( Object o ){
return ((String)o).startsWith(matchMe);}});

Granted, Java anonymous classes are a bit ungainly...
.