karouani / javasimon

Automatically exported from code.google.com/p/javasimon
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Make Split implement AutoCloseable #127

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Sometimes one may want to measure execution time of a method. In such case he 
may surround it's body with tr ... finally:

public int myMethod() {
    Split s = SimonManager.getStopwatch("myMethod").start();
    try {
        // method body
        return 1;
    } finally {
        s.stop();
    }
}

I suggest to modify 'Split' class so it implements either AutoCloseable 
(available since java 1.7) or Closeable (available since java 1.5) interface. 
In close() method implementation it will simply call stop() method. This will 
do a great job to improve readability in above case because it can be wrapped 
in try with resources if java runtime environment version is >= 1.7. The code 
will look like:

public int myMethod() {
    try (Split s = SimonManager.getStopwatch("myMethod").start()) {
        // method body
        return 1;
    }
}

I think it's an easy task. I can do a pull request or so. What do you think?

Original issue reported on code.google.com by vitava...@gmail.com on 24 Jul 2014 at 10:44

GoogleCodeExporter commented 8 years ago
Split now implements AutoCloseable. Very good idea and simple to apply. It will 
be released with v4.0 sometime in this summer.

Original comment by virgo47 on 24 Jul 2014 at 9:43