Index: packages/collection/lib/src/canonicalized_map.dart |
diff --git a/packages/collection/lib/src/canonicalized_map.dart b/packages/collection/lib/src/canonicalized_map.dart |
index 7ee3f86c4c717ae4f7c38c72fdf789d19ee926ea..a8af43a0a854b3a313b9d996a8018e69b6e52ef6 100644 |
--- a/packages/collection/lib/src/canonicalized_map.dart |
+++ b/packages/collection/lib/src/canonicalized_map.dart |
@@ -2,56 +2,53 @@ |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
-library dart.pkg.collection.canonicalized_map; |
- |
import 'dart:collection'; |
import 'utils.dart'; |
-/** |
- * A map whose keys are converted to canonical values of type `C`. |
- * |
- * This is useful for using case-insensitive String keys, for example. It's more |
- * efficient than a [LinkedHashMap] with a custom equality operator because it |
- * only canonicalizes each key once, rather than doing so for each comparison. |
- * |
- * By default, `null` is allowed as a key. It can be forbidden via the |
- * `isValidKey` parameter. |
- */ |
+typedef C _Canonicalize<C, K>(K key); |
+ |
+typedef bool _IsValidKey(Object key); |
+ |
+/// A map whose keys are converted to canonical values of type `C`. |
+/// |
+/// This is useful for using case-insensitive String keys, for example. It's |
+/// more efficient than a [LinkedHashMap] with a custom equality operator |
+/// because it only canonicalizes each key once, rather than doing so for each |
+/// comparison. |
+/// |
+/// By default, `null` is allowed as a key. It can be forbidden via the |
+/// `isValidKey` parameter. |
class CanonicalizedMap<C, K, V> implements Map<K, V> { |
- final Function _canonicalize; |
+ final _Canonicalize<C, K> _canonicalize; |
- final Function _isValidKeyFn; |
+ final _IsValidKey _isValidKeyFn; |
final _base = new Map<C, Pair<K, V>>(); |
- /** |
- * Creates an empty canonicalized map. |
- * |
- * The [canonicalize] function should return the canonical value for the given |
- * key. Keys with the same canonical value are considered equivalent. |
- * |
- * The [isValidKey] function is called before calling [canonicalize] for |
- * methods that take arbitrary objects. It can be used to filter out keys that |
- * can't be canonicalized. |
- */ |
+ /// Creates an empty canonicalized map. |
+ /// |
+ /// The [canonicalize] function should return the canonical value for the |
+ /// given key. Keys with the same canonical value are considered equivalent. |
+ /// |
+ /// The [isValidKey] function is called before calling [canonicalize] for |
+ /// methods that take arbitrary objects. It can be used to filter out keys |
+ /// that can't be canonicalized. |
CanonicalizedMap(C canonicalize(K key), {bool isValidKey(Object key)}) |
: _canonicalize = canonicalize, |
_isValidKeyFn = isValidKey; |
- /** |
- * Creates a canonicalized map that is initialized with the key/value pairs of |
- * [other]. |
- * |
- * The [canonicalize] function should return the canonical value for the given |
- * key. Keys with the same canonical value are considered equivalent. |
- * |
- * The [isValidKey] function is called before calling [canonicalize] for |
- * methods that take arbitrary objects. It can be used to filter out keys that |
- * can't be canonicalized. |
- */ |
+ /// Creates a canonicalized map that is initialized with the key/value pairs |
+ /// of [other]. |
+ /// |
+ /// The [canonicalize] function should return the canonical value for the |
+ /// given key. Keys with the same canonical value are considered equivalent. |
+ /// |
+ /// The [isValidKey] function is called before calling [canonicalize] for |
+ /// methods that take arbitrary objects. It can be used to filter out keys |
+ /// that can't be canonicalized. |
CanonicalizedMap.from(Map<K, V> other, C canonicalize(K key), |
- {bool isValidKey(Object key)}) |
+ {bool isValidKey(Object key)}) |
: _canonicalize = canonicalize, |
_isValidKeyFn = isValidKey { |
addAll(other); |
@@ -59,11 +56,12 @@ class CanonicalizedMap<C, K, V> implements Map<K, V> { |
V operator [](Object key) { |
if (!_isValidKey(key)) return null; |
- var pair = _base[_canonicalize(key)]; |
+ var pair = _base[_canonicalize(key as K)]; |
return pair == null ? null : pair.last; |
} |
void operator []=(K key, V value) { |
+ if (!_isValidKey(key)) return; |
_base[_canonicalize(key)] = new Pair(key, value); |
} |
@@ -77,7 +75,7 @@ class CanonicalizedMap<C, K, V> implements Map<K, V> { |
bool containsKey(Object key) { |
if (!_isValidKey(key)) return false; |
- return _base.containsKey(_canonicalize(key)); |
+ return _base.containsKey(_canonicalize(key as K)); |
} |
bool containsValue(Object value) => |
@@ -96,13 +94,14 @@ class CanonicalizedMap<C, K, V> implements Map<K, V> { |
int get length => _base.length; |
V putIfAbsent(K key, V ifAbsent()) { |
- return _base.putIfAbsent(_canonicalize(key), |
- () => new Pair(key, ifAbsent())).last; |
+ return _base |
+ .putIfAbsent(_canonicalize(key), () => new Pair(key, ifAbsent())) |
+ .last; |
} |
V remove(Object key) { |
if (!_isValidKey(key)) return null; |
- var pair = _base.remove(_canonicalize(key)); |
+ var pair = _base.remove(_canonicalize(key as K)); |
return pair == null ? null : pair.last; |
} |
@@ -110,6 +109,7 @@ class CanonicalizedMap<C, K, V> implements Map<K, V> { |
String toString() => Maps.mapToString(this); |
- bool _isValidKey(Object key) => (key == null || key is K) && |
+ bool _isValidKey(Object key) => |
+ (key == null || key is K) && |
(_isValidKeyFn == null || _isValidKeyFn(key)); |
} |