rewrote enum comparisons, iterator to removeIf, added some stream and filters

This commit is contained in:
ingmargoudt 2017-02-11 22:37:00 +01:00
parent 05e5ca3c78
commit 3a152ab3d6
41 changed files with 178 additions and 239 deletions

View file

@ -27,23 +27,18 @@
*/
package mage.game.permanent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import mage.abilities.keyword.PhasingAbility;
import mage.constants.CardType;
import mage.constants.RangeOfInfluence;
import mage.filter.FilterPermanent;
import mage.game.Game;
import java.io.Serializable;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class Battlefield implements Serializable {
@ -76,7 +71,7 @@ public class Battlefield implements Serializable {
/**
* Returns a count of all {@link Permanent} that match the filter and are
* controlled by controllerId.
*
* <p>
* Some filter predicates do not work here (e.g. AnotherPredicate() because
* filter.match() is called without controllerId. To use this predicates you
* can use count() instead of countAll()
@ -87,13 +82,13 @@ public class Battlefield implements Serializable {
* @return count
*/
public int countAll(FilterPermanent filter, UUID controllerId, Game game) {
int count = 0;
for (Permanent permanent : field.values()) {
if (permanent.getControllerId().equals(controllerId) && filter.match(permanent, game) && permanent.isPhasedIn()) {
count++;
}
}
return count;
return (int) field.values()
.stream()
.filter(permanent -> permanent.getControllerId().equals(controllerId)
&& filter.match(permanent, game)
&& permanent.isPhasedIn())
.count();
}
/**
@ -101,27 +96,27 @@ public class Battlefield implements Serializable {
* influence of the specified player id and that match the supplied filter.
*
* @param filter
* @param sourceId - sourceId of the MageObject the calling effect/ability
* belongs to
* @param sourceId - sourceId of the MageObject the calling effect/ability
* belongs to
* @param sourcePlayerId
* @param game
* @return count
*/
public int count(FilterPermanent filter, UUID sourceId, UUID sourcePlayerId, Game game) {
int count = 0;
int count;
if (game.getRangeOfInfluence() == RangeOfInfluence.ALL) {
for (Permanent permanent : field.values()) {
if (filter.match(permanent, sourceId, sourcePlayerId, game) && permanent.isPhasedIn()) {
count++;
}
}
count = (int) field.values()
.stream()
.filter(permanent -> filter.match(permanent, sourceId, sourcePlayerId, game)
&& permanent.isPhasedIn())
.count();
} else {
Set<UUID> range = game.getPlayer(sourcePlayerId).getInRange();
for (Permanent permanent : field.values()) {
if (range.contains(permanent.getControllerId()) && filter.match(permanent, sourceId, sourcePlayerId, game) && permanent.isPhasedIn()) {
count++;
}
}
count = (int) field.values()
.stream()
.filter(permanent -> range.contains(permanent.getControllerId())
&& filter.match(permanent, sourceId, sourcePlayerId, game)
&& permanent.isPhasedIn()).count();
}
return count;
}
@ -136,16 +131,11 @@ public class Battlefield implements Serializable {
* @return boolean
*/
public boolean contains(FilterPermanent filter, int num, Game game) {
int count = 0;
for (Permanent permanent : field.values()) {
if (filter.match(permanent, game) && permanent.isPhasedIn()) {
count++;
if (num == count) {
return true;
}
}
}
return false;
return field.values()
.stream()
.filter(permanent -> filter.match(permanent, game)
&& permanent.isPhasedIn()).count() == num;
}
/**
@ -186,14 +176,10 @@ public class Battlefield implements Serializable {
public boolean contains(FilterPermanent filter, UUID sourcePlayerId, Game game, int num) {
int count = 0;
if (game.getRangeOfInfluence() == RangeOfInfluence.ALL) {
for (Permanent permanent : field.values()) {
if (filter.match(permanent, null, sourcePlayerId, game) && permanent.isPhasedIn()) {
count++;
if (num == count) {
return true;
}
}
}
return field.values().stream()
.filter(permanent -> filter.match(permanent, null, sourcePlayerId, game)
&& permanent.isPhasedIn()).count() == num;
} else {
Set<UUID> range = game.getPlayer(sourcePlayerId).getInRange();
for (Permanent permanent : field.values()) {
@ -245,13 +231,10 @@ public class Battlefield implements Serializable {
}
public List<Permanent> getAllActivePermanents() {
List<Permanent> active = new ArrayList<>();
for (Permanent perm : field.values()) {
if (perm.isPhasedIn()) {
active.add(perm);
}
}
return active;
return field.values()
.stream()
.filter(Permanent::isPhasedIn)
.collect(Collectors.toList());
}
/**
@ -263,13 +246,11 @@ public class Battlefield implements Serializable {
* @see Permanent
*/
public List<Permanent> getAllActivePermanents(UUID controllerId) {
List<Permanent> active = new ArrayList<>();
for (Permanent perm : field.values()) {
if (perm.isPhasedIn() && perm.getControllerId().equals(controllerId)) {
active.add(perm);
}
}
return active;
return field.values()
.stream()
.filter(perm -> perm.isPhasedIn()
&& perm.getControllerId().equals(controllerId))
.collect(Collectors.toList());
}
/**
@ -281,20 +262,13 @@ public class Battlefield implements Serializable {
* @see Permanent
*/
public List<Permanent> getAllActivePermanents(CardType type) {
List<Permanent> active = new ArrayList<>();
for (Permanent perm : field.values()) {
if (perm.isPhasedIn() && perm.getCardType().contains(type)) {
active.add(perm);
}
}
return active;
return field.values().stream().filter(perm -> perm.isPhasedIn() && perm.getCardType().contains(type)).collect(Collectors.toList());
}
/**
* Returns all {@link Permanent} on the battlefield that match the supplied
* filter. This method ignores the range of influence.
*
*
* @param filter
* @param game
* @return a list of {@link Permanent}
@ -387,14 +361,13 @@ public class Battlefield implements Serializable {
if (game.getRangeOfInfluence() == RangeOfInfluence.ALL) {
return getAllActivePermanents();
} else {
List<Permanent> active = new ArrayList<>();
Set<UUID> range = game.getPlayer(sourcePlayerId).getInRange();
for (Permanent perm : field.values()) {
if (perm.isPhasedIn() && range.contains(perm.getControllerId())) {
active.add(perm);
}
}
return active;
return field.values()
.stream()
.filter(perm -> perm.isPhasedIn()
&& range.contains(perm.getControllerId()))
.collect(Collectors.toList());
}
}