Generics
Listen to this article
Now I'm sure you're all bored to tears with yet another blog on generics but I felt it was only fitting to convert all my example code.
So, here is what the immutable collection examples turned out like:
public class Component {
private final Collection<Component> _subComponents;
public Component(Collection<Component> subComponents) {
Assert.isTrue(subComponents != null, "subComponents can't be null!");
_subComponents = Collections.unmodifiableCollection(
Arrays.asList(subComponents.toArray(new Component[subComponents.size()])));
}
public Collection<Component> getSubComponents() {
return _subComponents;
}
}
Interesting. Because I wanted to use Collections.unmodifiableCollection(Collection<T>) it now needs to know the types. So I had to use the T[] Collection.toArray(T[]) method instead.
Or we could make it ever clearer:
public class Component {
private final Collection<Component> _subComponents;
public Component(Collection<Component> subComponents) {
Assert.isTrue(subComponents != null, "subComponents can't be null!");
List<Component> temp = new ArrayList<Component>(subComponents.size());
temp.addAll(subComponents);
_subComponents = Collections.unmodifiableCollection(temp);
}
public Collection<Component> getSubComponents() {
return _subComponents;
}
}
And of course my original bitch about custom type-safe collections:
Collection<Customer> customers = ...;
for (Iterator<Customer> i = customers.iterator(); i.hasNext(); ) {
doSomething(i.next());
}
Not bad. Feels like C++ again ;-). Not sure if that's such a good thing or not.
As Damian Guy points out, we could do even better using the new for construct provided by 1.5:
for (Customer c: customers) {
doSomething(c);
}
Now that definitely looks much nicer! Unfortunately I don't have a runtime that will support this - I can't seem to get J2SDK 1.5 to install on Gentoo :-(
Looking over all my newly re-written (that's re-factored for you buzzword bingo players) code, I have to say it reads pretty well. Time will tell if it really does add much more than using the old collection classes.
One thing I will note is that it's nearly impossible to copy and paste the examples for publishing as html. I have to change all the < and > to < and > respectively. Just putting that previous sentence in was a job and a half! hehehe
And lastly, I'm using IntelliJs support for generics using the 2.2 early access compiler from Sun. So no doubt things will change by the time they're released in J2SDK 1.5 proper.
Comments
Hey Simon,
You could even simplify the for loop to:
for (Component c: customers) {
doSomething(c);
}
Anyway, when you combine Generics,Foreach, and Boxing you end up with a heck of a lot less code in 1.5 than you would in 1.4. Bring it on!
Posted by: Damian Guy | December 30, 2003 04:40 PM
Codeguide has a generify refactoring which does a very good job of converting your existing project into one that replaces all the typeless collections and such into nicely
typed generic ones.
Posted by: Geert Bevin | January 19, 2004 12:11 AM
You use IntelliJ, right? It has an export feature for creating html from java source :) (File -> Export to HTML...)
- Jake
Posted by: Jacob Bay Hansen | January 19, 2004 05:47 AM
Simon,
There's an ebuild for the Java 1.5 EA now; while I haven't tried it, the release it's based on worked out-of-the-box for me.
Posted by: Robert Watkins | January 21, 2004 06:14 AM
Excellent!
Thanks for all the tips guys!
-- Cheers, Simon
Posted by: Simon Harris | January 21, 2004 07:26 AM