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

Unified Diff: pkg/barback/lib/src/utils.dart

Issue 119673002: Add a ScheduledStream class and some stream matchers. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 11 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
« no previous file with comments | « pkg/barback/lib/src/stream_replayer.dart ('k') | pkg/scheduled_test/lib/scheduled_stream.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/barback/lib/src/utils.dart
diff --git a/pkg/barback/lib/src/utils.dart b/pkg/barback/lib/src/utils.dart
index a4a59507a00b2a5215218b6a661bda9cc5580d80..2afac7a2cb95b58f086db5d850376a5edf4dece4 100644
--- a/pkg/barback/lib/src/utils.dart
+++ b/pkg/barback/lib/src/utils.dart
@@ -9,67 +9,76 @@ import 'dart:typed_data';
import 'package:stack_trace/stack_trace.dart';
-/// A pair of values.
-class Pair<E, F> {
- E first;
- F last;
+/// A class that represents a value or an error.
+class Fallible<E> {
+ /// Whether [this] has a [value], as opposed to an [error].
+ final bool hasValue;
- Pair(this.first, this.last);
+ /// Whether [this] has an [error], as opposed to a [value].
+ bool get hasError => !hasValue;
- String toString() => '($first, $last)';
+ /// The value.
+ ///
+ /// This will be `null` if [this] has an [error].
+ final E _value;
- bool operator==(other) {
- if (other is! Pair) return false;
- return other.first == first && other.last == last;
+ /// The value.
+ ///
+ /// This will throw a [StateError] if [this] has an [error].
+ E get value {
+ if (hasValue) return _value;
+ throw new StateError("Fallible has no value.\n"
+ "$_error$_stackTraceSuffix");
}
- int get hashCode => first.hashCode ^ last.hashCode;
-}
-
-/// A class that represents one and only one of two types of values.
-class Either<E, F> {
- /// Whether this is a value of type `E`.
- final bool isFirst;
-
- /// Whether this is a value of type `F`.
- bool get isSecond => !isFirst;
-
- /// The value, either of type `E` or `F`.
- final _value;
+ /// The error.
+ ///
+ /// This will be `null` if [this] has a [value].
+ final _error;
- /// The value of type `E`.
+ /// The error.
///
- /// It's an error to access this is this is of type `F`.
- E get first {
- assert(isFirst);
- return _value;
+ /// This will throw a [StateError] if [this] has a [value].
+ get error {
+ if (hasError) return _error;
+ throw new StateError("Fallible has no error.");
}
- /// The value of type `F`.
+ /// The stack trace for [_error].
///
- /// It's an error to access this is this is of type `E`.
- F get second {
- assert(isSecond);
- return _value;
+ /// This will be `null` if [this] has a [value], or if no stack trace was
+ /// provided.
+ final StackTrace _stackTrace;
+
+ /// The stack trace for [error].
+ ///
+ /// This will throw a [StateError] if [this] has a [value].
+ StackTrace get stackTrace {
+ if (hasError) return _stackTrace;
+ throw new StateError("Fallible has no error.");
}
- /// Creates an [Either] with type `E`.
- Either.withFirst(this._value)
- : isFirst = true;
+ Fallible.withValue(this._value)
+ : hasValue = true;
- /// Creates an [Either] with type `F`.
- Either.withSecond(this._value)
- : isFirst = false;
+ Fallible.withError(this._error, [this._stackTrace])
+ : hasValue = false;
- /// Runs [whenFirst] or [whenSecond] depending on the type of [this].
- ///
- /// Returns the result of whichvever function was run.
- match(whenFirst(E value), whenSecond(F value)) {
- if (isFirst) return whenFirst(first);
- return whenSecond(second);
+ /// Returns a completed Future with the same value or error as [this].
+ Future<E> toFuture() {
+ if (hasValue) return new Future<E>.value(value);
+ return new Future<E>.error(error, stackTrace);
}
- String toString() => "$_value (${isFirst? 'first' : 'second'})";
+ String toString() {
+ if (hasValue) return "Fallible value: $value";
+ return "Fallible error: $error$_stackTraceSuffix";
+ }
+
+ String get _stackTraceSuffix {
+ if (stackTrace == null) return "";
+ return "\nStack trace:\n${new Chain.from(_stackTrace).terse}";
+ }
}
/// Converts a number in the range [0-255] to a two digit hex string.
« no previous file with comments | « pkg/barback/lib/src/stream_replayer.dart ('k') | pkg/scheduled_test/lib/scheduled_stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698