| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 pub.source_registry; | 5 library pub.source_registry; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'source.dart'; | 9 import 'source.dart'; |
| 10 import 'source/unknown.dart'; |
| 10 | 11 |
| 11 /// A class that keeps track of [Source]s used for getting packages. | 12 /// A class that keeps track of [Source]s used for getting packages. |
| 12 class SourceRegistry extends IterableBase<Source> { | 13 class SourceRegistry extends IterableBase<Source> { |
| 13 final Map<String, Source> _map; | 14 final _sources = new Map<String, Source>(); |
| 14 Source _default; | 15 Source _default; |
| 15 | 16 |
| 16 /// Creates a new registry with no packages registered. | |
| 17 SourceRegistry() : _map = <String, Source>{}; | |
| 18 | |
| 19 /// Returns the default source, which is used when no source is specified. | 17 /// Returns the default source, which is used when no source is specified. |
| 20 Source get defaultSource => _default; | 18 Source get defaultSource => _default; |
| 21 | 19 |
| 22 /// Iterates over the registered sources in name order. | 20 /// Iterates over the registered sources in name order. |
| 23 Iterator<Source> get iterator { | 21 Iterator<Source> get iterator { |
| 24 var sources = _map.values.toList(); | 22 var sources = _sources.values.toList(); |
| 25 sources.sort((a, b) => a.name.compareTo(b.name)); | 23 sources.sort((a, b) => a.name.compareTo(b.name)); |
| 26 return sources.iterator; | 24 return sources.iterator; |
| 27 } | 25 } |
| 28 | 26 |
| 29 /// Sets the default source. This takes a string, which must be the name of a | 27 /// Sets the default source. This takes a string, which must be the name of a |
| 30 /// registered source. | 28 /// registered source. |
| 31 void setDefault(String name) { | 29 void setDefault(String name) { |
| 32 if (!_map.containsKey(name)) { | 30 if (!_sources.containsKey(name)) { |
| 33 throw new StateError('Default source $name is not in the registry'); | 31 throw new StateError('Default source $name is not in the registry'); |
| 34 } | 32 } |
| 35 | 33 |
| 36 _default = _map[name]; | 34 _default = _sources[name]; |
| 37 } | 35 } |
| 38 | 36 |
| 39 /// Registers a new source. This source may not have the same name as a source | 37 /// Registers a new source. This source may not have the same name as a source |
| 40 /// that's already been registered. | 38 /// that's already been registered. |
| 41 void register(Source source) { | 39 void register(Source source) { |
| 42 if (_map.containsKey(source.name)) { | 40 if (_sources.containsKey(source.name)) { |
| 43 throw new StateError('Source registry already has a source named ' | 41 throw new StateError('Source registry already has a source named ' |
| 44 '${source.name}'); | 42 '${source.name}'); |
| 45 } | 43 } |
| 46 | 44 |
| 47 _map[source.name] = source; | 45 _sources[source.name] = source; |
| 48 } | 46 } |
| 49 | 47 |
| 50 /// Returns `true` if there is a source named [name]. | 48 /// Returns the source named [name]. |
| 51 bool contains(String name) => _map.containsKey(name); | 49 /// |
| 52 | 50 /// Returns an [UnknownSource] if no source with that name has been |
| 53 /// Returns the source named [name]. Throws an error if no such source has | 51 /// registered. If [name] is null, returns the default source. |
| 54 /// been registered. If [name] is null, returns the default source. | |
| 55 // TODO(rnystrom): Return a NullSource that does nothing safely so that | |
| 56 // calling code doesn't have to worry about it. | |
| 57 Source operator[](String name) { | 52 Source operator[](String name) { |
| 58 if (name == null) { | 53 if (name == null) { |
| 59 if (defaultSource != null) return defaultSource; | 54 if (defaultSource != null) return defaultSource; |
| 60 throw new StateError('No default source has been registered'); | 55 throw new StateError('No default source has been registered'); |
| 61 } | 56 } |
| 62 if (_map.containsKey(name)) return _map[name]; | 57 |
| 63 throw new ArgumentError('No source named $name is registered'); | 58 if (_sources.containsKey(name)) return _sources[name]; |
| 59 return new UnknownSource(name); |
| 64 } | 60 } |
| 65 } | 61 } |
| OLD | NEW |