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 library yaml_map; | 5 library yaml.map; |
6 | |
7 import 'dart:collection'; | |
8 | |
9 import 'package:collection/collection.dart'; | |
6 | 10 |
7 import 'deep_equals.dart'; | 11 import 'deep_equals.dart'; |
8 import 'utils.dart'; | 12 import 'utils.dart'; |
9 | 13 |
10 /// This class wraps behaves almost identically to the normal Dart Map | 14 /// This class behaves almost identically to the normal Dart [Map] |
11 /// implementation, with the following differences: | 15 /// implementation, with the following differences: |
12 /// | 16 /// |
13 /// * It allows null, NaN, boolean, list, and map keys. | 17 /// * It NaN, list, and map keys. |
Bob Nystrom
2014/05/09 00:18:56
"It NaN" -> "It allows NaN".
nweiz
2014/05/20 00:15:07
Done.
| |
14 /// * It defines `==` structurally. That is, `yamlMap1 == yamlMap2` if they | 18 /// * It defines `==` structurally. That is, `yamlMap1 == yamlMap2` if they |
15 /// have the same contents. | 19 /// have the same contents. |
16 /// * It has a compatible [hashCode] method. | 20 /// * It has a compatible [hashCode] method. |
17 class YamlMap implements Map { | 21 /// |
18 final Map _map; | 22 /// This class is deprecated. In future releases, this package will use |
23 /// a [HashMap] with a custom equality operation rather than a custom class. | |
24 @Deprecated('1.0.0') | |
25 class YamlMap extends DelegatingMap { | |
26 YamlMap() | |
27 : super(new HashMap(equals: deepEquals, hashCode: hashCodeFor)); | |
19 | 28 |
20 YamlMap() : _map = new Map(); | 29 YamlMap.from(Map map) |
21 | 30 : super(new HashMap(equals: deepEquals, hashCode: hashCodeFor)) { |
22 YamlMap.from(Map map) : _map = new Map.from(map); | 31 addAll(map); |
23 | |
24 YamlMap._wrap(this._map); | |
25 | |
26 void addAll(Map other) { | |
27 other.forEach((key, value) { | |
28 this[key] = value; | |
29 }); | |
30 } | 32 } |
31 | 33 |
32 bool containsValue(value) => _map.containsValue(value); | 34 int get hashCode => hashCodeFor(this); |
33 bool containsKey(key) => _map.containsKey(_wrapKey(key)); | |
34 operator [](key) => _map[_wrapKey(key)]; | |
35 operator []=(key, value) { _map[_wrapKey(key)] = value; } | |
36 putIfAbsent(key, ifAbsent()) => _map.putIfAbsent(_wrapKey(key), ifAbsent); | |
37 remove(key) => _map.remove(_wrapKey(key)); | |
38 void clear() => _map.clear(); | |
39 void forEach(void f(key, value)) => | |
40 _map.forEach((k, v) => f(_unwrapKey(k), v)); | |
41 Iterable get keys => _map.keys.map(_unwrapKey); | |
42 Iterable get values => _map.values; | |
43 int get length => _map.length; | |
44 bool get isEmpty => _map.isEmpty; | |
45 bool get isNotEmpty => _map.isNotEmpty; | |
46 String toString() => _map.toString(); | |
47 | |
48 int get hashCode => hashCodeFor(_map); | |
49 | 35 |
50 bool operator ==(other) { | 36 bool operator ==(other) { |
51 if (other is! YamlMap) return false; | 37 if (other is! YamlMap) return false; |
52 return deepEquals(this, other); | 38 return deepEquals(this, other); |
53 } | 39 } |
54 | |
55 /// Wraps an object for use as a key in the map. | |
56 _wrapKey(obj) { | |
57 if (obj != null && obj is! bool && obj is! List && | |
58 (obj is! double || !obj.isNan) && | |
59 (obj is! Map || obj is YamlMap)) { | |
60 return obj; | |
61 } else if (obj is Map) { | |
62 return new YamlMap._wrap(obj); | |
63 } | |
64 return new _WrappedHashKey(obj); | |
65 } | |
66 | |
67 /// Unwraps an object that was used as a key in the map. | |
68 _unwrapKey(obj) => obj is _WrappedHashKey ? obj.value : obj; | |
69 } | 40 } |
70 | |
71 /// A class for wrapping normally-unhashable objects that are being used as keys | |
72 /// in a YamlMap. | |
73 class _WrappedHashKey { | |
74 final value; | |
75 | |
76 _WrappedHashKey(this.value); | |
77 | |
78 int get hashCode => hashCodeFor(value); | |
79 | |
80 String toString() => value.toString(); | |
81 | |
82 /// This is defined as both values being structurally equal. | |
83 bool operator ==(other) { | |
84 if (other is! _WrappedHashKey) return false; | |
85 return deepEquals(this.value, other.value); | |
86 } | |
87 } | |
OLD | NEW |