Line data Source code
1 : // Copyright (c) 2018, 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 : import 'dart:collection'; 6 : 7 : import '../path.dart' as p; 8 : 9 : /// A map whose keys are paths, compared using [p.equals] and [p.hash]. 10 : class PathMap<V> extends MapView<String?, V> { 11 : /// Creates an empty [PathMap] whose keys are compared using `context.equals` 12 : /// and `context.hash`. 13 : /// 14 : /// The [context] defaults to the current path context. 15 0 : PathMap({p.Context? context}) : super(_create(context)); 16 : 17 : /// Creates a [PathMap] with the same keys and values as [other] whose keys 18 : /// are compared using `context.equals` and `context.hash`. 19 : /// 20 : /// The [context] defaults to the current path context. If multiple keys in 21 : /// [other] represent the same logical path, the last key's value will be 22 : /// used. 23 0 : PathMap.of(Map<String, V> other, {p.Context? context}) 24 0 : : super(_create(context)..addAll(other)); 25 : 26 : /// Creates a map that uses [context] for equality and hashing. 27 0 : static Map<String?, V> _create<V>(p.Context? context) { 28 0 : context ??= p.context; 29 0 : return LinkedHashMap( 30 0 : equals: (path1, path2) { 31 : if (path1 == null) return path2 == null; 32 : if (path2 == null) return false; 33 0 : return context!.equals(path1, path2); 34 : }, 35 0 : hashCode: (path) => path == null ? 0 : context!.hash(path), 36 0 : isValidKey: (path) => path is String || path == null); 37 : } 38 : }