Chromium Code Reviews| 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..baaa9294ef76b4e75f6f73e51fde7fe91bf83124 |
| --- /dev/null |
| +++ b/pkg/shelf/lib/src/shelf_unmodifiable_map.dart |
| @@ -0,0 +1,59 @@ |
| +// 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 new _EmptyShelfUnmodifiableMap<V>(); |
|
nweiz
2014/05/05 20:05:55
"new" -> "const"
kevmoo
2014/05/06 20:25:58
Done.
|
| + } |
| + |
| + 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> { |
| + |
|
nweiz
2014/05/05 20:05:55
Unnecessary newline.
kevmoo
2014/05/06 20:25:58
Done.
|
| + const _EmptyShelfUnmodifiableMap() : super(const {}); |
| +} |