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

Unified Diff: packages/quiver/lib/src/core/optional.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 5 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 | « packages/quiver/lib/src/core/hash.dart ('k') | packages/quiver/lib/src/iterables/concat.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packages/quiver/lib/src/core/optional.dart
diff --git a/packages/quiver/lib/src/core/optional.dart b/packages/quiver/lib/src/core/optional.dart
index 6802229282e1520bb4b59f9f118ea9525972e941..d7496cce446fd66f0e4913207072c04c3756591c 100644
--- a/packages/quiver/lib/src/core/optional.dart
+++ b/packages/quiver/lib/src/core/optional.dart
@@ -14,47 +14,35 @@
part of quiver.core;
-/**
- * A value that might be absent.
- *
- * Use Optional as an alternative to allowing fields, parameters or return
- * values to be null. It signals that a value is not required and provides
- * convenience methods for dealing with the absent case.
- */
-class Optional<T> {
+/// A value that might be absent.
+///
+/// Use Optional as an alternative to allowing fields, parameters or return
+/// values to be null. It signals that a value is not required and provides
+/// convenience methods for dealing with the absent case.
+class Optional<T> extends IterableBase<T> {
final T _value;
- /**
- * Constructs an empty Optional.
- */
+ /// Constructs an empty Optional.
const Optional.absent() : _value = null;
- /**
- * Constructs an Optional of the given [value].
- *
- * Throws [ArgumentError] if [value] is null.
- */
+ /// Constructs an Optional of the given [value].
+ ///
+ /// Throws [ArgumentError] if [value] is null.
Optional.of(T value) : this._value = value {
if (this._value == null) throw new ArgumentError('Must not be null.');
}
- /**
- * Constructs an Optional of the given [value].
- *
- * If [value] is null, returns [absent()].
- */
+ /// Constructs an Optional of the given [value].
+ ///
+ /// If [value] is null, returns [absent()].
const Optional.fromNullable(T value) : this._value = value;
- /**
- * Whether the Optional contains a value.
- */
+ /// Whether the Optional contains a value.
bool get isPresent => _value != null;
- /**
- * Gets the Optional value.
- *
- * Throws [StateError] if [value] is null.
- */
+ /// Gets the Optional value.
+ ///
+ /// Throws [StateError] if [value] is null.
T get value {
if (this._value == null) {
throw new StateError('value called on absent Optional.');
@@ -62,31 +50,25 @@ class Optional<T> {
return _value;
}
- /**
- * Executes a function if the Optional value is present.
- */
+ /// Executes a function if the Optional value is present.
void ifPresent(void ifPresent(T value)) {
if (isPresent) {
ifPresent(_value);
}
}
- /**
- * Execution a function if the Optional value is absent.
- */
+ /// Execution a function if the Optional value is absent.
void ifAbsent(void ifAbsent()) {
if (!isPresent) {
ifAbsent();
}
}
- /**
- * Gets the Optional value with a default.
- *
- * The default is returned if the Optional is [absent()].
- *
- * Throws [ArgumentError] if [defaultValue] is null.
- */
+ /// Gets the Optional value with a default.
+ ///
+ /// The default is returned if the Optional is [absent()].
+ ///
+ /// Throws [ArgumentError] if [defaultValue] is null.
T or(T defaultValue) {
if (defaultValue == null) {
throw new ArgumentError('defaultValue must not be null.');
@@ -94,32 +76,28 @@ class Optional<T> {
return _value == null ? defaultValue : _value;
}
- /**
- * Gets the Optional value, or [null] if there is none.
- */
+ /// Gets the Optional value, or [null] if there is none.
T get orNull => _value;
- /**
- * Transforms the Optional value.
- *
- * If the Optional is [absent()], returns [absent()] without applying the transformer.
- *
- * The transformer must not return [null]. If it does, an [ArgumentError] is thrown.
- */
- Optional transform(dynamic transformer(T value)) {
+ /// Transforms the Optional value.
+ ///
+ /// If the Optional is [absent()], returns [absent()] without applying the transformer.
+ ///
+ /// The transformer must not return [null]. If it does, an [ArgumentError] is thrown.
+ Optional<S> transform<S>(S transformer(T value)) {
return _value == null
? new Optional.absent()
: new Optional.of(transformer(_value));
}
- /**
- * Delegates to the underlying [value] hashCode.
- */
+ @override
+ Iterator<T> get iterator =>
+ isPresent ? <T>[_value].iterator : new Iterable<T>.empty().iterator;
+
+ /// Delegates to the underlying [value] hashCode.
int get hashCode => _value.hashCode;
- /**
- * Delegates to the underlying [value] operator==.
- */
+ /// Delegates to the underlying [value] operator==.
bool operator ==(o) => o is Optional && o._value == _value;
String toString() {
« no previous file with comments | « packages/quiver/lib/src/core/hash.dart ('k') | packages/quiver/lib/src/iterables/concat.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698