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 /** | 5 /** |
6 * This class wraps behaves almost identically to the normal Dart Map | 6 * This class wraps behaves almost identically to the normal Dart Map |
7 * implementation, with the following differences: | 7 * implementation, with the following differences: |
8 * | 8 * |
9 * * It allows null, NaN, boolean, list, and map keys. | 9 * * It allows null, NaN, boolean, list, and map keys. |
10 * * It is itself Hashable. | |
11 * * It defines `==` structurally. That is, `yamlMap1 == yamlMap2` if they have | 10 * * It defines `==` structurally. That is, `yamlMap1 == yamlMap2` if they have |
12 * the same contents. | 11 * the same contents. |
12 * * It has a compatible [hashCode] method. | |
13 */ | 13 */ |
14 class YamlMap implements Map, Hashable { | 14 class YamlMap implements Map { |
15 Map _map; | 15 Map _map; |
16 | 16 |
17 YamlMap() : _map = new Map(); | 17 YamlMap() : _map = new Map(); |
18 | 18 |
19 YamlMap.from(Map map) : _map = new Map.from(map); | 19 YamlMap.from(Map map) : _map = new Map.from(map); |
20 | 20 |
21 YamlMap._wrap(this._map); | 21 YamlMap._wrap(this._map); |
22 | 22 |
23 bool containsValue(value) => _map.containsValue(value); | 23 bool containsValue(value) => _map.containsValue(value); |
24 bool containsKey(key) => _map.containsKey(_wrapKey(key)); | 24 bool containsKey(key) => _map.containsKey(_wrapKey(key)); |
(...skipping 30 matching lines...) Expand all Loading... | |
55 } | 55 } |
56 | 56 |
57 /** Unwraps an object that was used as a key in the map. */ | 57 /** Unwraps an object that was used as a key in the map. */ |
58 _unwrapKey(obj) => obj is _WrappedHashKey ? obj.value : obj; | 58 _unwrapKey(obj) => obj is _WrappedHashKey ? obj.value : obj; |
59 } | 59 } |
60 | 60 |
61 /** | 61 /** |
62 * A class for wrapping normally-unhashable objects that are being used as keys | 62 * A class for wrapping normally-unhashable objects that are being used as keys |
63 * in a YamlMap. | 63 * in a YamlMap. |
64 */ | 64 */ |
65 class _WrappedHashKey implements Hashable { | 65 class _WrappedHashKey { |
Mads Ager (google)
2012/09/27 12:48:27
hep
| |
66 var value; | 66 var value; |
67 | 67 |
68 _WrappedHashKey(this.value); | 68 _WrappedHashKey(this.value); |
69 | 69 |
70 int hashCode() => _hashCode(value); | 70 int hashCode() => _hashCode(value); |
71 | 71 |
72 String toString() => value.toString(); | 72 String toString() => value.toString(); |
73 | 73 |
74 /** This is defined as both values being structurally equal. */ | 74 /** This is defined as both values being structurally equal. */ |
75 bool operator ==(other) { | 75 bool operator ==(other) { |
(...skipping 29 matching lines...) Expand all Loading... | |
105 for (var e in obj) { | 105 for (var e in obj) { |
106 hash ^= _hashCode(e, parents); | 106 hash ^= _hashCode(e, parents); |
107 } | 107 } |
108 return hash; | 108 return hash; |
109 } | 109 } |
110 return obj.hashCode(); | 110 return obj.hashCode(); |
111 } finally { | 111 } finally { |
112 parents.removeLast(); | 112 parents.removeLast(); |
113 } | 113 } |
114 } | 114 } |
OLD | NEW |