Re: Refactoring into ravioli code
- From: "Daniel T." <daniel_t@xxxxxxxxxxxxx>
- Date: Wed, 16 Jan 2008 23:32:09 -0500
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...
.
- Follow-Ups:
- Re: Refactoring into ravioli code
- From: Bizonho
- Re: Refactoring into ravioli code
- References:
- Refactoring into ravioli code
- From: chris_a_brooks
- Re: Refactoring into ravioli code
- From: Daniel T.
- Re: Refactoring into ravioli code
- From: chris_a_brooks
- Refactoring into ravioli code
- Prev by Date: Re: Business objects, subset of collection
- Next by Date: Re: Business objects, subset of collection
- Previous by thread: Re: Refactoring into ravioli code
- Next by thread: Re: Refactoring into ravioli code
- Index(es):