| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * A simple implementation of the [StopWatch] interface. | 6 * A simple implementation of the [Stopwatch] interface. |
| 7 */ | 7 */ |
| 8 class StopWatchImplementation implements StopWatch { | 8 class StopwatchImplementation implements Stopwatch { |
| 9 // The _start and _stop fields capture the time when [start] and [stop] | 9 // The _start and _stop fields capture the time when [start] and [stop] |
| 10 // are called respectively. | 10 // are called respectively. |
| 11 // If _start is null, then the [StopWatch] has not been started yet. | 11 // If _start is null, then the [Stopwatch] has not been started yet. |
| 12 // If _stop is null, then the [StopWatch] has not been stopped yet. | 12 // If _stop is null, then the [Stopwatch] has not been stopped yet. |
| 13 int _start; | 13 int _start; |
| 14 int _stop; | 14 int _stop; |
| 15 | 15 |
| 16 StopWatchImplementation() : _start = null, _stop = null {} | 16 StopwatchImplementation() : _start = null, _stop = null {} |
| 17 StopwatchImplementation.start() : _start = null, _stop = null { |
| 18 start(); |
| 19 } |
| 17 | 20 |
| 18 void start() { | 21 void start() { |
| 19 if (_start === null) { | 22 if (_start === null) { |
| 20 // This stopwatch has never been started. | 23 // This stopwatch has never been started. |
| 21 _start = Clock.now(); | 24 _start = Clock.now(); |
| 22 } else { | 25 } else { |
| 23 if (_stop === null) { | 26 if (_stop === null) { |
| 24 return; | 27 return; |
| 25 } | 28 } |
| 26 // Restarting this stopwatch. Prepend the elapsed time to the current | 29 // Restarting this stopwatch. Prepend the elapsed time to the current |
| 27 // start time. | 30 // start time. |
| 28 _start = Clock.now() - (_stop - _start); | 31 _start = Clock.now() - (_stop - _start); |
| 29 } | 32 } |
| 30 } | 33 } |
| 31 | 34 |
| 32 void stop() { | 35 void stop() { |
| 33 if (_start === null) { | 36 if (_start === null) { |
| 34 return; | 37 return; |
| 35 } | 38 } |
| 36 _stop = Clock.now(); | 39 _stop = Clock.now(); |
| 37 } | 40 } |
| 38 | 41 |
| 42 void reset() { |
| 43 if (_start === null) return; |
| 44 // If [_start] is not null, then the stopwatch had already been started. It |
| 45 // may running right now. |
| 46 _start = Clock.now(); |
| 47 if (_stop !== null) { |
| 48 // The watch is not running. So simply set the [_stop] to [_start] thus |
| 49 // having an elapsed time of 0. |
| 50 _stop = _start; |
| 51 } |
| 52 } |
| 53 |
| 39 int elapsed() { | 54 int elapsed() { |
| 40 if (_start === null) { | 55 if (_start === null) { |
| 41 return 0; | 56 return 0; |
| 42 } | 57 } |
| 43 return (_stop === null) ? (Clock.now() - _start) : (_stop - _start); | 58 return (_stop === null) ? (Clock.now() - _start) : (_stop - _start); |
| 44 } | 59 } |
| 45 | 60 |
| 46 int elapsedInUs() { | 61 int elapsedInUs() { |
| 47 return (elapsed() * 1000000) ~/ frequency(); | 62 return (elapsed() * 1000000) ~/ frequency(); |
| 48 } | 63 } |
| 49 | 64 |
| 50 int elapsedInMs() { | 65 int elapsedInMs() { |
| 51 return (elapsed() * 1000) ~/ frequency(); | 66 return (elapsed() * 1000) ~/ frequency(); |
| 52 } | 67 } |
| 53 | 68 |
| 54 int frequency() { | 69 int frequency() { |
| 55 return Clock.frequency(); | 70 return Clock.frequency(); |
| 56 } | 71 } |
| 57 | 72 |
| 58 } | 73 } |
| OLD | NEW |