| 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. | 10 * * It is itself Hashable. |
| 11 * * It defines `==` structurally. That is, `yamlMap1 == yamlMap2` if they have | 11 * * It defines `==` structurally. That is, `yamlMap1 == yamlMap2` if they have |
| 12 * the same contents. | 12 * the same contents. |
| 13 */ | 13 */ |
| 14 class YamlMap implements Map, Hashable { | 14 class YamlMap implements Map, Hashable { |
| 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); |
| 22 |
| 21 bool containsValue(value) => _map.containsValue(value); | 23 bool containsValue(value) => _map.containsValue(value); |
| 22 bool containsKey(key) => _map.containsKey(_wrapKey(key)); | 24 bool containsKey(key) => _map.containsKey(_wrapKey(key)); |
| 23 operator [](key) => _map[_wrapKey(key)]; | 25 operator [](key) => _map[_wrapKey(key)]; |
| 24 operator []=(key, value) { _map[_wrapKey(key)] = value; } | 26 operator []=(key, value) { _map[_wrapKey(key)] = value; } |
| 25 putIfAbsent(key, ifAbsent()) => _map.putIfAbsent(_wrapKey(key), ifAbsent); | 27 putIfAbsent(key, ifAbsent()) => _map.putIfAbsent(_wrapKey(key), ifAbsent); |
| 26 remove(key) => _map.remove(_wrapKey(key)); | 28 remove(key) => _map.remove(_wrapKey(key)); |
| 27 void clear() => _map.clear(); | 29 void clear() => _map.clear(); |
| 28 void forEach(void f(key, value)) => | 30 void forEach(void f(key, value)) => |
| 29 _map.forEach((k, v) => f(_unwrapKey(k), v)); | 31 _map.forEach((k, v) => f(_unwrapKey(k), v)); |
| 30 Collection getKeys() => _map.getKeys().map(_unwrapKey); | 32 Collection getKeys() => _map.getKeys().map(_unwrapKey); |
| 31 Collection getValues() => _map.getValues(); | 33 Collection getValues() => _map.getValues(); |
| 32 int get length() => _map.length; | 34 int get length() => _map.length; |
| 33 bool isEmpty() => _map.isEmpty(); | 35 bool isEmpty() => _map.isEmpty(); |
| 34 String toString() => _map.toString(); | 36 String toString() => _map.toString(); |
| 35 | 37 |
| 36 int hashCode() => _hashCode(_map); | 38 int hashCode() => _hashCode(_map); |
| 37 | 39 |
| 38 bool operator ==(other) { | 40 bool operator ==(other) { |
| 39 if (other is! YamlMap) return false; | 41 if (other is! YamlMap) return false; |
| 40 return deepEquals(this, other); | 42 return deepEquals(this, other); |
| 41 } | 43 } |
| 42 | 44 |
| 43 /** Wraps an object for use as a key in the map. */ | 45 /** Wraps an object for use as a key in the map. */ |
| 44 _wrapKey(obj) { | 46 _wrapKey(obj) { |
| 45 if (obj != null && obj is! bool && obj is! List && | 47 if (obj != null && obj is! bool && obj is! List && |
| 46 (obj is! double || !obj.isNan()) && | 48 (obj is! double || !obj.isNan()) && |
| 47 (obj is! Map || obj is YamlMap)) { | 49 (obj is! Map || obj is YamlMap)) { |
| 48 return obj; | 50 return obj; |
| 51 } else if (obj is Map) { |
| 52 return new YamlMap._wrap(obj); |
| 49 } | 53 } |
| 50 return new _WrappedHashKey(obj); | 54 return new _WrappedHashKey(obj); |
| 51 } | 55 } |
| 52 | 56 |
| 53 /** 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. */ |
| 54 _unwrapKey(obj) => obj is _WrappedHashKey ? obj.value : obj; | 58 _unwrapKey(obj) => obj is _WrappedHashKey ? obj.value : obj; |
| 55 } | 59 } |
| 56 | 60 |
| 57 /** | 61 /** |
| 58 * 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 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 for (var e in obj) { | 105 for (var e in obj) { |
| 102 hash ^= _hashCode(e, parents); | 106 hash ^= _hashCode(e, parents); |
| 103 } | 107 } |
| 104 return hash; | 108 return hash; |
| 105 } | 109 } |
| 106 return obj.hashCode(); | 110 return obj.hashCode(); |
| 107 } finally { | 111 } finally { |
| 108 parents.removeLast(); | 112 parents.removeLast(); |
| 109 } | 113 } |
| 110 } | 114 } |
| OLD | NEW |