FilterQ - A lightweight filtering API for Iterable Objects.
Thursday, July 30th, 2009
Wow, version 0.3 of FilterQ has been released and is available at filterq.googlecode.com. Several cool features have been added, several bugs have been fixed. But the best of all things, at least for me, is that I am using it in my JUnit 4.5 extensions project. FilterQ is my lightweight filtering API for Iterable Objects [...]
Varargs Null Checking
Friday, May 8th, 2009
VarArgs Null checking? This should be easy, right? All we need to do is put a “VarArgs”.length == 0 check before the important method’s body and we are set (See Checker class), correct?
class Checker {
public void check(Arg… args){
if(args.length == 0) return;
final List<Arg> someArgs = Arrays.asList(args);
for(Arg each: someArgs){
System.out.println("checked:" + each.toString());
}
}
}
class Arg{
private final String [...]
Reading files a la for-each loop.
Tuesday, January 27th, 2009
This post will show you how to use the for-each loop for reading files. This approach tried to replace the following approach
try {
BufferedReader in = new BufferedReader(new FileReader("infilename"));
String str;
[...]
More on Filters as First-class functions.
Friday, October 17th, 2008
By expanding (more like enhancing ) the filter pattern described here and borrowing some of the beauties of the matcher pattern in Guice, I coded a more concise version of this filter pattern. Now this pattern’s implementation has a fluent API for combining other filters together, a place where all filters can be defined, [...]
Class>.cast(..) and Generics - a powerful combination
Saturday, July 12th, 2008
As you know when it comes to casting a la Java 1.4 a developer will have to write, typically, something like this:
Object a = "iron man";
String b = (String)a;
Well, there is nothing wrong with this code. However, it is not pretty :). With Java 5.0 the casting mechanism is more explicit and nicer. [...]
Documenting Exceptions via Factory Methods
Wednesday, May 28th, 2008
Jesse described a technique for handling methods that always throws. This is a very interesting technique. Having written that, would it not be nice to encourage the use of this technique, in our Java coding conventions, as a way to document DRY exceptions? Honestly, I’d’ think we should. It would prevent us from repeating the [...]
Do you need to cache your objects?
Wednesday, May 14th, 2008
I’ve found the Cache Management Pattern very useful in more than a couple of projects that needed a simple caching mechanism. Now that we have Generics at our disposal, I think this pattern deserves a tiny change. Something like a generic structure or code that we can follow or use every time we need to [...]
DRY benchmarking….
Tuesday, May 13th, 2008
Every time we try to write a benchmark for particular functionality, we habitually specify a load (max iterations), write a for-loop, set timers before and after the loop, and call the method of interest inside this loop. This typically involves copying & pasting from another benchmark and adapting the current benchmark to your needs. Why [...]
No more downcasting via “Recursive Bounds”
Sunday, May 4th, 2008
I recently coded a fairly tiny application that made use of the MVC pattern. One of the things that I noticed while I was writing it was that I was down-casting a lot. Imagine something like this:
Example
// main type
interface Model {
void someMethod();
}
// implementation
class Mixer implements Model {
public void anotherMethod(){
[...]
Binding data to Java enums…
Friday, April 25th, 2008
Paul Stovell blogged about binding data to enums in C#. Here is how I would implement this technique in Java….
First, an annotation that I will use to bind data to my enum
@Target(Element.FIELD)
@Retention(RententionPolicy.RUNTIME)
public @interface Config {
String name();
String alias();
}
Then, here is my enum
public enum Role {
@Config(name="James Bond", alias = "Agent 007")
AGENT;
// static attribute that will allow us [...]