Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library shelf.shelf_unmodifiable_map; | 5 library shelf.shelf_unmodifiable_map; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:collection/wrappers.dart' as pc; | 9 import 'package:http_parser/http_parser.dart'; |
| 10 | 10 |
| 11 /// A simple wrapper over [pc.UnmodifiableMapView] which avoids re-wrapping | 11 // TODO(kevmoo): Get rid of this once we get something like |
| 12 /// itself. | 12 // https://codereview.chromium.org/1319783003/ |
|
nweiz
2015/10/20 21:06:50
Will we be able to preserve case-insensitivity wit
kevmoo
2015/10/21 00:17:17
perhaps not
| |
| 13 /// A simple wrapper over [UnmodifiableMapView] which avoids re-wrapping itself. | |
| 13 class ShelfUnmodifiableMap<V> extends UnmodifiableMapView<String, V> { | 14 class ShelfUnmodifiableMap<V> extends UnmodifiableMapView<String, V> { |
| 15 /// `true` if the key values are already lowercase. | |
| 16 final bool ignoreKeyCase; | |
|
nweiz
2015/10/20 21:06:50
Why is this public?
kevmoo
2015/10/21 00:17:17
same reason ShelfUnmodifiableMap is public – but i
nweiz
2015/10/21 00:19:21
ShelfUnmodifiableMap is public because it needs to
| |
| 17 | |
| 14 /// If [source] is a [ShelfUnmodifiableMap] with matching [ignoreKeyCase], | 18 /// If [source] is a [ShelfUnmodifiableMap] with matching [ignoreKeyCase], |
| 15 /// then [source] is returned. | 19 /// then [source] is returned. |
| 16 /// | 20 /// |
| 17 /// If [source] is `null` it is treated like an empty map. | 21 /// If [source] is `null` it is treated like an empty map. |
| 18 /// | 22 /// |
| 19 /// If [ignoreKeyCase] is `true`, the keys will have case-insensitive access. | 23 /// If [ignoreKeyCase] is `true`, the keys will have case-insensitive access. |
| 20 /// | 24 /// |
| 21 /// [source] is copied to a new [Map] to ensure changes to the parameter value | 25 /// [source] is copied to a new [Map] to ensure changes to the parameter value |
| 22 /// after constructions are not reflected. | 26 /// after constructions are not reflected. |
| 23 factory ShelfUnmodifiableMap(Map<String, V> source, | 27 factory ShelfUnmodifiableMap(Map<String, V> source, |
| 24 {bool ignoreKeyCase: false}) { | 28 {bool ignoreKeyCase: false}) { |
| 25 if (source is ShelfUnmodifiableMap<V>) { | 29 if (source is ShelfUnmodifiableMap<V> && |
| 30 // !ignoreKeyCase: no transformation of the input is required | |
| 31 // source.ignoreKeyCase: the input cannot be transformed any more | |
| 32 (!ignoreKeyCase || source.ignoreKeyCase)) { | |
| 26 return source; | 33 return source; |
| 27 } | 34 } |
| 28 | 35 |
| 29 if (source == null || source.isEmpty) { | 36 if (source == null || source.isEmpty) { |
| 30 return const _EmptyShelfUnmodifiableMap(); | 37 return const _EmptyShelfUnmodifiableMap(); |
| 31 } | 38 } |
| 32 | 39 |
| 33 if (ignoreKeyCase) { | 40 if (ignoreKeyCase) { |
| 34 source = new pc.CanonicalizedMap<String, String, V>.from( | 41 source = new CaseInsensitiveMap<V>.from(source); |
| 35 source, (key) => key.toLowerCase(), isValidKey: (key) => key != null); | |
| 36 } else { | 42 } else { |
| 37 source = new Map<String, V>.from(source); | 43 source = new Map<String, V>.from(source); |
| 38 } | 44 } |
| 39 | 45 |
| 40 return new ShelfUnmodifiableMap<V>._(source); | 46 return new ShelfUnmodifiableMap<V>._(source, ignoreKeyCase); |
| 41 } | 47 } |
| 42 | 48 |
| 43 ShelfUnmodifiableMap._(Map<String, V> source) : super(source); | 49 ShelfUnmodifiableMap._(Map<String, V> source, this.ignoreKeyCase) |
| 50 : super(source); | |
| 44 } | 51 } |
| 45 | 52 |
| 46 /// An const empty implementation of [ShelfUnmodifiableMap]. | 53 /// A const implementation of an empty [ShelfUnmodifiableMap]. |
| 47 // TODO(kevmoo): Consider using MapView from dart:collection which has a const | 54 class _EmptyShelfUnmodifiableMap<V> extends MapView<String, V> |
| 48 // ctor. Would require updating min SDK to 1.5. | |
| 49 class _EmptyShelfUnmodifiableMap<V> extends pc.DelegatingMap<String, V> | |
| 50 implements ShelfUnmodifiableMap<V> { | 55 implements ShelfUnmodifiableMap<V> { |
| 56 bool get ignoreKeyCase => true; | |
| 51 const _EmptyShelfUnmodifiableMap() : super(const {}); | 57 const _EmptyShelfUnmodifiableMap() : super(const {}); |
| 52 } | 58 } |
| OLD | NEW |