OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library barback.asset.asset_set; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import 'asset.dart'; |
| 10 import 'asset_id.dart'; |
| 11 |
| 12 /// A set of [Asset]s with distinct IDs. |
| 13 /// |
| 14 /// This uses the [AssetId] of each asset to determine uniqueness, so no two |
| 15 /// assets with the same ID can be in the set. |
| 16 class AssetSet extends IterableBase<Asset> { |
| 17 final _assets = new Map<AssetId, Asset>(); |
| 18 |
| 19 /// The ids of the assets in the set. |
| 20 Iterable<AssetId> get ids => _assets.keys; |
| 21 |
| 22 AssetSet(); |
| 23 |
| 24 /// Creates a new AssetSet from the contents of [other]. |
| 25 /// |
| 26 /// If multiple assets in [other] have the same id, the last one takes |
| 27 /// precedence. |
| 28 AssetSet.from(Iterable<Asset> other) { |
| 29 for (var asset in other) { |
| 30 _assets[asset.id] = asset; |
| 31 } |
| 32 } |
| 33 |
| 34 Iterator<Asset> get iterator => _assets.values.iterator; |
| 35 |
| 36 int get length => _assets.length; |
| 37 |
| 38 /// Gets the [Asset] in the set with [id], or returns `null` if no asset with |
| 39 /// that ID is present. |
| 40 Asset operator[](AssetId id) => _assets[id]; |
| 41 |
| 42 /// Adds [asset] to the set. |
| 43 /// |
| 44 /// If there is already an asset with that ID in the set, it is replaced by |
| 45 /// the new one. Returns [asset]. |
| 46 Asset add(Asset asset) { |
| 47 _assets[asset.id] = asset; |
| 48 return asset; |
| 49 } |
| 50 |
| 51 /// Adds [assets] to the set. |
| 52 void addAll(Iterable<Asset> assets) { |
| 53 assets.forEach(add); |
| 54 } |
| 55 |
| 56 /// Returns `true` if the set contains [asset]. |
| 57 bool contains(Asset asset) { |
| 58 var other = _assets[asset.id]; |
| 59 return other == asset; |
| 60 } |
| 61 |
| 62 /// Returns `true` if the set contains an [Asset] with [id]. |
| 63 bool containsId(AssetId id) { |
| 64 return _assets.containsKey(id); |
| 65 } |
| 66 |
| 67 /// If the set contains an [Asset] with [id], removes and returns it. |
| 68 Asset removeId(AssetId id) => _assets.remove(id); |
| 69 |
| 70 /// Removes all assets from the set. |
| 71 void clear() { |
| 72 _assets.clear(); |
| 73 } |
| 74 |
| 75 String toString() => _assets.toString(); |
| 76 } |
OLD | NEW |