| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 part of quiver.core; | |
| 16 | |
| 17 /** | |
| 18 * A value that might be absent. | |
| 19 * | |
| 20 * Use Optional as an alternative to allowing fields, parameters or return | |
| 21 * values to be null. It signals that a value is not required and provides | |
| 22 * convenience methods for dealing with the absent case. | |
| 23 */ | |
| 24 class Optional<T> { | |
| 25 final T _value; | |
| 26 | |
| 27 /** | |
| 28 * Constructs an empty Optional. | |
| 29 */ | |
| 30 const Optional.absent() : _value = null; | |
| 31 | |
| 32 /** | |
| 33 * Constructs an Optional of the given [value]. | |
| 34 * | |
| 35 * Throws [ArgumentError] if [value] is null. | |
| 36 */ | |
| 37 Optional.of(T value) : this._value = value { | |
| 38 if (this._value == null) throw new ArgumentError('Must not be null.'); | |
| 39 } | |
| 40 | |
| 41 /** | |
| 42 * Constructs an Optional of the given [value]. | |
| 43 * | |
| 44 * If [value] is null, returns [absent()]. | |
| 45 */ | |
| 46 const Optional.fromNullable(T value) : this._value = value; | |
| 47 | |
| 48 /** | |
| 49 * Whether the Optional contains a value. | |
| 50 */ | |
| 51 bool get isPresent => _value != null; | |
| 52 | |
| 53 /** | |
| 54 * Gets the Optional value. | |
| 55 * | |
| 56 * Throws [StateError] if [value] is null. | |
| 57 */ | |
| 58 T get value { | |
| 59 if (this._value == null) { | |
| 60 throw new StateError('value called on absent Optional.'); | |
| 61 } | |
| 62 return _value; | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Executes a function if the Optional value is present. | |
| 67 */ | |
| 68 void ifPresent(void ifPresent(T value)) { | |
| 69 if (isPresent) { | |
| 70 ifPresent(_value); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 /** | |
| 75 * Execution a function if the Optional value is absent. | |
| 76 */ | |
| 77 void ifAbsent(void ifAbsent()) { | |
| 78 if (!isPresent) { | |
| 79 ifAbsent(); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 /** | |
| 84 * Gets the Optional value with a default. | |
| 85 * | |
| 86 * The default is returned if the Optional is [absent()]. | |
| 87 * | |
| 88 * Throws [ArgumentError] if [defaultValue] is null. | |
| 89 */ | |
| 90 T or(T defaultValue) { | |
| 91 if (defaultValue == null) { | |
| 92 throw new ArgumentError('defaultValue must not be null.'); | |
| 93 } | |
| 94 return _value == null ? defaultValue : _value; | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * Gets the Optional value, or [null] if there is none. | |
| 99 */ | |
| 100 T get orNull => _value; | |
| 101 | |
| 102 /** | |
| 103 * Transforms the Optional value. | |
| 104 * | |
| 105 * If the Optional is [absent()], returns [absent()] without applying the tran
sformer. | |
| 106 * | |
| 107 * The transformer must not return [null]. If it does, an [ArgumentError] is t
hrown. | |
| 108 */ | |
| 109 Optional transform(dynamic transformer(T value)) { | |
| 110 return _value == null | |
| 111 ? new Optional.absent() | |
| 112 : new Optional.of(transformer(_value)); | |
| 113 } | |
| 114 | |
| 115 /** | |
| 116 * Delegates to the underlying [value] hashCode. | |
| 117 */ | |
| 118 int get hashCode => _value.hashCode; | |
| 119 | |
| 120 /** | |
| 121 * Delegates to the underlying [value] operator==. | |
| 122 */ | |
| 123 bool operator ==(o) => o is Optional && o._value == _value; | |
| 124 | |
| 125 String toString() { | |
| 126 return _value == null | |
| 127 ? 'Optional { absent }' | |
| 128 : 'Optional { value: ${_value} }'; | |
| 129 } | |
| 130 } | |
| OLD | NEW |