Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(443)

Side by Side Diff: lib/src/canonicalized_map.dart

Issue 1638163002: Modernize the package's style. (Closed) Base URL: git@github.com:dart-lang/collection@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 dart.pkg.collection.canonicalized_map;
6
7 import 'dart:collection'; 5 import 'dart:collection';
8 6
9 import 'utils.dart'; 7 import 'utils.dart';
10 8
11 /** 9 /// A map whose keys are converted to canonical values of type `C`.
12 * A map whose keys are converted to canonical values of type `C`. 10 ///
13 * 11 /// This is useful for using case-insensitive String keys, for example. It's mor e
Lasse Reichstein Nielsen 2016/01/27 10:12:28 Long line. The /// comment style allows one less c
nweiz 2016/01/27 23:43:07 Done.
14 * This is useful for using case-insensitive String keys, for example. It's more 12 /// efficient than a [LinkedHashMap] with a custom equality operator because it
15 * efficient than a [LinkedHashMap] with a custom equality operator because it 13 /// only canonicalizes each key once, rather than doing so for each comparison.
16 * only canonicalizes each key once, rather than doing so for each comparison. 14 ///
17 * 15 /// By default, `null` is allowed as a key. It can be forbidden via the
18 * By default, `null` is allowed as a key. It can be forbidden via the 16 /// `isValidKey` parameter.
19 * `isValidKey` parameter.
20 */
21 class CanonicalizedMap<C, K, V> implements Map<K, V> { 17 class CanonicalizedMap<C, K, V> implements Map<K, V> {
22 final Function _canonicalize; 18 final Function _canonicalize;
23 19
24 final Function _isValidKeyFn; 20 final Function _isValidKeyFn;
25 21
26 final _base = new Map<C, Pair<K, V>>(); 22 final _base = new Map<C, Pair<K, V>>();
27 23
28 /** 24 /// Creates an empty canonicalized map.
29 * Creates an empty canonicalized map. 25 ///
30 * 26 /// The [canonicalize] function should return the canonical value for the give n
31 * The [canonicalize] function should return the canonical value for the given 27 /// key. Keys with the same canonical value are considered equivalent.
32 * key. Keys with the same canonical value are considered equivalent. 28 ///
33 * 29 /// The [isValidKey] function is called before calling [canonicalize] for
34 * The [isValidKey] function is called before calling [canonicalize] for 30 /// methods that take arbitrary objects. It can be used to filter out keys tha t
35 * methods that take arbitrary objects. It can be used to filter out keys that 31 /// can't be canonicalized.
36 * can't be canonicalized.
37 */
38 CanonicalizedMap(C canonicalize(K key), {bool isValidKey(Object key)}) 32 CanonicalizedMap(C canonicalize(K key), {bool isValidKey(Object key)})
39 : _canonicalize = canonicalize, 33 : _canonicalize = canonicalize,
40 _isValidKeyFn = isValidKey; 34 _isValidKeyFn = isValidKey;
41 35
42 /** 36 /// Creates a canonicalized map that is initialized with the key/value pairs o f
43 * Creates a canonicalized map that is initialized with the key/value pairs of 37 /// [other].
44 * [other]. 38 ///
45 * 39 /// The [canonicalize] function should return the canonical value for the give n
46 * The [canonicalize] function should return the canonical value for the given 40 /// key. Keys with the same canonical value are considered equivalent.
47 * key. Keys with the same canonical value are considered equivalent. 41 ///
48 * 42 /// The [isValidKey] function is called before calling [canonicalize] for
49 * The [isValidKey] function is called before calling [canonicalize] for 43 /// methods that take arbitrary objects. It can be used to filter out keys tha t
50 * methods that take arbitrary objects. It can be used to filter out keys that 44 /// can't be canonicalized.
51 * can't be canonicalized.
52 */
53 CanonicalizedMap.from(Map<K, V> other, C canonicalize(K key), 45 CanonicalizedMap.from(Map<K, V> other, C canonicalize(K key),
54 {bool isValidKey(Object key)}) 46 {bool isValidKey(Object key)})
55 : _canonicalize = canonicalize, 47 : _canonicalize = canonicalize,
56 _isValidKeyFn = isValidKey { 48 _isValidKeyFn = isValidKey {
57 addAll(other); 49 addAll(other);
58 } 50 }
59 51
60 V operator [](Object key) { 52 V operator [](Object key) {
61 if (!_isValidKey(key)) return null; 53 if (!_isValidKey(key)) return null;
62 var pair = _base[_canonicalize(key)]; 54 var pair = _base[_canonicalize(key)];
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 return pair == null ? null : pair.last; 99 return pair == null ? null : pair.last;
108 } 100 }
109 101
110 Iterable<V> get values => _base.values.map((pair) => pair.last); 102 Iterable<V> get values => _base.values.map((pair) => pair.last);
111 103
112 String toString() => Maps.mapToString(this); 104 String toString() => Maps.mapToString(this);
113 105
114 bool _isValidKey(Object key) => (key == null || key is K) && 106 bool _isValidKey(Object key) => (key == null || key is K) &&
115 (_isValidKeyFn == null || _isValidKeyFn(key)); 107 (_isValidKeyFn == null || _isValidKeyFn(key));
116 } 108 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698