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

View file

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

View file

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

View file

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