cloneSet<E> method

  1. @override
Set<E> cloneSet<E>(
  1. Set<E> source
)
override

Return a deep-cloned Set.

Preserves the concrete Set implementation (e.g. LinkedHashSet, HashSet, UnmodifiableSetView). When doTypedClone is true the returned set preserves element types and performs typed cloning of elements. Typed cloning will throw UnsupportedTypedCloneException if a nested Map is encountered.

Implementation

@override
Set<E> cloneSet<E>(Set<E> source) {
  final isUnmodifiable = source is UnmodifiableSetView<E>;
  final Set<E> cloned;
  if (doTypedClone) {
    cloned = source.toSet();
    cloned.clear();
    for (final e in source) {
      cloned.add(_cloneValueTyped(e));
    }
  } else {
    cloned = switch (source) {
      LinkedHashSet<E>() => {for (final e in source) cloneValue(e)},
      HashSet<E>() => HashSet<E>.of(source.map((e) => cloneValue(e))),
      _ => {for (final e in source) cloneValue(e)},
    };
  }
  return isUnmodifiable ? UnmodifiableSetView<E>(cloned) : cloned;
}