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

Unified Diff: pkg/shelf/lib/src/shelf_unmodifiable_map.dart

Issue 256753004: pkg/shelf: change helper method on Request and Response (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: nevermind Created 6 years, 7 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 | « pkg/shelf/lib/src/response.dart ('k') | pkg/shelf/lib/src/util.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/shelf/lib/src/shelf_unmodifiable_map.dart
diff --git a/pkg/shelf/lib/src/shelf_unmodifiable_map.dart b/pkg/shelf/lib/src/shelf_unmodifiable_map.dart
new file mode 100644
index 0000000000000000000000000000000000000000..34775f092f7ab93200c52084b8acb378c02a2d23
--- /dev/null
+++ b/pkg/shelf/lib/src/shelf_unmodifiable_map.dart
@@ -0,0 +1,58 @@
+// Copyright (c) 2014, 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.
+
+library shelf.shelf_unmodifiable_map;
+
+import 'dart:collection';
+
+// TODO(kevmoo): use UnmodifiableMapView from SDK once 1.4 ships
+import 'package:collection/wrappers.dart' as pc;
+
+/// A simple wrapper over [pc.UnmodifiableMapView] which avoids re-wrapping
+/// itself.
+class ShelfUnmodifiableMap<V> extends pc.UnmodifiableMapView<String, V> {
+ /// If [source] is a [ShelfUnmodifiableMap] with matching [ignoreKeyCase],
+ /// then [source] is returned.
+ ///
+ /// If [source] is `null` it is treated like an empty map.
+ ///
+ /// If [ignoreKeyCase] is `true`, the keys will have case-insensitive access.
+ ///
+ /// [source] is copied to a new [Map] to ensure changes to the paramater value
+ /// after constructions are not reflected.
+ factory ShelfUnmodifiableMap(Map<String, V> source,
+ {bool ignoreKeyCase: false}) {
+ if (source is ShelfUnmodifiableMap<V>) {
+ return source;
+ }
+
+ if (source == null || source.isEmpty) {
+ return const _EmptyShelfUnmodifiableMap();
+ }
+
+ if (ignoreKeyCase) {
+ // TODO(kevmoo) generalize this model with a 'canonical map' to align with
+ // similiar implementation in http pkg [BaseRequest].
+ var map = new LinkedHashMap<String, V>(
+ equals: (key1, key2) => key1.toLowerCase() == key2.toLowerCase(),
+ hashCode: (key) => key.toLowerCase().hashCode);
+
+ map.addAll(source);
+
+ source = map;
+ } else {
+ source = new Map<String, V>.from(source);
+ }
+
+ return new ShelfUnmodifiableMap<V>._(source);
+ }
+
+ ShelfUnmodifiableMap._(Map<String, V> source) : super(source);
+}
+
+/// An const empty implementation of [ShelfUnmodifiableMap].
+class _EmptyShelfUnmodifiableMap<V> extends pc.DelegatingMap<String, V>
+ implements ShelfUnmodifiableMap<V> {
+ const _EmptyShelfUnmodifiableMap() : super(const {});
+}
« no previous file with comments | « pkg/shelf/lib/src/response.dart ('k') | pkg/shelf/lib/src/util.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698