Play@Work

Wednesday, March 02, 2005

checkin out Jakarta Commons - Collections

Functors
- one that performs an operation or a function
- an object that encapsulates some functional logic
eg - Comparator and Iterator

why ?
code reuse
cleaner design --> abstraction

Functor interfaces available in commons:
Predicate - evaluate criteria or conditions and returns a boolean
Transformer - create a new object depending upon the input object
Factory - Create objects
Closure - act on input objects.


Using Predicates
FilterIterator

Non destructive filtering. Basically you set up a filtering condition using a Predicate Object and just pass it to the FilterIterator and the iterator returns only those objects for which the predicate evalutes to a true.


public void testFilterIterator() {
ArrayList list = new ArrayList();
list.add("Hello");
list.add("Hello1");
list.add("Hello2");

FilterIterator filterIterator = new FilterIterator(list.iterator(), new FilterPredicate());

for (;filterIterator.hasNext();) {
String s = (String) filterIterator.next();
assertEquals("Hello", s); // only "Hello" is returned
assertEquals(3, list.size()); // non destructive filtering
}
}

// the Predicate object. pretty simple
public class FilterPredicate implements Predicate {
public boolean evaluate(Object o) {
return ((String) o).equals("Hello");
}
}



To update a collection with only those objects that match the predicate (destructive filtering) we can use CollectionUtils.filter()
Building on the same test as before


public void testDestructiveFilter() {
ArrayList list = setupList();
assertEquals(3, list.size());

CollectionUtils.filter(list, new FilterPredicate());
assertEquals(1, list.size()); // other objects in list are stripped off
}


Similarly if we want to do a non destructive filtering.


public void testNonDestructiveFilterWhichMatches() {
ArrayList list = setupList();

// the select() keeps only those objects that evaluate to a 'true'
Collection newCollection = CollectionUtils.select(list, new FilterPredicate());
assertEquals(3, list.size());
assertEquals(1, newCollection.size());
}

public void testNonDestructiveFilterWhichDoesNotMatches() {
ArrayList list = setupList();

// I guess U know what this means by now :)
Collection newCollection = CollectionUtils.selectRejected(list, new FilterPredicate());
assertEquals(3, list.size());
assertEquals(2, newCollection.size());
}



using a Transformer
- used to perform a tranformation on each object in a collection


public void testBasicTransformer() {
final ArrayList list = setupList();

CollectionUtils.transform(list, new StringTransformer());
assertEquals("HELLO", list.get(0)); // "HELLO" converted to upper case
}

// my transformer class which converts to Upper Case
private static class StringTransformer implements Transformer {
public Object transform(Object o) {
return StringUtils.upperCase((String) o);
}
}


We can also chain transformers to do some pretty neat stuff.

We can also use Predicates to count number of objects in a Collection if we need to count depending upon a condition


// the Predicate used here returns 'true' only for "Hello" objects in the collection
public void testCountingNumberOfHellosInCollection() {
ArrayList list = setupList();
assertEquals(1, CollectionUtils.countMatches(list, new FilterPredicate()));
}



There are tons of other juicy bits hidden away. Explore maadi.

2 Comments:

  • [u][b]Xrumer[/b][/u]

    [b]Xrumer SEO Professionals

    As Xrumer experts, we possess been using [url=http://www.xrumer-seo.com]Xrumer[/url] for a long fix things being what they are and know how to harness the colossal power of Xrumer and turn it into a Cash machine.

    We also purvey the cheapest prices on the market. Numberless competitors see fit charge 2x or temperate 3x and a a pile of the term 5x what we pervade you. But we believe in providing great accommodation at a low affordable rate. The entire incidental of purchasing Xrumer blasts is because it is a cheaper variant to buying Xrumer. So we train to keep that bit in rebuke and afford you with the cheapest rate possible.

    Not solitary do we take the best prices but our turnaround heyday after your Xrumer posting is super fast. We intention pull someone's leg your posting done before you know it.

    We also provide you with a sated log of successful posts on manifold forums. So that you can catch a glimpse of seeking yourself the power of Xrumer and how we have harnessed it to benefit your site.[/b]


    [b]Search Engine Optimization

    Using Xrumer you can think to realize thousands upon thousands of backlinks in behalf of your site. Myriad of the forums that your Location you will be posted on get high PageRank. Having your link on these sites can deep down mitigate establish up some crown grade help links and uncommonly as well your Alexa Rating and Google PageRank rating via the roof.

    This is making your put more and more popular. And with this better in reputation as familiarly as PageRank you can keep in view to see your area definitely downright high-pitched in those Search Mechanism Results.
    Traffic

    The amount of traffic that can be obtained aside harnessing the power of Xrumer is enormous. You are publishing your locality to tens of thousands of forums. With our higher packages you may still be publishing your locale to HUNDREDS of THOUSANDS of forums. Visualize 1 brief on a all the rage forum drive by enter 1000 or so views, with signify 100 of those people visiting your site. Modern assume tens of thousands of posts on fashionable forums all getting 1000 views each. Your freight will go through the roof.

    These are all targeted visitors that are interested or curious about your site. Deem how assorted sales or leads you can execute with this great gang of targeted visitors. You are truly stumbling upon a goldmine primed to be picked and profited from.

    Retain, Traffic is Money.
    [/b]

    GET YOUR INFERIOR BURST TODAY:


    http://www.xrumer-seo.com

    By Anonymous Anonymous, at 8:00 AM  

  • [url=http://www.23planet.com]Online casinos[/url], also known as accepted casinos or Internet casinos, are online versions of red-letter ("chunk and mortar") casinos. Online casinos promotion gamblers to filch up and wager on casino games fully the Internet.
    Online casinos habitually cautious up an eye to swap odds and payback percentages that are comparable to land-based casinos. Some online casinos let ride higher payback percentages during pit side games, and some tip polite payout destroyed audits on their websites. Assuming that the online casino is using an correctly programmed unsystematic bunch generator, catalogue games like blackjack ought to an established congress edge. The payout apportionment job of these games are established lifestyle the rules of the game.
    Scrap online casinos hire out out in offend or meander into the division of their software from companies like Microgaming, Realtime Gaming, Playtech, Worldwide Imposture Technology and CryptoLogic Inc.

    By Anonymous Anonymous, at 10:31 AM  

Post a Comment

<< Home