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 part of _js_helper; | 5 part of _js_helper; |
6 | 6 |
| 7 class ConstantMapView<K, V> extends UnmodifiableMapView |
| 8 implements ConstantMap { |
| 9 ConstantMapView(Map base) : super(base); |
| 10 } |
| 11 |
7 abstract class ConstantMap<K, V> implements Map<K, V> { | 12 abstract class ConstantMap<K, V> implements Map<K, V> { |
| 13 // Used to create unmodifiable maps from other maps. |
| 14 factory ConstantMap.from(Map other) { |
| 15 List keys = other.keys.toList(); |
| 16 bool allStrings = true; |
| 17 for (var k in keys) { |
| 18 if (k is! String) { |
| 19 allStrings = false; |
| 20 break; |
| 21 } |
| 22 } |
| 23 if (allStrings) { |
| 24 bool containsProto = false; |
| 25 var protoValue = null; |
| 26 var object = JS('=Object', '{}'); |
| 27 int length = 0; |
| 28 for (var k in keys) { |
| 29 var v = other[k]; |
| 30 if (k != "__proto__") { |
| 31 if (!jsHasOwnProperty(object, k)) length++; |
| 32 JS("void", "#[#] = #", object, k, v); |
| 33 } else { |
| 34 containsProto = true; |
| 35 protoValue = v; |
| 36 } |
| 37 } |
| 38 if (containsProto) { |
| 39 length++; |
| 40 return new ConstantProtoMap<K, V>._(length, object, keys, protoValue); |
| 41 } |
| 42 return new ConstantStringMap<K, V>._(length, object, keys); |
| 43 } |
| 44 // TODO(lrn): Make a proper unmodifiable map implementation. |
| 45 return new ConstantMapView<K, V>(new Map.from(other)); |
| 46 } |
| 47 |
8 const ConstantMap._(); | 48 const ConstantMap._(); |
9 | 49 |
10 bool get isEmpty => length == 0; | 50 bool get isEmpty => length == 0; |
11 | 51 |
12 bool get isNotEmpty => !isEmpty; | 52 bool get isNotEmpty => !isEmpty; |
13 | 53 |
14 String toString() => Maps.mapToString(this); | 54 String toString() => Maps.mapToString(this); |
15 | 55 |
16 _throwUnmodifiable() { | 56 static _throwUnmodifiable() { |
17 throw new UnsupportedError("Cannot modify unmodifiable Map"); | 57 throw new UnsupportedError("Cannot modify unmodifiable Map"); |
18 } | 58 } |
19 void operator []=(K key, V val) => _throwUnmodifiable(); | 59 void operator []=(K key, V val) => _throwUnmodifiable(); |
20 V putIfAbsent(K key, V ifAbsent()) => _throwUnmodifiable(); | 60 V putIfAbsent(K key, V ifAbsent()) => _throwUnmodifiable(); |
21 V remove(K key) => _throwUnmodifiable(); | 61 V remove(K key) => _throwUnmodifiable(); |
22 void clear() => _throwUnmodifiable(); | 62 void clear() => _throwUnmodifiable(); |
23 void addAll(Map<K, V> other) => _throwUnmodifiable(); | 63 void addAll(Map<K, V> other) => _throwUnmodifiable(); |
24 } | 64 } |
25 | 65 |
26 class ConstantStringMap<K, V> extends ConstantMap<K, V> { | 66 class ConstantStringMap<K, V> extends ConstantMap<K, V> { |
27 | 67 |
28 // This constructor is not used. The instantiation is shortcut by the | 68 // This constructor is not used for actual compile-time constants. |
29 // compiler. It is here to make the uninitialized final fields legal. | 69 // The instantiation of constant maps is shortcut by the compiler. |
30 const ConstantStringMap._(this.length, this._jsObject, this._keys) | 70 const ConstantStringMap._(this.length, this._jsObject, this._keys) |
31 : super._(); | 71 : super._(); |
32 | 72 |
33 final int length; | 73 final int length; |
34 // A constant map is backed by a JavaScript object. | 74 // A constant map is backed by a JavaScript object. |
35 final _jsObject; | 75 final _jsObject; |
36 final List<K> _keys; | 76 final List<K> _keys; |
37 | 77 |
38 bool containsValue(V needle) { | 78 bool containsValue(V needle) { |
39 return values.any((V value) => value == needle); | 79 return values.any((V value) => value == needle); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 Iterable<K> get keys { | 179 Iterable<K> get keys { |
140 return _getMap().keys; | 180 return _getMap().keys; |
141 } | 181 } |
142 | 182 |
143 Iterable<V> get values { | 183 Iterable<V> get values { |
144 return _getMap().values; | 184 return _getMap().values; |
145 } | 185 } |
146 | 186 |
147 int get length => _getMap().length; | 187 int get length => _getMap().length; |
148 } | 188 } |
OLD | NEW |