replace loops in triggeredabilities to lambdas
@@ -30,7 +30,6 @@ package mage.abilities;
import mage.MageObject;
import mage.constants.Zone;
-import mage.designations.Designation;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.events.NumberOfTriggersEvent;
@@ -41,9 +40,8 @@ import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
- *
* @author BetaSteward_at_googlemail.com
- *
+ * <p>
* This class uses ConcurrentHashMap to avoid ConcurrentModificationExceptions.
* See ticket https://github.com/magefree/mage/issues/966 and
* https://github.com/magefree/mage/issues/473
@@ -163,16 +161,9 @@ public class TriggeredAbilities extends ConcurrentHashMap<String, TriggeredAbili
return key;
}
+
public void removeAbilitiesOfSource(UUID sourceId) {
- List<String> keysToRemove = new ArrayList<>();
- for (String key : this.keySet()) {
- if (key.endsWith(sourceId.toString())) {
- keysToRemove.add(key);
- }
- }
- for (String key : keysToRemove) {
- remove(key);
- }
+ keySet().removeIf(key -> key.endsWith(sourceId.toString()));
}
public void removeAllGainedAbilities() {
@@ -182,19 +173,10 @@ public class TriggeredAbilities extends ConcurrentHashMap<String, TriggeredAbili
public void removeAbilitiesOfNonExistingSources(Game game) {
// e.g. Token that had triggered abilities
- List<String> keysToRemove = new ArrayList<>();
- Abilities:
- for (Entry<String, TriggeredAbility> entry : this.entrySet()) {
- if (game.getObject(entry.getValue().getSourceId()) == null) {
- for (Designation designation : game.getState().getDesignations()) {
- if (designation.getId().equals(entry.getValue().getSourceId())) {
- continue Abilities;
- }
- }
- keysToRemove.add(entry.getKey());
- }
- }
- this.keySet().removeAll(keysToRemove);
+
+ entrySet().removeIf(entry -> game.getObject(entry.getValue().getSourceId()) == null
+ && game.getState().getDesignations().stream().noneMatch(designation -> designation.getId().equals(entry.getValue().getSourceId())));
+
}
public TriggeredAbilities copy() {
