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

Unified Diff: lib/core/duration.dart

Issue 10950006: Convert most interfaces in dart:core to abstract classes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: lib/core/duration.dart
diff --git a/lib/core/duration.dart b/lib/core/duration.dart
index c3bf66b37cffda10e391e1e54276fdcc12b273c8..f6a32fab8c27cba4a985e22659bd2d0a2636a201 100644
--- a/lib/core/duration.dart
+++ b/lib/core/duration.dart
@@ -7,7 +7,29 @@
/**
* A [Duration] represents a time span. A duration can be negative.
*/
-interface Duration extends Comparable default DurationImplementation {
+class Duration implements Comparable, Hashable {
+ static const int MILLISECONDS_PER_SECOND = 1000;
+ static const int SECONDS_PER_MINUTE = 60;
+ static const int MINUTES_PER_HOUR = 60;
+ static const int HOURS_PER_DAY = 24;
+
+ static const int MILLISECONDS_PER_MINUTE =
+ MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE;
+ static const int MILLISECONDS_PER_HOUR =
+ MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR;
+ static const int MILLISECONDS_PER_DAY =
+ MILLISECONDS_PER_HOUR * HOURS_PER_DAY;
+
+ static const int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;
+ static const int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;
+
+ static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY;
+
+ /**
+ * This [Duration] in milliseconds.
+ */
+ final int inMilliseconds;
+
/**
* The duration is the sum of all individual parts. This means that individual
* parts don't need to be less than the next-bigger unit. For example [hours]
@@ -16,51 +38,83 @@ interface Duration extends Comparable default DurationImplementation {
* All individual parts are allowed to be negative.
* All arguments are by default 0.
*/
- const Duration({int days, int hours, int minutes, int seconds,
- int milliseconds});
+ const Duration({int days: 0,
+ int hours: 0,
+ int minutes: 0,
+ int seconds: 0,
+ int milliseconds: 0})
+ : inMilliseconds = days * Duration.MILLISECONDS_PER_DAY +
+ hours * Duration.MILLISECONDS_PER_HOUR +
+ minutes * Duration.MILLISECONDS_PER_MINUTE +
+ seconds * Duration.MILLISECONDS_PER_SECOND +
+ milliseconds;
/**
- * Returns this [Duration] in days. Incomplete days are discarded.
+ * This [Duration] in days. Incomplete days are discarded
*/
- int get inDays;
+ int get inDays {
+ return inMilliseconds ~/ Duration.MILLISECONDS_PER_DAY;
+ }
/**
- * Returns this [Duration] in hours. Incomplete hours are discarded.
+ * This [Duration] in hours. Incomplete hours are discarded.
* The returned value can be greater than 23.
*/
- int get inHours;
+ int get inHours {
+ return inMilliseconds ~/ Duration.MILLISECONDS_PER_HOUR;
+ }
/**
- * Returns this [Duration] in minutes. Incomplete minutes are discarded.
+ * This [Duration] in minutes. Incomplete minutes are discarded.
* The returned value can be greater than 59.
*/
- int get inMinutes;
+ int get inMinutes {
+ return inMilliseconds ~/ Duration.MILLISECONDS_PER_MINUTE;
+ }
/**
- * Returns this [Duration] in seconds. Incomplete seconds are discarded.
+ * This [Duration] in seconds. Incomplete seconds are discarded.
* The returned value can be greater than 59.
*/
- int get inSeconds;
+ int get inSeconds {
+ return inMilliseconds ~/ Duration.MILLISECONDS_PER_SECOND;
+ }
- /**
- * Returns this [Duration] in milliseconds.
- */
- int get inMilliseconds;
+ bool operator ==(other) {
+ if (other is !Duration) return false;
+ return inMilliseconds == other.inMilliseconds;
+ }
- static const int MILLISECONDS_PER_SECOND = 1000;
- static const int SECONDS_PER_MINUTE = 60;
- static const int MINUTES_PER_HOUR = 60;
- static const int HOURS_PER_DAY = 24;
+ int hashCode() {
+ return inMilliseconds.hashCode();
+ }
- static const int MILLISECONDS_PER_MINUTE =
- MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE;
- static const int MILLISECONDS_PER_HOUR =
- MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR;
- static const int MILLISECONDS_PER_DAY =
- MILLISECONDS_PER_HOUR * HOURS_PER_DAY;
+ int compareTo(Duration other) {
+ return inMilliseconds.compareTo(other.inMilliseconds);
+ }
- static const int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;
- static const int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;
+ String toString() {
+ String threeDigits(int n) {
+ if (n >= 100) return "$n";
+ if (n > 10) return "0$n";
+ return "00$n";
+ }
+ String twoDigits(int n) {
+ if (n >= 10) return "$n";
+ return "0$n";
+ }
- static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY;
+ if (inMilliseconds < 0) {
+ Duration duration =
+ new Duration(milliseconds: -inMilliseconds);
+ return "-$duration";
+ }
+ String twoDigitMinutes =
+ twoDigits(inMinutes.remainder(Duration.MINUTES_PER_HOUR));
+ String twoDigitSeconds =
+ twoDigits(inSeconds.remainder(Duration.SECONDS_PER_MINUTE));
+ String threeDigitMs =
+ threeDigits(inMilliseconds.remainder(Duration.MILLISECONDS_PER_SECOND));
+ return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$threeDigitMs";
+ }
}

Powered by Google App Engine
This is Rietveld 408576698