Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: dart/corelib/src/implementation/stopwatch_implementation.dart

Issue 9416065: Stopwatch.elapsed must always increase after start has been called. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Address comments and rebase Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | dart/frog/minfrog » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // or is running.
13 int _start; 14 int _start;
14 int _stop; 15 int _stop;
15 16
16 StopwatchImplementation() : _start = null, _stop = null {} 17 StopwatchImplementation() : _start = null, _stop = null {}
17 StopwatchImplementation.start() : _start = null, _stop = null { 18 StopwatchImplementation.start() : _start = null, _stop = null {
18 start(); 19 start();
19 } 20 }
20 21
21 void start() { 22 void start() {
22 if (_start === null) { 23 if (_start === null) {
23 // This stopwatch has never been started. 24 // This stopwatch has never been started.
24 _start = Clock.now(); 25 _start = Clock.now();
25 } else { 26 } else {
26 if (_stop === null) { 27 if (_stop === null) {
27 return; 28 return;
28 } 29 }
29 // Restarting this stopwatch. Prepend the elapsed time to the current 30 // Restarting this stopwatch. Prepend the elapsed time to the current
30 // start time. 31 // start time.
31 _start = Clock.now() - (_stop - _start); 32 _start = Clock.now() - (_stop - _start);
33 _stop = null;
32 } 34 }
33 } 35 }
34 36
35 void stop() { 37 void stop() {
36 if (_start === null) { 38 if (_start === null || _stop !== null) {
37 return; 39 return;
38 } 40 }
39 _stop = Clock.now(); 41 _stop = Clock.now();
40 } 42 }
41 43
42 void reset() { 44 void reset() {
43 if (_start === null) return; 45 if (_start === null) return;
44 // If [_start] is not null, then the stopwatch had already been started. It 46 // If [_start] is not null, then the stopwatch had already been started. It
45 // may running right now. 47 // may running right now.
46 _start = Clock.now(); 48 _start = Clock.now();
(...skipping 17 matching lines...) Expand all
64 66
65 int elapsedInMs() { 67 int elapsedInMs() {
66 return (elapsed() * 1000) ~/ frequency(); 68 return (elapsed() * 1000) ~/ frequency();
67 } 69 }
68 70
69 int frequency() { 71 int frequency() {
70 return Clock.frequency(); 72 return Clock.frequency();
71 } 73 }
72 74
73 } 75 }
OLDNEW
« no previous file with comments | « no previous file | dart/frog/minfrog » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698