[filters] Replaced ControllerId condition in FilterPermanent with Predicate

This commit is contained in:
North 2012-07-16 21:41:00 +03:00
parent 4563e518a6
commit 30cbdd643d
15 changed files with 131 additions and 66 deletions

View file

@ -40,12 +40,10 @@ import mage.game.permanent.Permanent;
/**
*
* @author BetaSteward_at_googlemail.com
* @author North
*/
public class FilterPermanent extends FilterObject<Permanent> {
protected List<ObjectPlayerPredicate<ObjectPlayer<Permanent>>> extraPredicates = new ArrayList<ObjectPlayerPredicate<ObjectPlayer<Permanent>>>();
protected List<UUID> controllerId = new ArrayList<UUID>();
protected boolean notController;
public FilterPermanent() {
super("permanent");
@ -53,8 +51,6 @@ public class FilterPermanent extends FilterObject<Permanent> {
public FilterPermanent(final FilterPermanent filter) {
super(filter);
this.controllerId = new ArrayList<UUID>(filter.controllerId);
this.notController = filter.notController;
this.extraPredicates = new ArrayList<ObjectPlayerPredicate<ObjectPlayer<Permanent>>>(extraPredicates);
}
@ -62,17 +58,6 @@ public class FilterPermanent extends FilterObject<Permanent> {
super(name);
}
@Override
public boolean match(Permanent permanent, Game game) {
if (!super.match(permanent, game))
return notFilter;
if (controllerId.size() > 0 && controllerId.contains(permanent.getControllerId()) == notController)
return notFilter;
return !notFilter;
}
public boolean match(Permanent permanent, UUID sourceId, UUID playerId, Game game) {
if (!this.match(permanent, game))
return false;
@ -84,14 +69,6 @@ public class FilterPermanent extends FilterObject<Permanent> {
extraPredicates.add(predicate);
}
public List<UUID> getControllerId() {
return controllerId;
}
public void setNotController(boolean notController) {
this.notController = notController;
}
@Override
public FilterPermanent copy() {
return new FilterPermanent(this);

View file

@ -28,15 +28,18 @@
package mage.filter.common;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
import mage.filter.FilterImpl;
import mage.filter.FilterPlayer;
import mage.filter.predicate.Predicate;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
import java.util.Set;
import java.util.UUID;
/**
*
* @author BetaSteward_at_googlemail.com
@ -48,8 +51,12 @@ public class FilterPlaneswalkerOrPlayer extends FilterImpl<Object> {
public FilterPlaneswalkerOrPlayer(Set<UUID> defenders) {
super("planeswalker or player");
ArrayList<Predicate<Permanent>> permanentPredicates = new ArrayList<Predicate<Permanent>>();
for (UUID defenderId : defenders) {
permanentPredicates.add(new ControllerIdPredicate(defenderId));
}
planeswalkerFilter = new FilterPlaneswalkerPermanent();
planeswalkerFilter.getControllerId().addAll(defenders);
planeswalkerFilter.add(Predicates.or(permanentPredicates));
playerFilter = new FilterPlayer();
playerFilter.getPlayerId().addAll(defenders);
}

View file

@ -0,0 +1,56 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.filter.predicate.permanent;
import java.util.UUID;
import mage.filter.predicate.Predicate;
import mage.game.Game;
import mage.game.permanent.Permanent;
/**
*
* @author North
*/
public class ControllerIdPredicate implements Predicate<Permanent> {
private final UUID controllerId;
public ControllerIdPredicate(UUID controllerId) {
this.controllerId = controllerId;
}
@Override
public boolean apply(Permanent input, Game game) {
return controllerId.equals(input.getControllerId());
}
@Override
public String toString() {
return "ControllerId(" + controllerId + ')';
}
}