Pruning back thickets of waffle with Java Seven

Java SE 7 was released back in the summer of 2011. I still haven’t written any code for it, perhaps you haven’t either? I could have gotten out of the habit of seriously considering updates to the language after the interminable wait since the release of Six, or was it that broken optimization scaremongering at the time of Seven’s release, or is this simply the behaviour of a forsaken MacOSX user?

However, since attending a presentation at QConSF 2011 given by Alex Buckley, the specification lead for the Java language, I find myself seeing a number of Java coding patterns repeatedly being used that would have been streamlined had I been writing for Java Seven.

Diamond Operator – Removing duplicate generics information during construction.

Old:

List<String> ls = new ArrayList<String>();

Old w/Google Guava:

List<String> ls = Lists.newArrayList();

Shiny New:

List<String> ls = new ArrayList<>();

NIO2 – Richer API for interacting with IO/Files that includes some useful utility methods.

Old:

out = new FileOutputStream(tmpFile);
int read = 0;
byte[] buf = new byte[BUF_SIZE];
while ((read = in.read(buf)) != -1) {
    out.write(buf, 0, read);
}
out.close();

Old w/Apache Commons:

import org.apache.commons.io.IOUtils;
out = new FileOutputStream(tmpFile);
IOUtils.copy(in, out);

Shiny New:

import java.nio.file.*;
Files.copy(in, tmpFile, REPLACE_EXISTING);

Exception Handling – Remove duplication required to handle multiple exception types and provide more precise propagation of exceptions.

Old:

try {
    Object result = future.get(5, SECONDS);
} catch (InterruptedException ex) {
    cleanup();
    throw ex;
} catch (ExecutionException ex) {
    cleanup();
    throw ex;
} catch (TimeoutException ex) {
    cleanup();
    throw ex;
}

Shiny New:

try {
    Object result = future.get(5, SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
    cleanup();
    throw ex;
}

Reliable Resource Cleanup – Reduce boilerplate to support resources in the face of failures, and increase the chance of it behaving correctly.

Old:

InputStream in = url.openStream();
try {
    ...
}
catch (...) { ... }
finally {
    if (in != null) {
        in.close();
    }
}

Old w/Apache Commons:

InputStream in = url.openStream();
try {
    ...
}
catch (...) { ... }
finally {
    IOUtils.closeQuietly(in);
}

Shiny New:

try (InputStream in = url.openStream()) {
    ...
}
catch (...) { ... }

Ready to break out the shears?

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

3 thoughts on “Pruning back thickets of waffle with Java Seven

  1. Ken Stevens

    Excellent read. Thanks David.

    The thing I want to see the most is a new module visibility level. Kind of like what OSGi is about, but integrated into the java language.

    hibernate.jar consists of many packages, and those packages need to talk with one another through “public” APIs. But as a consumer of hibernate.jar, my visibility needs are different from hibernate’s internal viability needs. There needs to be a way in the java language to express visibility at the “module” level, so when I hit ctrl-shit-T inside eclipse, I only see those hibernate classes that I’m meant to use.

Leave a Reply