Redo "Fast path constructor for Duration"

TBR=lrn@google.com
BUG=

Review URL: https://codereview.chromium.org//715233006

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@41699 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
sra@google.com
2014-11-12 21:42:14 +00:00
parent 7f8da31b88
commit eed03994e4
+18 -15
View File
@@ -92,19 +92,24 @@ class Duration implements Comparable<Duration> {
int seconds: 0,
int milliseconds: 0,
int microseconds: 0})
: _duration = days * MICROSECONDS_PER_DAY +
hours * MICROSECONDS_PER_HOUR +
minutes * MICROSECONDS_PER_MINUTE +
seconds * MICROSECONDS_PER_SECOND +
milliseconds * MICROSECONDS_PER_MILLISECOND +
microseconds;
: this._microseconds(
days * MICROSECONDS_PER_DAY +
hours * MICROSECONDS_PER_HOUR +
minutes * MICROSECONDS_PER_MINUTE +
seconds * MICROSECONDS_PER_SECOND +
milliseconds * MICROSECONDS_PER_MILLISECOND +
microseconds);
// Fast path internal direct constructor to avoids the optional arguments and
// [_microseconds] recomputation.
const Duration._microseconds(this._duration);
/**
* Adds this Duration and [other] and
* returns the sum as a new Duration object.
*/
Duration operator +(Duration other) {
return new Duration(microseconds: _duration + other._duration);
return new Duration._microseconds(_duration + other._duration);
}
/**
@@ -112,7 +117,7 @@ class Duration implements Comparable<Duration> {
* returns the difference as a new Duration object.
*/
Duration operator -(Duration other) {
return new Duration(microseconds: _duration - other._duration);
return new Duration._microseconds(_duration - other._duration);
}
/**
@@ -123,7 +128,7 @@ class Duration implements Comparable<Duration> {
* 53 bits, precision is lost because of double-precision arithmetic.
*/
Duration operator *(num factor) {
return new Duration(microseconds: (_duration * factor).round());
return new Duration._microseconds((_duration * factor).round());
}
/**
@@ -136,7 +141,7 @@ class Duration implements Comparable<Duration> {
// By doing the check here instead of relying on "~/" below we get the
// exception even with dart2js.
if (quotient == 0) throw new IntegerDivisionByZeroException();
return new Duration(microseconds: _duration ~/ quotient);
return new Duration._microseconds(_duration ~/ quotient);
}
/**
@@ -248,9 +253,7 @@ class Duration implements Comparable<Duration> {
}
if (inMicroseconds < 0) {
Duration duration =
new Duration(microseconds: -inMicroseconds);
return "-$duration";
return "-${-this}";
}
String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR));
String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE));
@@ -274,7 +277,7 @@ class Duration implements Comparable<Duration> {
* The returned `Duration` has the same length as this one, but is always
* positive.
*/
Duration abs() => new Duration(microseconds: _duration.abs());
Duration abs() => new Duration._microseconds(_duration.abs());
/**
* Returns a new `Duration` representing this `Duration` negated.
@@ -282,5 +285,5 @@ class Duration implements Comparable<Duration> {
* The returned `Duration` has the same length as this one, but will have the
* opposite sign of this one.
*/
Duration operator -() => new Duration(microseconds: -_duration);
Duration operator -() => new Duration._microseconds(-_duration);
}