loop to streams

This commit is contained in:
igoudt 2017-06-06 09:31:41 +02:00
parent d42b2a6d37
commit 0cb7d8aab6
4 changed files with 67 additions and 90 deletions

View file

@ -38,6 +38,7 @@ import mage.util.ThreadLocalStringBuilder;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* @param <T> * @param <T>
@ -117,82 +118,72 @@ public class AbilitiesImpl<T extends Ability> extends ArrayList<T> implements Ab
@Override @Override
public Abilities<ActivatedAbility> getActivatedAbilities(Zone zone) { public Abilities<ActivatedAbility> getActivatedAbilities(Zone zone) {
Abilities<ActivatedAbility> zonedAbilities = new AbilitiesImpl<>(); return stream()
for (T ability : this) { .filter(ability -> ability instanceof ActivatedAbility)
if (ability instanceof ActivatedAbility && ability.getZone().match(zone)) { .filter(ability -> ability.getZone().match(zone))
zonedAbilities.add((ActivatedAbility) ability); .map(ability -> (ActivatedAbility) ability)
} .collect(Collectors.toCollection(AbilitiesImpl::new));
}
return zonedAbilities;
} }
@Override @Override
public Abilities<ActivatedAbility> getPlayableAbilities(Zone zone) { public Abilities<ActivatedAbility> getPlayableAbilities(Zone zone) {
Abilities<ActivatedAbility> zonedAbilities = new AbilitiesImpl<>(); return stream()
for (T ability : this) { .filter(ability -> (ability instanceof ActivatedAbility))
if ((ability instanceof ActivatedAbility || (ability instanceof PlayLandAbility)) .filter(ability -> ability.getZone().match(zone))
&& ability.getZone().match(zone)) { .map(ability -> (ActivatedAbility) ability)
zonedAbilities.add((ActivatedAbility) ability); .collect(Collectors.toCollection(AbilitiesImpl::new));
}
}
return zonedAbilities;
} }
@Override @Override
public Abilities<ActivatedManaAbilityImpl> getActivatedManaAbilities(Zone zone) { public Abilities<ActivatedManaAbilityImpl> getActivatedManaAbilities(Zone zone) {
Abilities<ActivatedManaAbilityImpl> abilities = new AbilitiesImpl<>(); return stream()
for (T ability : this) { .filter(ability -> ability instanceof ActivatedManaAbilityImpl)
if (ability instanceof ActivatedManaAbilityImpl && ability.getZone().match(zone)) { .filter(ability -> ability.getZone().match(zone))
abilities.add((ActivatedManaAbilityImpl) ability); .map(ability -> (ActivatedManaAbilityImpl) ability)
} .collect(Collectors.toCollection(AbilitiesImpl::new));
}
return abilities;
} }
@Override @Override
public Abilities<ActivatedManaAbilityImpl> getAvailableActivatedManaAbilities(Zone zone, Game game) { public Abilities<ActivatedManaAbilityImpl> getAvailableActivatedManaAbilities(Zone zone, Game game) {
Abilities<ActivatedManaAbilityImpl> abilities = new AbilitiesImpl<>(); return stream()
for (T ability : this) { .filter(ability -> ability instanceof ActivatedManaAbilityImpl)
if (ability instanceof ActivatedManaAbilityImpl && ability.getZone().match(zone)) { .filter(ability -> ability.getZone().match(zone))
if ((((ActivatedManaAbilityImpl) ability).canActivate(ability.getControllerId(), game))) { .filter(ability -> (((ActivatedManaAbilityImpl) ability).canActivate(ability.getControllerId(), game)))
abilities.add((ActivatedManaAbilityImpl) ability); .map(ability -> (ActivatedManaAbilityImpl) ability)
} .collect(Collectors.toCollection(AbilitiesImpl::new));
}
}
return abilities;
} }
@Override @Override
public Abilities<Ability> getManaAbilities(Zone zone) { public Abilities<Ability> getManaAbilities(Zone zone) {
Abilities<Ability> abilities = new AbilitiesImpl<>(); return stream()
for (T ability : this) { .filter(ability -> ability.getAbilityType() == AbilityType.MANA)
if (ability.getAbilityType() == AbilityType.MANA && ability.getZone().match(zone)) { .filter(ability -> ability.getZone().match(zone))
abilities.add(ability); .collect(Collectors.toCollection(AbilitiesImpl::new));
}
}
return abilities;
} }
@Override @Override
public Abilities<EvasionAbility> getEvasionAbilities() { public Abilities<EvasionAbility> getEvasionAbilities() {
Abilities<EvasionAbility> abilities = new AbilitiesImpl<>(); return stream()
for (T ability : this) { .filter(ability -> ability instanceof EvasionAbility)
if (ability instanceof EvasionAbility) { .map(ability -> (EvasionAbility) ability)
abilities.add((EvasionAbility) ability); .collect(Collectors.toCollection(AbilitiesImpl::new));
}
}
return abilities;
} }
@Override @Override
public Abilities<StaticAbility> getStaticAbilities(Zone zone) { public Abilities<StaticAbility> getStaticAbilities(Zone zone) {
Abilities<StaticAbility> zonedAbilities = new AbilitiesImpl<>(); return stream()
for (T ability : this) { .filter(ability -> ability instanceof StaticAbility)
if (ability instanceof StaticAbility && ability.getZone().match(zone)) { .filter(ability -> ability.getZone().match(zone))
zonedAbilities.add((StaticAbility) ability); .map(ability -> (StaticAbility) ability)
} .collect(Collectors.toCollection(AbilitiesImpl::new));
}
return zonedAbilities;
} }
@Override @Override
@ -213,13 +204,11 @@ public class AbilitiesImpl<T extends Ability> extends ArrayList<T> implements Ab
@Override @Override
public Abilities<ProtectionAbility> getProtectionAbilities() { public Abilities<ProtectionAbility> getProtectionAbilities() {
Abilities<ProtectionAbility> abilities = new AbilitiesImpl<>(); return stream()
for (T ability : this) { .filter(ability -> ability instanceof ProtectionAbility)
if (ability instanceof ProtectionAbility) { .map(ability -> (ProtectionAbility) ability)
abilities.add((ProtectionAbility) ability); .collect(Collectors.toCollection(AbilitiesImpl::new));
}
}
return abilities;
} }
@Override @Override
@ -305,12 +294,8 @@ public class AbilitiesImpl<T extends Ability> extends ArrayList<T> implements Ab
@Override @Override
public int getOutcomeTotal() { public int getOutcomeTotal() {
int total = 0; return stream().mapToInt(ability -> ability.getEffects().getOutcomeTotal()).sum();
for (T ability : this) { }
total += ability.getEffects().getOutcomeTotal();
}
return total;
}
@Override @Override
public String getValue() { public String getValue() {

View file

@ -27,11 +27,12 @@
*/ */
package mage.abilities.condition; package mage.abilities.condition;
import java.util.ArrayList;
import java.util.Arrays;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.game.Game; import mage.game.Game;
import java.util.ArrayList;
import java.util.Arrays;
/** /**
* Combines conditions to one compound conditon, all single conditons must be * Combines conditions to one compound conditon, all single conditons must be
* true to return true for the compound condtion. * true to return true for the compound condtion.
@ -55,12 +56,7 @@ public class CompoundCondition implements Condition {
@Override @Override
public boolean apply(Game game, Ability source) { public boolean apply(Game game, Ability source) {
for (Condition condition : conditions) { return conditions.stream().allMatch(condition -> condition.apply(game, source));
if (!condition.apply(game, source)) {
return false;
}
}
return true;
} }
@Override @Override

View file

@ -27,13 +27,15 @@
*/ */
package mage.choices; package mage.choices;
import java.util.ArrayList;
import java.util.List;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.constants.Outcome; import mage.constants.Outcome;
import mage.game.Game; import mage.game.Game;
import mage.players.Player; import mage.players.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/** /**
* *
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
@ -57,13 +59,9 @@ public class Choices extends ArrayList<Choice> {
} }
public List<Choice> getUnchosen() { public List<Choice> getUnchosen() {
List<Choice> unchosen = new ArrayList<>(); return stream()
for (Choice choice : this) { .filter(choice -> !choice.isChosen())
if (!choice.isChosen()) { .collect(Collectors.toList());
unchosen.add(choice);
}
}
return unchosen;
} }
public void clearChosen() { public void clearChosen() {

View file

@ -27,27 +27,24 @@
*/ */
package mage.util; package mage.util;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import mage.MageObject; import mage.MageObject;
import mage.Mana; import mage.Mana;
import mage.ObjectColor;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.ActivatedAbility; import mage.abilities.ActivatedAbility;
import mage.abilities.SpellAbility; import mage.abilities.SpellAbility;
import mage.abilities.costs.VariableCost; import mage.abilities.costs.VariableCost;
import mage.abilities.costs.mana.*; import mage.abilities.costs.mana.*;
import mage.cards.Card; import mage.cards.Card;
import mage.filter.FilterMana;
import mage.game.Game; import mage.game.Game;
import mage.game.permanent.Permanent; import mage.game.permanent.Permanent;
import mage.game.permanent.token.Token; import mage.game.permanent.token.Token;
import mage.game.stack.Spell;
import mage.util.functions.CopyTokenFunction; import mage.util.functions.CopyTokenFunction;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
/** /**
* @author nantuko * @author nantuko
*/ */
@ -396,6 +393,7 @@ public final class CardUtil {
} }
public static boolean checkNumeric(String s) { public static boolean checkNumeric(String s) {
for (int i = 0; i < s.length(); i++) { for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) { if (!Character.isDigit(s.charAt(i))) {
return false; return false;