JoSQL is a pretty addictive API for querying java.util.Collection
classes. The more I use it, the more I like it.
This weekend I had a scenario where my Collection
was full of objects implementing a custom Event
interface - some of them SystemEvent
, and some of them MonitorEvent
(and one day, potentially, a host of others). MonitorEvent
s have a bunch of additional routines they expose which I needed to query on.
So, how do you filter out all the MonitorEvent
instances out of the Event
collection, then apply a query only relevant to MonitorEvent
methods so you can extract only a subset of those that match your criteria? First you talk to Gary at JoSQL support… And then you write three lines of JoSQL…
Query q.parse("SELECT * from a.b.c.MonitorEvent " + " where class.name = '" + MonitorEvent.class.getName() + "' " + " and orgId = " + orgId + " LIMIT " + max); // eventQueue is my List of Event objects QueryResults qr = q.execute(eventQueue); List myList = q.getResults();
Of course, there’s actually a bunch of other ways to do this. I could add a getType()
method on my Event object, and be done with the class filter. Just query for objects matching the given event type (but that might not make semantic sense depending on what an Event represents).
Another option Gary suggested was to write a custom JoSQL function which does an instanceOf() check (the ANDs get applied before the cast, so life is all good). In fact, he mentioned this feature might even make it in 1.3. Too cool.
Anyways, really happy with the expressiveness JoSQL gives me to do all sorts of collection querying. Great stuff. Loving that JoSQL goodness…