Index: frog/leg/lib/constant_map.dart |
diff --git a/frog/leg/lib/constant_map.dart b/frog/leg/lib/constant_map.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0b12f2152c58a29e5ece48809b30dc6dd6d20b2a |
--- /dev/null |
+++ b/frog/leg/lib/constant_map.dart |
@@ -0,0 +1,50 @@ |
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
+// 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. |
+ |
+// This class has no constructor. This is on purpose since the instantiation |
+// is shortcut by the compiler. |
+class ConstantMap<V> implements Map<String, V> { |
+ final int length; |
+ // A constant map is backed by a JavaScript object. |
+ final _jsObject; |
+ final List<String> _keys; |
+ |
+ bool containsValue(V needle) { |
+ return getValues().some((V value) => value == needle); |
+ } |
+ |
+ bool containsKey(String key) { |
+ if (key == '__proto__') return false; |
+ return jsHasOwnProperty(_jsObject, key); |
+ } |
+ |
+ V operator [](String key) { |
+ if (!containsKey(key)) return null; |
+ return jsPropertyAccess(_jsObject, key); |
+ } |
+ |
+ void forEach(void f(String key, V value)) { |
+ _keys.forEach((String key) => f(key, this[key])); |
+ } |
+ |
+ Collection<String> getKeys() => _keys; |
+ |
+ Collection<V> getValues() { |
+ List<V> result = <V>[]; |
+ _keys.forEach((String key) => result.add(this[key])); |
+ return result; |
+ } |
+ |
+ bool isEmpty() => length == 0; |
+ |
+ String toString() => Maps.mapToString(this); |
+ |
+ _throwImmutable() { |
+ throw const IllegalAccessException(); |
+ } |
+ void operator []=(String key, V val) => _throwImmutable(); |
+ V putIfAbsent(String key, V ifAbsent()) => _throwImmutable(); |
+ V remove(String key) => _throwImmutable(); |
+ void clear() => _throwImmutable(); |
+} |