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 // TODO(kevmoo): MapView lacks a const ctor, so we have to use DelegatingMap | |
| 10 // from pkg/collection - https://codereview.chromium.org/294093003/ | |
| 11 import 'package:collection/wrappers.dart' as pc; | 9 import 'package:collection/wrappers.dart' as pc; |
| 12 | 10 |
| 13 /// A simple wrapper over [pc.UnmodifiableMapView] which avoids re-wrapping | 11 /// A simple wrapper over [pc.UnmodifiableMapView] which avoids re-wrapping |
| 14 /// itself. | 12 /// itself. |
| 15 class ShelfUnmodifiableMap<V> extends UnmodifiableMapView<String, V> { | 13 class ShelfUnmodifiableMap<V> extends UnmodifiableMapView<String, V> { |
| 16 /// If [source] is a [ShelfUnmodifiableMap] with matching [ignoreKeyCase], | 14 /// If [source] is a [ShelfUnmodifiableMap] with matching [ignoreKeyCase], |
| 17 /// then [source] is returned. | 15 /// then [source] is returned. |
| 18 /// | 16 /// |
| 19 /// If [source] is `null` it is treated like an empty map. | 17 /// If [source] is `null` it is treated like an empty map. |
| 20 /// | 18 /// |
| 21 /// If [ignoreKeyCase] is `true`, the keys will have case-insensitive access. | 19 /// If [ignoreKeyCase] is `true`, the keys will have case-insensitive access. |
| 22 /// | 20 /// |
| 23 /// [source] is copied to a new [Map] to ensure changes to the paramater value | 21 /// [source] is copied to a new [Map] to ensure changes to the paramater value |
| 24 /// after constructions are not reflected. | 22 /// after constructions are not reflected. |
| 25 factory ShelfUnmodifiableMap(Map<String, V> source, | 23 factory ShelfUnmodifiableMap(Map<String, V> source, |
| 26 {bool ignoreKeyCase: false}) { | 24 {bool ignoreKeyCase: false}) { |
| 27 if (source is ShelfUnmodifiableMap<V>) { | 25 if (source is ShelfUnmodifiableMap<V>) { |
| 28 return source; | 26 return source; |
| 29 } | 27 } |
| 30 | 28 |
| 31 if (source == null || source.isEmpty) { | 29 if (source == null || source.isEmpty) { |
| 32 return const _EmptyShelfUnmodifiableMap(); | 30 return const _EmptyShelfUnmodifiableMap(); |
| 33 } | 31 } |
| 34 | 32 |
| 35 if (ignoreKeyCase) { | 33 if (ignoreKeyCase) { |
| 36 // TODO(kevmoo) generalize this model with a 'canonical map' to align with | 34 source = new pc.CanonicalizedMap<String, String, V>.from(source, |
| 37 // similiar implementation in http pkg [BaseRequest]. | 35 (key) => key.toLowerCase()); |
|
nweiz
2014/07/07 19:59:42
Add an [isValidKey] function that asserts that the
kevmoo
2014/07/07 23:52:44
Done.
| |
| 38 var map = new LinkedHashMap<String, V>( | |
| 39 equals: (key1, key2) => key1.toLowerCase() == key2.toLowerCase(), | |
| 40 hashCode: (key) => key.toLowerCase().hashCode); | |
| 41 | |
| 42 map.addAll(source); | |
| 43 | |
| 44 source = map; | |
| 45 } else { | 36 } else { |
| 46 source = new Map<String, V>.from(source); | 37 source = new Map<String, V>.from(source); |
| 47 } | 38 } |
| 48 | 39 |
| 49 return new ShelfUnmodifiableMap<V>._(source); | 40 return new ShelfUnmodifiableMap<V>._(source); |
| 50 } | 41 } |
| 51 | 42 |
| 52 ShelfUnmodifiableMap._(Map<String, V> source) : super(source); | 43 ShelfUnmodifiableMap._(Map<String, V> source) : super(source); |
| 53 } | 44 } |
| 54 | 45 |
| 55 /// An const empty implementation of [ShelfUnmodifiableMap]. | 46 /// An const empty implementation of [ShelfUnmodifiableMap]. |
| 47 // TODO(kevmoo): Consider using MapView from dart:collection which has a const | |
| 48 // ctor. Would require updating min SDK to 1.5. | |
| 56 class _EmptyShelfUnmodifiableMap<V> extends pc.DelegatingMap<String, V> | 49 class _EmptyShelfUnmodifiableMap<V> extends pc.DelegatingMap<String, V> |
| 57 implements ShelfUnmodifiableMap<V> { | 50 implements ShelfUnmodifiableMap<V> { |
| 58 const _EmptyShelfUnmodifiableMap() : super(const {}); | 51 const _EmptyShelfUnmodifiableMap() : super(const {}); |
| 59 } | 52 } |
| OLD | NEW |