Collection.fromCardSets constructor

Collection.fromCardSets(
  1. Iterable<CardSetInventory> cardSets, {
  2. int count(
    1. Card
    ) = _defaultCount,
})

Creates a new Collection containing the cards from each card set.

For each copy of a card, a single card is added to the collection. To set a different number of copies of a card, provide a count closure; note that the number of copies of a card must be greater than zero.

Implementation

factory Collection.fromCardSets(
  Iterable<CardSetInventory> cardSets, {
  int Function(Card) count = _defaultCount,
}) {
  final map = <CardSet, Map<Card, int>>{};
  for (final cardSet in cardSets) {
    for (final card in cardSet.cards) {
      final copies = count(card);
      RangeError.checkNotNegative(copies, 'count');
      if (copies == 0) {
        continue;
      }
      final cardSetMap = map.putIfAbsent(card.cardSet, () => {});
      cardSetMap.update(
        card,
        (count) => count + copies,
        ifAbsent: () => copies,
      );
    }
  }
  return Collection._(map);
}