Disclaimer: I'm a developper, and, as such, I'm lazy. So please don't expect this blog to be updated very often. Moreover, most of the time I don't really know what I'm talking about (it's not like software development is my job or something), so don't expect to find very useful stuff here either. Basically it's just some sort of semi public notepad with a few problems / things I encountered. Oh, and I'm French, so my english wont be any good... That said, nice reading!

Usefull generics reminder

How could I not see this till now?

Here is a very usefull piece of info that I stumbled upon while reading Play Framework doc:

The play.db.jpa.Model defines a set of generic methods. These generic methods use a type parameter to specify the method’s return type. When using those methods, the concrete type to be used as return value is derived from the invocation context using type inference.

For example, the findAll method is defined as:

<T> List<T> findAll();

And you use it as:

List<Post> posts = Post.findAll();

Here the Java compiler resolves the actual type of T using the fact that you assign the method result to a List. So T is resolved as a Post type.

Unfortunately, this doesn’t work if the generic method’s return value is directly used as a parameter for another method invocation or used in a loop. So the following code fails with a compiler error saying “Type mismatch: cannot convert from element type Object to Post”:

for(Post p : Post.findAll()) {
    p.delete();
}

Of course you can resolve this issue using a temporary local variable, as:

List<Post> posts = Post.findAll(); // type inference works here!
for(Post p : posts) {
    p.delete();
}

But wait, there is a better way. You can use a practical but somewhat unknown Java language feature, which makes the code shorter while more readable at the same time:

for(Post p : Post.<Post>findAll()) {
    p.delete();
}

Tags: , , ,

Switching to mac : the good, the bad & the ugly

It’s been more than two years now that I’ve replaced my old winxp home desktop with a 13″ unibody Macbook and each time someone asks me if I’m satisfied I can’t help to answer something like “Eerrr, not fully satisfied”. Here is an article I’ve started a year ago summarizing my thoughts on this topic :

1977 - Apple logo

1977 - Apple logo designed by Rob Janoff

Read the rest of this entry »

Tags: , , ,

Enforcing architecture rules with a unit test…

… is neat!

At the beginning of a project, everybody in the team agrees to make things properly, identify layers, isolate responsibilities, code with defined formatting rules, etc. But after a few weeks, rules tend to be bent and laxism is all over the place. Formatting rules can be checked at code-time using Checkstyle Eclipse plugin, test coverage can be monitored using Maven Cobertura plugin (build-time) or EclEmma Eclispe plugin (test-time), but what can one use to enforce a certain set of architecture rules?

yum, multi-layered cake!

Read the rest of this entry »

Tags: , , , ,

Formatting Rules Clashes

I was modifying someone else’s code the other day and I was looking at something I find awful :

if (condition == true)
{
    someThing();
}
else
{
    someOtherThing();
}

And I was thinking “ok, this is ugly but whatever…” but if I had to work full-time on this project, I don’t think I could bear this syntax for long.

Maybe I’m an integrist (well, I am, mosdef!) but I think it’s a legitimate question: what should we do when the members of a team don’t agree on a common set of formatting rules? Should the technical leader dictate his choice? I really don’t know. Fortunately enough, Java coders are often on the same page on this topic… (the example above is from a Flex coder… Eww :P )

if (condition == true) {
    someThing();
} else {
    someOtherThing();
}

That’s way better !

Tags: , ,

Some interesting reading (on IoC)

TheServerSide has recently published part 2 of an article explaining the Contexts & Dependency Injection (CDI) in JEE6. It seems to be very interesting.

care for a little injection?

I particularly like this extract from part 1 :

If you are familiar with Spring IoC, CDI is likely to feel more type-safe, futuristic and annotation-driven. Seam developers will find that CDI has a lot more advanced features. CDI is perhaps more geared towards enterprise development than Guice is. Finally, Java EE 5 developers will likely feel that CDI is much more complex at first glance but will see that most of the complexity is well justified and can be ignored when not needed.

Also, I never really looked into Guice and decided to do so this morning. The documentation is really well written and helpful.

Tags: , , , ,

quick 90 percentile calculation

Recently I have been working on a internal component providing features similar to a rule engine. This component is embedded into live applications and as its behaviour can be tricky and time consuming, we had to provide a way to monitor method calls to this inner component.

90 percentile

We used the JETM library to monitor method calls and it was quite quick and easy to do so. Integration with Spring framework is also facilitated with the use of aspects and the ability to define interceptors around preexisting beans. But I think I’ll do a whole post about this lib.

But, out of the box, this lib only provided us min, max, avg and total time elapsed metrics. And this is satisfactory for most uses I guess but we are working on a complex module, with a lot of caches and having the first call taking a lot of time and affecting the average was not so representative. So we thought about adding a 90 percentile metric but the problem was that we did not want to keep every measurement point in memory (for obvious reasons).

Ow, by the way, for those who don’t know (like me, three days ago) what 90 percentile means : the 90 percentile is such as 90% of execution samples take less time than it. Meaning that if I have a 90 percentile of 480ms for my method doSomething(), I know that 90% of the time, executing doSomething() will take less thant 480ms.  It is then possible to define xx percentile, where xx is any number between 0 and 100. And as you may have guessed, the 50 percentile is called the median.

So my coworker thought of a way to calculate it, roughly, but effectively, just with a little HashMap. Maybe it is a standard, widespread way to do it, but I found it clever, so here it is.

Read the rest of this entry »

Tags: , , , ,

pic-reporter, an EXIF extractor / stats generator

Recently I realized that Lightroom (Adobe image organizer for photographers) did not allow to display easily which focal lengths were mostly used and which ones are not (a focal length is the equivalent to a zoom factor, in a nutshell). It did not really annoy me but a coworker said to me “it would not be complicated to get the data from the files and dump it into a CSV file or whatever”, and I had a free week end, alone, free to geek all I wanted!

reflex

So I spent I couple hours coding a small command line tool which would do exactly this (in java, since I cannot do anything else, and I am lazy). The focal length is one of the many fields that can be stored in a JPG image as EXIF metadata (EXIF is to an image what ID3 tags are to music files). So basically what I had to do is get a list of all my images, and then find a nice little library that would read this EXIF data for me. And that’s exactly what I did. But I learnt a few things, and maybe you will too…

Read the rest of this entry »

Tags: , , , , ,

log4jAdmin.jsp

Just a quick post to archive the code for a log4jadmin page I found very useful. The goal is to be able to change logger levels on the fly, without having to restart the application. It is a “all-included” JSP and the only dependency is to have log4j lib on the classpath.

I think I’ve found it on a blog on the interwebs (it’s a common issue) and customized it a bit to be able to manage package level loggers.

The JSP code is in the full post..

Read the rest of this entry »

Tags: , , , ,

JXyDiff : xml diff the classy way

strange logo

JXyDiff (strange) logo

I was recently asked the question of what tool to use in order to compare two xml files. I had no good answer to give… I mainly use notepad++ to deal with xml files. And what I do is only browsing them looking for a particular tag, searching them and so on. I don’t need a big XmlSpy to do that. So I don’t know any XML-manipulating tool, even less one that could do nice diffs.
Read the rest of this entry »

Tags: , , , ,

adding files to svn recursively

A quick tip concerning svn command line, because I’ve been using it a little recently.

subversion logo
Read the rest of this entry »

Tags: , ,