Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Unified Diff: frog/leg/lib/constant_map.dart

Issue 9665001: Implement constant maps. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove trailing whitespace. Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « frog/leg/compile_time_constants.dart ('k') | frog/leg/lib/js_helper.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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();
+}
« no previous file with comments | « frog/leg/compile_time_constants.dart ('k') | frog/leg/lib/js_helper.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698