OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library analyzer.src.task.inputs; | 5 library analyzer.src.task.inputs; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analyzer/task/model.dart'; | 9 import 'package:analyzer/task/model.dart'; |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 */ | 34 */ |
35 abstract class ListTaskInputMixin<E> implements ListTaskInput<E> { | 35 abstract class ListTaskInputMixin<E> implements ListTaskInput<E> { |
36 ListTaskInput /*<V>*/ toList(UnaryFunction<E, dynamic /*<V>*/ > mapper) { | 36 ListTaskInput /*<V>*/ toList(UnaryFunction<E, dynamic /*<V>*/ > mapper) { |
37 return new ListToListTaskInput<E, dynamic /*V*/ >(this, mapper); | 37 return new ListToListTaskInput<E, dynamic /*V*/ >(this, mapper); |
38 } | 38 } |
39 | 39 |
40 ListTaskInput /*<V>*/ toListOf(ResultDescriptor /*<V>*/ valueResult) { | 40 ListTaskInput /*<V>*/ toListOf(ResultDescriptor /*<V>*/ valueResult) { |
41 return (this as ListTaskInputImpl<AnalysisTarget>).toList(valueResult.of); | 41 return (this as ListTaskInputImpl<AnalysisTarget>).toList(valueResult.of); |
42 } | 42 } |
43 | 43 |
44 TaskInput<Map<E, dynamic /*V*/ >> toMap( | 44 MapTaskInput<E, dynamic /*V*/ > toMap( |
45 UnaryFunction<E, dynamic /*<V>*/ > mapper) { | 45 UnaryFunction<E, dynamic /*<V>*/ > mapper) { |
46 return new ListToMapTaskInput<E, dynamic /*V*/ >(this, mapper); | 46 return new ListToMapTaskInput<E, dynamic /*V*/ >(this, mapper); |
47 } | 47 } |
48 | 48 |
49 TaskInput<Map<AnalysisTarget, dynamic /*V*/ >> toMapOf( | 49 MapTaskInput<AnalysisTarget, dynamic /*V*/ > toMapOf( |
50 ResultDescriptor /*<V>*/ valueResult) { | 50 ResultDescriptor /*<V>*/ valueResult) { |
51 return (this as ListTaskInputImpl<AnalysisTarget>).toMap(valueResult.of); | 51 return (this as ListTaskInputImpl<AnalysisTarget>).toMap(valueResult.of); |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 /** | 55 /** |
56 * An input to an [AnalysisTask] that is computed by the following steps. First | 56 * An input to an [AnalysisTask] that is computed by the following steps. First |
57 * another (base) task input is used to compute a [List]-valued result. An input | 57 * another (base) task input is used to compute a [List]-valued result. An input |
58 * generator function is then used to map each element of that list to a task | 58 * generator function is then used to map each element of that list to a task |
59 * input. Finally, each of the task inputs are used to access analysis results, | 59 * input. Finally, each of the task inputs are used to access analysis results, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 | 106 |
107 /** | 107 /** |
108 * An input to an [AnalysisTask] that is computed by the following steps. First | 108 * An input to an [AnalysisTask] that is computed by the following steps. First |
109 * another (base) task input is used to compute a [List]-valued result. An input | 109 * another (base) task input is used to compute a [List]-valued result. An input |
110 * generator function is then used to map each element of that list to a task | 110 * generator function is then used to map each element of that list to a task |
111 * input. Finally, each of the task inputs are used to access analysis results, | 111 * input. Finally, each of the task inputs are used to access analysis results, |
112 * and the map of the base elements to the analysis results is used as the | 112 * and the map of the base elements to the analysis results is used as the |
113 * input to the task. | 113 * input to the task. |
114 */ | 114 */ |
115 class ListToMapTaskInput<B, E> | 115 class ListToMapTaskInput<B, E> |
116 extends _ListToCollectionTaskInput<B, E, Map<B, E>> { | 116 extends _ListToCollectionTaskInput<B, E, Map<B, E>> |
| 117 with MapTaskInputMixin<B, E> { |
117 /** | 118 /** |
118 * Initialize a result accessor to use the given [baseAccessor] to access a | 119 * Initialize a result accessor to use the given [baseAccessor] to access a |
119 * list of values that can be passed to the given [generateTaskInputs] to | 120 * list of values that can be passed to the given [generateTaskInputs] to |
120 * generate a list of task inputs that can be used to access the elements of | 121 * generate a list of task inputs that can be used to access the elements of |
121 * the input being accessed. | 122 * the input being accessed. |
122 */ | 123 */ |
123 ListToMapTaskInput(TaskInput<List<B>> baseAccessor, | 124 ListToMapTaskInput(TaskInput<List<B>> baseAccessor, |
124 GenerateTaskInputs<B, E> generateTaskInputs) | 125 GenerateTaskInputs<B, E> generateTaskInputs) |
125 : super(baseAccessor, generateTaskInputs); | 126 : super(baseAccessor, generateTaskInputs); |
126 | 127 |
(...skipping 23 matching lines...) Expand all Loading... |
150 _resultValue[baseElement] = resultElement; | 151 _resultValue[baseElement] = resultElement; |
151 } | 152 } |
152 | 153 |
153 @override | 154 @override |
154 void _initResultValue() { | 155 void _initResultValue() { |
155 _resultValue = new HashMap<B, E>(); | 156 _resultValue = new HashMap<B, E>(); |
156 } | 157 } |
157 } | 158 } |
158 | 159 |
159 /** | 160 /** |
| 161 * A mixin-ready implementation of [MapTaskInput]. |
| 162 */ |
| 163 abstract class MapTaskInputMixin<K, V> implements MapTaskInput<K, V> { |
| 164 TaskInput<List /*<E>*/ > toFlattenList( |
| 165 BinaryFunction<K, dynamic /*element of V*/, dynamic /*<E>*/ > mapper) { |
| 166 return new MapToFlattenListTaskInput<K, dynamic /*element of V*/, dynamic /*
E*/ >( |
| 167 this as MapTaskInput<K, List /*<element of V>*/ >, mapper); |
| 168 } |
| 169 } |
| 170 |
| 171 /** |
| 172 * A [TaskInput] that is computed by the following steps. |
| 173 * |
| 174 * First the [base] task input is used to compute a [Map]-valued result. |
| 175 * The values of the [Map] must be [List]s. |
| 176 * |
| 177 * The given [mapper] is used to transform each key / value pair of the [Map] |
| 178 * into task inputs. |
| 179 * |
| 180 * Finally, each of the task inputs are used to access analysis results, |
| 181 * and the list of the results is used as the input. |
| 182 */ |
| 183 class MapToFlattenListTaskInput<K, V, E> implements TaskInput<List<E>> { |
| 184 final MapTaskInput<K, List<V>> base; |
| 185 final BinaryFunction<K, V, E> mapper; |
| 186 |
| 187 MapToFlattenListTaskInput(this.base, this.mapper); |
| 188 |
| 189 @override |
| 190 TaskInputBuilder<List> createBuilder() { |
| 191 return new MapToFlattenListTaskInputBuilder<K, V, E>(base, mapper); |
| 192 } |
| 193 } |
| 194 |
| 195 /** |
| 196 * The [TaskInputBuilder] for [MapToFlattenListTaskInput]. |
| 197 */ |
| 198 class MapToFlattenListTaskInputBuilder<K, V, E> |
| 199 implements TaskInputBuilder<List<E>> { |
| 200 final MapTaskInput<K, List<V>> base; |
| 201 final BinaryFunction<K, V, E> mapper; |
| 202 |
| 203 TaskInputBuilder currentBuilder; |
| 204 Map<K, List<V>> baseMap; |
| 205 Iterator<K> keyIterator; |
| 206 Iterator<V> valueIterator; |
| 207 |
| 208 final List<E> inputValue = <E>[]; |
| 209 |
| 210 MapToFlattenListTaskInputBuilder(this.base, this.mapper) { |
| 211 currentBuilder = base.createBuilder(); |
| 212 } |
| 213 |
| 214 @override |
| 215 ResultDescriptor get currentResult { |
| 216 if (currentBuilder == null) { |
| 217 return null; |
| 218 } |
| 219 return currentBuilder.currentResult; |
| 220 } |
| 221 |
| 222 AnalysisTarget get currentTarget { |
| 223 if (currentBuilder == null) { |
| 224 return null; |
| 225 } |
| 226 return currentBuilder.currentTarget; |
| 227 } |
| 228 |
| 229 @override |
| 230 void set currentValue(Object value) { |
| 231 if (currentBuilder == null) { |
| 232 throw new StateError( |
| 233 'Cannot set the result value when there is no current result'); |
| 234 } |
| 235 currentBuilder.currentValue = value; |
| 236 } |
| 237 |
| 238 @override |
| 239 bool moveNext() { |
| 240 // Prepare base Map. |
| 241 if (baseMap == null) { |
| 242 if (currentBuilder.moveNext()) { |
| 243 return true; |
| 244 } |
| 245 baseMap = currentBuilder.inputValue; |
| 246 keyIterator = baseMap.keys.iterator; |
| 247 // Done with this builder. |
| 248 currentBuilder = null; |
| 249 } |
| 250 // Prepare the next result value. |
| 251 if (currentBuilder != null) { |
| 252 if (currentBuilder.moveNext()) { |
| 253 return true; |
| 254 } |
| 255 // Add the result value for the current Map key/value. |
| 256 E resultValue = currentBuilder.inputValue; |
| 257 inputValue.add(resultValue); |
| 258 // Done with this builder. |
| 259 currentBuilder = null; |
| 260 } |
| 261 // Move to the next Map value. |
| 262 if (valueIterator != null && valueIterator.moveNext()) { |
| 263 K key = keyIterator.current; |
| 264 V value = valueIterator.current; |
| 265 currentBuilder = mapper(key, value).createBuilder(); |
| 266 return moveNext(); |
| 267 } |
| 268 // Move to the next Map key. |
| 269 if (keyIterator.moveNext()) { |
| 270 K key = keyIterator.current; |
| 271 valueIterator = baseMap[key].iterator; |
| 272 return moveNext(); |
| 273 } |
| 274 // No more values/keys to transform. |
| 275 return false; |
| 276 } |
| 277 } |
| 278 |
| 279 /** |
160 * An input to an [AnalysisTask] that is computed by accessing a single result | 280 * An input to an [AnalysisTask] that is computed by accessing a single result |
161 * defined on a single target. | 281 * defined on a single target. |
162 */ | 282 */ |
163 class SimpleTaskInput<V> implements TaskInput<V> { | 283 class SimpleTaskInput<V> implements TaskInput<V> { |
164 /** | 284 /** |
165 * The target on which the result is defined. | 285 * The target on which the result is defined. |
166 */ | 286 */ |
167 final AnalysisTarget target; | 287 final AnalysisTarget target; |
168 | 288 |
169 /** | 289 /** |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 return false; | 636 return false; |
517 } | 637 } |
518 _baseListElement = _baseList[_baseListIndex]; | 638 _baseListElement = _baseList[_baseListIndex]; |
519 currentBuilder = input.generateTaskInputs(_baseListElement).createBuilder(); | 639 currentBuilder = input.generateTaskInputs(_baseListElement).createBuilder(); |
520 return currentBuilder.moveNext(); | 640 return currentBuilder.moveNext(); |
521 } | 641 } |
522 | 642 |
523 void _addResultElement(B baseElement, E resultElement); | 643 void _addResultElement(B baseElement, E resultElement); |
524 void _initResultValue(); | 644 void _initResultValue(); |
525 } | 645 } |
OLD | NEW |