« Immutable Collections | Main | My foray into C# »

Generics

Listen to this articleListen 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 &lt; and &gt; 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!

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.

You use IntelliJ, right? It has an export feature for creating html from java source :) (File -> Export to HTML...)

- Jake

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.

Excellent!

Thanks for all the tips guys!

-- Cheers, Simon

Post a comment