OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library shelf.shelf_unmodifiable_map; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 // TODO(kevmoo): use UnmodifiableMapView from SDK once 1.4 ships |
| 10 import 'package:collection/wrappers.dart' as pc; |
| 11 |
| 12 /// A simple wrapper over [pc.UnmodifiableMapView] which avoids re-wrapping |
| 13 /// itself. |
| 14 class ShelfUnmodifiableMap<V> extends pc.UnmodifiableMapView<String, V> { |
| 15 /// If [source] is a [ShelfUnmodifiableMap] with matching [ignoreKeyCase], |
| 16 /// then [source] is returned. |
| 17 /// |
| 18 /// If [source] is `null` it is treated like an empty map. |
| 19 /// |
| 20 /// If [ignoreKeyCase] is `true`, the keys will have case-insensitive access. |
| 21 /// |
| 22 /// [source] is copied to a new [Map] to ensure changes to the paramater value |
| 23 /// after constructions are not reflected. |
| 24 factory ShelfUnmodifiableMap(Map<String, V> source, |
| 25 {bool ignoreKeyCase: false}) { |
| 26 if (source is ShelfUnmodifiableMap<V>) { |
| 27 return source; |
| 28 } |
| 29 |
| 30 if (source == null || source.isEmpty) { |
| 31 return const _EmptyShelfUnmodifiableMap(); |
| 32 } |
| 33 |
| 34 if (ignoreKeyCase) { |
| 35 // TODO(kevmoo) generalize this model with a 'canonical map' to align with |
| 36 // similiar implementation in http pkg [BaseRequest]. |
| 37 var map = new LinkedHashMap<String, V>( |
| 38 equals: (key1, key2) => key1.toLowerCase() == key2.toLowerCase(), |
| 39 hashCode: (key) => key.toLowerCase().hashCode); |
| 40 |
| 41 map.addAll(source); |
| 42 |
| 43 source = map; |
| 44 } else { |
| 45 source = new Map<String, V>.from(source); |
| 46 } |
| 47 |
| 48 return new ShelfUnmodifiableMap<V>._(source); |
| 49 } |
| 50 |
| 51 ShelfUnmodifiableMap._(Map<String, V> source) : super(source); |
| 52 } |
| 53 |
| 54 /// An const empty implementation of [ShelfUnmodifiableMap]. |
| 55 class _EmptyShelfUnmodifiableMap<V> extends pc.DelegatingMap<String, V> |
| 56 implements ShelfUnmodifiableMap<V> { |
| 57 const _EmptyShelfUnmodifiableMap() : super(const {}); |
| 58 } |
OLD | NEW |