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_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 AssetSet(); | |
20 | |
21 /// Creates a new AssetSet from the contents of [other]. | |
22 /// | |
23 /// If multiple assets in [other] have the same id, the last one takes | |
24 /// precedence. | |
25 AssetSet.from(Iterable<Asset> other) { | |
26 for (var asset in other) { | |
27 _assets[asset.id] = asset; | |
28 } | |
29 } | |
30 | |
31 Iterator<Asset> get iterator => _assets.values.iterator; | |
32 | |
33 int get length => _assets.length; | |
34 | |
35 /// Gets the [Asset] in the set with [id], or returns `null` if no asset with | |
36 /// that ID is present. | |
37 Asset operator[](AssetId id) => _assets[id]; | |
38 | |
39 /// Adds [asset] to the set. | |
40 /// | |
41 /// If there is already an asset with that ID in the set, it is replaced by | |
42 /// the new one. Returns [asset]. | |
43 Asset add(Asset asset) { | |
44 _assets[asset.id] = asset; | |
45 return asset; | |
46 } | |
47 | |
48 /// Adds [assets] to the set. | |
49 void addAll(Iterable<Asset> assets) { | |
50 assets.forEach(add); | |
51 } | |
52 | |
53 /// Returns `true` if the set contains [asset]. | |
54 bool contains(Asset asset) { | |
55 var other = _assets[asset.id]; | |
56 return other == asset; | |
57 } | |
58 | |
59 /// Returns `true` if the set contains an [Asset] with [id]. | |
60 bool containsId(AssetId id) { | |
61 return _assets.containsKey(id); | |
62 } | |
63 | |
64 /// If the set contains an [Asset] with [id], removes and returns it. | |
65 Asset removeId(AssetId id) => _assets.remove(id); | |
66 | |
67 /// Removes all assets from the set. | |
68 void clear() { | |
69 _assets.clear(); | |
70 } | |
71 | |
72 String toString() => _assets.toString(); | |
73 } | |
OLD | NEW |