blob: 46b422347475fd50d392fb1c9060ee525d5b0b76 [file] [log] [blame]
// 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';
import 'package:http_parser/http_parser.dart';
/// A simple wrapper over [UnmodifiableMapView] which avoids re-wrapping itself.
class ShelfUnmodifiableMap<V> extends UnmodifiableMapView<String, V> {
/// `true` if the key values are already lowercase.
final bool _ignoreKeyCase;
/// 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 parameter value
/// after constructions are not reflected.
factory ShelfUnmodifiableMap(Map<String, V> source,
{bool ignoreKeyCase: false}) {
if (source is ShelfUnmodifiableMap<V> &&
// !ignoreKeyCase: no transformation of the input is required
// source._ignoreKeyCase: the input cannot be transformed any more
(!ignoreKeyCase || source._ignoreKeyCase)) {
return source;
}
if (source == null || source.isEmpty) {
return const _EmptyShelfUnmodifiableMap();
}
if (ignoreKeyCase) {
source = new CaseInsensitiveMap<V>.from(source);
} else {
source = new Map<String, V>.from(source);
}
return new ShelfUnmodifiableMap<V>._(source, ignoreKeyCase);
}
ShelfUnmodifiableMap._(Map<String, V> source, this._ignoreKeyCase)
: super(source);
}
/// A const implementation of an empty [ShelfUnmodifiableMap].
class _EmptyShelfUnmodifiableMap<V> extends MapView<String, V>
implements ShelfUnmodifiableMap<V> {
bool get _ignoreKeyCase => true;
const _EmptyShelfUnmodifiableMap() : super(const {});
}