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

@ -87,9 +87,7 @@ public class CardState implements Serializable {
abilities = new AbilitiesImpl<>();
}
abilities.add(ability);
for (Ability sub : ability.getSubAbilities()) {
abilities.add(sub);
}
abilities.addAll(ability.getSubAbilities());
}
public void clearAbilities() {

View file

@ -79,10 +79,7 @@ public class Exile implements Serializable, Copyable<Exile> {
}
private ExileZone createZone(UUID id, String name, boolean hidden) {
if (!exileZones.containsKey(id)) {
ExileZone exile = new ExileZone(id, name, hidden);
exileZones.put(id, exile);
}
exileZones.putIfAbsent(id, new ExileZone(id, name, hidden));
return exileZones.get(id);
}

View file

@ -1092,9 +1092,7 @@ public class GameState implements Serializable, Copyable<GameState> {
}
public CardState getCardState(UUID cardId) {
if (!cardState.containsKey(cardId)) {
cardState.put(cardId, new CardState());
}
cardState.putIfAbsent(cardId, new CardState());
return cardState.get(cardId);
}

View file

@ -68,9 +68,7 @@ public class Revealed extends HashMap<String, Cards> implements Serializable, Co
}
public Cards createRevealed(String name) {
if (!this.containsKey(name)) {
this.put(name, new CardsImpl());
}
putIfAbsent(name, new CardsImpl());
return this.get(name);
}

View file

@ -28,6 +28,7 @@
package mage.game;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

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());
}
}

View file

@ -206,19 +206,19 @@ public class TournamentPlayer {
}
public void setStateAtTournamentEnd() {
if (this.getState().equals(TournamentPlayerState.DRAFTING)
|| this.getState().equals(TournamentPlayerState.CONSTRUCTING)
|| this.getState().equals(TournamentPlayerState.DUELING)
|| this.getState().equals(TournamentPlayerState.SIDEBOARDING)
|| this.getState().equals(TournamentPlayerState.WAITING)) {
if (this.getState() == TournamentPlayerState.DRAFTING
|| this.getState() == TournamentPlayerState.CONSTRUCTING
|| this.getState() == TournamentPlayerState.DUELING
|| this.getState() == TournamentPlayerState.SIDEBOARDING
|| this.getState() == TournamentPlayerState.WAITING) {
this.setState(TournamentPlayerState.FINISHED);
}
}
public boolean isInTournament() {
return !this.getState().equals(TournamentPlayerState.CANCELED)
&& !this.getState().equals(TournamentPlayerState.ELIMINATED)
&& !this.getState().equals(TournamentPlayerState.FINISHED);
return !(this.getState() == TournamentPlayerState.CANCELED)
&& !(this.getState() == TournamentPlayerState.ELIMINATED)
&& !(this.getState() == TournamentPlayerState.FINISHED);
}
public TournamentPlayer getReplacedTournamentPlayer() {