adapt<E> method
Converts the given source or this into a strongly-typed Set<E>.
- If
sourceis HashSet or collection.HashSet, elements are cast toE. - If
sourceis a native Set, a typed copy is returned. - Otherwise, throws IllegalArgumentException.
Example:
final adaptable = AdaptableSet();
adaptable.addAll([1, 2, 3]);
final typedSet = adaptable.adapt<int>(); // Set<int>
Implementation
Set<E> adapt<E>([Object? source]) {
source ??= this;
if (source is HashSet) {
final set = HashSet<E>();
for (var e in source) {
set.add(e as E);
}
return set;
}
if (source is collection.HashSet) {
final set = collection.HashSet<E>();
for (var e in source) {
set.add(e as E);
}
return set;
}
if (source is Set) {
return Set<E>.from(source);
}
throw IllegalArgumentException('Cannot adapt $source to Set<$E>');
}