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 set containing paths, compared using [p.equals] and [p.hash]. 10 : class PathSet extends IterableBase<String?> implements Set<String?> { 11 : /// The set to which we forward implementation methods. 12 : final Set<String?> _inner; 13 : 14 : /// Creates an empty [PathSet] whose contents are compared using 15 : /// `context.equals` and `context.hash`. 16 : /// 17 : /// The [context] defaults to the current path context. 18 0 : PathSet({p.Context? context}) : _inner = _create(context); 19 : 20 : /// Creates a [PathSet] with the same contents as [other] whose elements are 21 : /// compared using `context.equals` and `context.hash`. 22 : /// 23 : /// The [context] defaults to the current path context. If multiple elements 24 : /// in [other] represent the same logical path, the first value will be 25 : /// used. 26 0 : PathSet.of(Iterable<String> other, {p.Context? context}) 27 0 : : _inner = _create(context)..addAll(other); 28 : 29 : /// Creates a set that uses [context] for equality and hashing. 30 0 : static Set<String?> _create(p.Context? context) { 31 0 : context ??= p.context; 32 0 : return LinkedHashSet( 33 0 : equals: (path1, path2) { 34 : if (path1 == null) return path2 == null; 35 : if (path2 == null) return false; 36 0 : return context!.equals(path1, path2); 37 : }, 38 0 : hashCode: (path) => path == null ? 0 : context!.hash(path), 39 0 : isValidKey: (path) => path is String || path == null); 40 : } 41 : 42 : // Normally we'd use DelegatingSetView from the collection package to 43 : // implement these, but we want to avoid adding dependencies from path because 44 : // it's so widely used that even brief version skew can be very painful. 45 : 46 0 : @override 47 0 : Iterator<String?> get iterator => _inner.iterator; 48 : 49 0 : @override 50 0 : int get length => _inner.length; 51 : 52 0 : @override 53 0 : bool add(String? value) => _inner.add(value); 54 : 55 0 : @override 56 0 : void addAll(Iterable<String?> elements) => _inner.addAll(elements); 57 : 58 0 : @override 59 0 : Set<T> cast<T>() => _inner.cast<T>(); 60 : 61 0 : @override 62 0 : void clear() => _inner.clear(); 63 : 64 0 : @override 65 0 : bool contains(Object? element) => _inner.contains(element); 66 : 67 0 : @override 68 0 : bool containsAll(Iterable<Object?> other) => _inner.containsAll(other); 69 : 70 0 : @override 71 0 : Set<String?> difference(Set<Object?> other) => _inner.difference(other); 72 : 73 0 : @override 74 0 : Set<String?> intersection(Set<Object?> other) => _inner.intersection(other); 75 : 76 0 : @override 77 0 : String? lookup(Object? element) => _inner.lookup(element); 78 : 79 0 : @override 80 0 : bool remove(Object? value) => _inner.remove(value); 81 : 82 0 : @override 83 0 : void removeAll(Iterable<Object?> elements) => _inner.removeAll(elements); 84 : 85 0 : @override 86 0 : void removeWhere(bool Function(String?) test) => _inner.removeWhere(test); 87 : 88 0 : @override 89 0 : void retainAll(Iterable<Object?> elements) => _inner.retainAll(elements); 90 : 91 0 : @override 92 0 : void retainWhere(bool Function(String?) test) => _inner.retainWhere(test); 93 : 94 0 : @override 95 0 : Set<String?> union(Set<String?> other) => _inner.union(other); 96 : 97 0 : @override 98 0 : Set<String?> toSet() => _inner.toSet(); 99 : }