StopwatchImplementation Class
A simple implementation of the Stopwatch
interface.
Implements
Constructors
Code new StopwatchImplementation.start() #
StopwatchImplementation.start() : _start = null, _stop = null { start(); }
Code new StopwatchImplementation() #
StopwatchImplementation() : _start = null, _stop = null {}
Methods
Code int elapsed() #
int elapsed() { if (_start === null) { return 0; } return (_stop === null) ? (Clock.now() - _start) : (_stop - _start); }
Code void reset() #
void reset() { if (_start === null) return; // If [_start] is not null, then the stopwatch had already been started. It // may running right now. _start = Clock.now(); if (_stop !== null) { // The watch is not running. So simply set the [_stop] to [_start] thus // having an elapsed time of 0. _stop = _start; } }
Code void start() #
void start() { if (_start === null) { // This stopwatch has never been started. _start = Clock.now(); } else { if (_stop === null) { return; } // Restarting this stopwatch. Prepend the elapsed time to the current // start time. _start = Clock.now() - (_stop - _start); _stop = null; } }
Code void stop() #
void stop() { if (_start === null || _stop !== null) { return; } _stop = Clock.now(); }