From a167122459c70e713e1f9470e83d7f61c2f1c42f Mon Sep 17 00:00:00 2001 From: igoudt Date: Sat, 8 Jul 2017 10:57:36 +0200 Subject: [PATCH] loop to streams --- .../main/java/mage/server/ChatSession.java | 4 +-- .../java/mage/server/game/GameController.java | 18 +++--------- .../condition/common/DashedCondition.java | 11 +++----- .../condition/common/EvokedCondition.java | 10 ++----- .../mage/abilities/costs/mana/ManaCosts.java | 16 +++++------ .../common/DiscardCostCardConvertedMana.java | 6 +--- .../common/SacrificeCostCreaturesPower.java | 8 ++---- .../SacrificeCostCreaturesToughness.java | 7 +---- .../abilities/keyword/ConspireAbility.java | 11 ++++---- .../main/java/mage/game/draft/DraftImpl.java | 28 ++++++------------- 10 files changed, 38 insertions(+), 81 deletions(-) diff --git a/Mage.Server/src/main/java/mage/server/ChatSession.java b/Mage.Server/src/main/java/mage/server/ChatSession.java index f7a9c969fb..9dd362060f 100644 --- a/Mage.Server/src/main/java/mage/server/ChatSession.java +++ b/Mage.Server/src/main/java/mage/server/ChatSession.java @@ -129,9 +129,7 @@ public class ChatSession { clientsToRemove.add(userId); } } - for (UUID userIdToRemove : clientsToRemove) { - clients.remove(userIdToRemove); - } + clients.keySet().removeAll(clientsToRemove); } } diff --git a/Mage.Server/src/main/java/mage/server/game/GameController.java b/Mage.Server/src/main/java/mage/server/game/GameController.java index fc4359f6c3..0046d625c0 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameController.java +++ b/Mage.Server/src/main/java/mage/server/game/GameController.java @@ -46,6 +46,7 @@ import mage.game.Table; import mage.game.events.Listener; import mage.game.events.PlayerQueryEvent; import mage.game.events.TableEvent; +import mage.game.match.MatchPlayer; import mage.game.permanent.Permanent; import mage.interfaces.Action; import mage.players.Player; @@ -65,7 +66,6 @@ import java.util.*; import java.util.Map.Entry; import java.util.concurrent.*; import java.util.zip.GZIPOutputStream; -import mage.game.match.MatchPlayer; /** * @author BetaSteward_at_googlemail.com @@ -108,12 +108,7 @@ public class GameController implements GameCallback { this.tableId = tableId; this.choosingPlayerId = choosingPlayerId; this.gameOptions = gameOptions; - for (Player player : game.getPlayers().values()) { - if (!player.isHuman()) { - useTimeout = false; // no timeout for AI players because of beeing idle - break; - } - } + useTimeout = game.getPlayers().values().stream().allMatch(Player::isHuman); init(); } @@ -832,13 +827,8 @@ public class GameController implements GameCallback { } final String message = new StringBuilder(game.getStep().getType().toString()).append(" - Waiting for ").append(controller.getName()).toString(); for (final Entry entry : gameSessions.entrySet()) { - boolean skip = false; - for (UUID uuid : players) { - if (entry.getKey().equals(uuid)) { - skip = true; - break; - } - } + boolean skip = players.stream().anyMatch(playerId -> entry.getKey().equals(playerId)); + if (!skip) { entry.getValue().inform(message); } diff --git a/Mage/src/main/java/mage/abilities/condition/common/DashedCondition.java b/Mage/src/main/java/mage/abilities/condition/common/DashedCondition.java index bbf1157f6d..072a7b1a89 100644 --- a/Mage/src/main/java/mage/abilities/condition/common/DashedCondition.java +++ b/Mage/src/main/java/mage/abilities/condition/common/DashedCondition.java @@ -45,13 +45,10 @@ public enum DashedCondition implements Condition { public boolean apply(Game game, Ability source) { Card card = game.getCard(source.getSourceId()); if (card != null) { - for (Ability ability : card.getAbilities()) { - if (ability instanceof DashAbility) { - if (((DashAbility) ability).isActivated(source, game)) { - return true; - } - } - } + return card.getAbilities().stream() + .filter(a -> a instanceof DashAbility) + .anyMatch(d -> ((DashAbility)d).isActivated(source, game)); + } return false; } diff --git a/Mage/src/main/java/mage/abilities/condition/common/EvokedCondition.java b/Mage/src/main/java/mage/abilities/condition/common/EvokedCondition.java index dc1da194f0..8c455bde6c 100644 --- a/Mage/src/main/java/mage/abilities/condition/common/EvokedCondition.java +++ b/Mage/src/main/java/mage/abilities/condition/common/EvokedCondition.java @@ -48,13 +48,9 @@ public enum EvokedCondition implements Condition { public boolean apply(Game game, Ability source) { Card card = game.getCard(source.getSourceId()); if (card != null) { - for (Ability ability: card.getAbilities()) { - if (ability instanceof EvokeAbility) { - if(((EvokeAbility) ability).isActivated(source, game)) { - return true; - } - } - } + return card.getAbilities().stream() + .filter(ab -> ab instanceof EvokeAbility) + .anyMatch(evoke -> ((EvokeAbility) evoke).isActivated(source, game)); } return false; } diff --git a/Mage/src/main/java/mage/abilities/costs/mana/ManaCosts.java b/Mage/src/main/java/mage/abilities/costs/mana/ManaCosts.java index 4a129a6cc1..78b8299d05 100644 --- a/Mage/src/main/java/mage/abilities/costs/mana/ManaCosts.java +++ b/Mage/src/main/java/mage/abilities/costs/mana/ManaCosts.java @@ -27,13 +27,15 @@ */ package mage.abilities.costs.mana; -import java.util.List; -import java.util.UUID; import mage.Mana; import mage.abilities.Ability; import mage.abilities.costs.VariableCost; import mage.game.Game; +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + /** * * @author BetaSteward_at_googlemail.com @@ -63,14 +65,10 @@ public interface ManaCosts extends List, ManaCost { static ManaCosts removeVariableManaCost(ManaCosts m) { - ManaCosts manaCosts = new ManaCostsImpl<>(); - for(ManaCost manaCost : m){ - if(!(manaCost instanceof VariableManaCost)){ - manaCosts.add(manaCost); + return m.stream() + .filter(mc -> !(mc instanceof VariableManaCost)) + .collect(Collectors.toCollection(ManaCostsImpl::new)); - } - } - return manaCosts; } } diff --git a/Mage/src/main/java/mage/abilities/dynamicvalue/common/DiscardCostCardConvertedMana.java b/Mage/src/main/java/mage/abilities/dynamicvalue/common/DiscardCostCardConvertedMana.java index 0b51372789..145cf7ddaf 100644 --- a/Mage/src/main/java/mage/abilities/dynamicvalue/common/DiscardCostCardConvertedMana.java +++ b/Mage/src/main/java/mage/abilities/dynamicvalue/common/DiscardCostCardConvertedMana.java @@ -18,11 +18,7 @@ public class DiscardCostCardConvertedMana implements DynamicValue { for (Cost cost : sourceAbility.getCosts()) { if (cost instanceof DiscardTargetCost) { DiscardTargetCost discardCost = (DiscardTargetCost) cost; - int cmc = 0; - for (Card card : discardCost.getCards()) { - cmc += card.getConvertedManaCost(); - } - return cmc; + return discardCost.getCards().stream().mapToInt(Card::getConvertedManaCost).sum(); } } return 0; diff --git a/Mage/src/main/java/mage/abilities/dynamicvalue/common/SacrificeCostCreaturesPower.java b/Mage/src/main/java/mage/abilities/dynamicvalue/common/SacrificeCostCreaturesPower.java index 3c99127868..290a303c88 100644 --- a/Mage/src/main/java/mage/abilities/dynamicvalue/common/SacrificeCostCreaturesPower.java +++ b/Mage/src/main/java/mage/abilities/dynamicvalue/common/SacrificeCostCreaturesPower.java @@ -6,7 +6,6 @@ import mage.abilities.costs.common.SacrificeTargetCost; import mage.abilities.dynamicvalue.DynamicValue; import mage.abilities.effects.Effect; import mage.game.Game; -import mage.game.permanent.Permanent; /** * @author LevelX2 @@ -18,11 +17,8 @@ public class SacrificeCostCreaturesPower implements DynamicValue { for (Cost cost : sourceAbility.getCosts()) { if (cost instanceof SacrificeTargetCost) { SacrificeTargetCost sacrificeCost = (SacrificeTargetCost) cost; - int powerSum = 0; - for (Permanent permanent : sacrificeCost.getPermanents()) { - powerSum += permanent.getPower().getValue(); - } - return powerSum; + return sacrificeCost.getPermanents() + .stream().mapToInt(p -> p.getPower().getValue()).sum(); } } return 0; diff --git a/Mage/src/main/java/mage/abilities/dynamicvalue/common/SacrificeCostCreaturesToughness.java b/Mage/src/main/java/mage/abilities/dynamicvalue/common/SacrificeCostCreaturesToughness.java index 9864669877..bfc03a261c 100644 --- a/Mage/src/main/java/mage/abilities/dynamicvalue/common/SacrificeCostCreaturesToughness.java +++ b/Mage/src/main/java/mage/abilities/dynamicvalue/common/SacrificeCostCreaturesToughness.java @@ -6,7 +6,6 @@ import mage.abilities.costs.common.SacrificeTargetCost; import mage.abilities.dynamicvalue.DynamicValue; import mage.abilities.effects.Effect; import mage.game.Game; -import mage.game.permanent.Permanent; /** * @author LevelX2 @@ -18,11 +17,7 @@ public class SacrificeCostCreaturesToughness implements DynamicValue { for (Cost cost : sourceAbility.getCosts()) { if (cost instanceof SacrificeTargetCost) { SacrificeTargetCost sacrificeCost = (SacrificeTargetCost) cost; - int toughnessSum = 0; - for (Permanent permanent : sacrificeCost.getPermanents()) { - toughnessSum += permanent.getToughness().getValue(); - } - return toughnessSum; + return sacrificeCost.getPermanents().stream().mapToInt(p -> p.getToughness().getValue()).sum(); } } return 0; diff --git a/Mage/src/main/java/mage/abilities/keyword/ConspireAbility.java b/Mage/src/main/java/mage/abilities/keyword/ConspireAbility.java index fe9421ac61..ce90e57208 100644 --- a/Mage/src/main/java/mage/abilities/keyword/ConspireAbility.java +++ b/Mage/src/main/java/mage/abilities/keyword/ConspireAbility.java @@ -27,10 +27,6 @@ */ package mage.abilities.keyword; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; -import java.util.UUID; import mage.abilities.Ability; import mage.abilities.SpellAbility; import mage.abilities.StaticAbility; @@ -56,6 +52,11 @@ import mage.game.stack.StackObject; import mage.players.Player; import mage.target.common.TargetControlledPermanent; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.UUID; + /* * 702.77. Conspire * 702.77a Conspire is a keyword that represents two abilities. @@ -165,7 +166,7 @@ public class ConspireAbility extends StaticAbility implements OptionalAdditional if (conspireCost.canPay(ability, getSourceId(), getControllerId(), game) && player.chooseUse(Outcome.Benefit, "Pay " + conspireCost.getText(false) + " ?", ability, game)) { activateConspire(ability, game); - for (Iterator it = ((Costs) conspireCost).iterator(); it.hasNext();) { + for (Iterator it = conspireCost.iterator(); it.hasNext(); ) { Cost cost = (Cost) it.next(); ability.getCosts().add(cost.copy()); } diff --git a/Mage/src/main/java/mage/game/draft/DraftImpl.java b/Mage/src/main/java/mage/game/draft/DraftImpl.java index c76b6fd774..56305b33c4 100644 --- a/Mage/src/main/java/mage/game/draft/DraftImpl.java +++ b/Mage/src/main/java/mage/game/draft/DraftImpl.java @@ -28,20 +28,16 @@ package mage.game.draft; -import java.util.*; - import mage.cards.Card; import mage.cards.ExpansionSet; import mage.game.draft.DraftOptions.TimingOption; -import mage.game.events.Listener; -import mage.game.events.PlayerQueryEvent; -import mage.game.events.PlayerQueryEventSource; -import mage.game.events.TableEvent; +import mage.game.events.*; import mage.game.events.TableEvent.EventType; -import mage.game.events.TableEventSource; import mage.players.Player; import mage.players.PlayerList; +import java.util.*; + /** * * @author BetaSteward_at_googlemail.com @@ -262,22 +258,16 @@ public abstract class DraftImpl implements Draft { if(isAbort()) { return true; } - for (DraftPlayer player: players.values()) { - if (player.isPicking()) { - return false; - } - } - return true; + return players.values() + .stream() + .noneMatch(DraftPlayer::isPicking); + } @Override public boolean allJoined() { - for (DraftPlayer player: this.players.values()) { - if (!player.isJoined()) { - return false; - } - } - return true; + return players.values().stream() + .allMatch(DraftPlayer::isJoined); } @Override