Stopwatches and Cooldowns

####Preface
A couple days ago I wanted to do a simple time comparison for a process, so I wrote a Stopwatch class. It was a stupid Stopwatch class so I rewrote it three times and stayed up until midnight. Now its a nice Stopwatch class, and I figured I’d share it.

####What is it?
This resource is a single class that you can use in your project if you want. The class has the ability to count time elapsed, be dynamically paused and stopped, and potentially be used for cooldowns. Moreover, it’s not as performance heavy as a repeating task (which is probably one of the worst ways to go about cooldowns, and yes, I’ve done it that way before). It can also give you human-readable representations of the time elapsed, in the form “[hours] Hours, [minutes] Minutes, [seconds] Seconds, and [millis] Milliseconds,” times that are 0 are not included, and milliseconds can be optionally rounded.

###Cooldowns
You can use the class for cooldowns by using the until or humanReadableUntil methods. These methods return the amount of time until the time elapsed reaches the passed in time.

###Benefits

  • Thread-safe
  • Reasonably efficient
  • Makes pretty human readable strings
  • Fully documented

###Drawbacks

  • Doesn’t display days, or any greater period of time
  • Written by a mad scientist

####Where is it?
Righter here:

So overcomplicated

Yes, yes it is.

With unquestionable qualifications like that, this product is entitled to the Yggdrasyl Labs Seal of Approval.
May the FLARD stay out of your socks.

3 Likes

Nice work, for stuff like this, system.nanoTime is usually more appropriate I think…

Also you should look into Aikar’s timing stuff for measuring sponge plugin performance (if that is what the main use of this stopwatch was) as it allows server users to publish the timings to get help on forums etc.

1 Like

Why not use the one built into Guava, since the Sponge API contains Guava.

1 Like

Because I had been timing something not in sponge… And didn’t realize guava had one.

1 Like