From d66c1729528c24e303206ea0405fffd057d09362 Mon Sep 17 00:00:00 2001
From: North <robyter@gmail>
Date: Sun, 15 Jul 2012 16:58:01 +0300
Subject: [PATCH] [filters] Refactored FilterAbility to use Predicates
 FilterAbility isn't used anywhere. I only noticed after I finished rewriting
 it. I'll keep the class in the repository just in case someone would need it.

---
 .../src/mage/player/ai/ComputerPlayer7.java   |  16 ---
 .../src/mage/player/ai/ComputerPlayer3.java   |  16 ---
 Mage/src/mage/filter/FilterAbility.java       | 117 +++++++-----------
 3 files changed, 44 insertions(+), 105 deletions(-)

diff --git a/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer7.java b/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer7.java
index ecb4f9571e..7b6c02436f 100644
--- a/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer7.java
+++ b/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer7.java
@@ -29,11 +29,8 @@
 package mage.player.ai;
 
 import mage.Constants;
-import mage.Constants.AbilityType;
 import mage.Constants.RangeOfInfluence;
-import mage.Constants.Zone;
 import mage.abilities.Ability;
-import mage.filter.FilterAbility;
 import mage.game.Game;
 import mage.game.combat.Combat;
 import mage.game.combat.CombatGroup;
@@ -54,21 +51,8 @@ public class ComputerPlayer7 extends ComputerPlayer6 implements Player {
 
     private static final transient Logger logger = Logger.getLogger(ComputerPlayer7.class);
 
-    private static FilterAbility filterLand = new FilterAbility();
-    private static FilterAbility filterNotLand = new FilterAbility();
-
     private boolean allowBadMoves;
 
-    static {
-        filterLand.getTypes().add(AbilityType.PLAY_LAND);
-        filterLand.setZone(Zone.HAND);
-
-        filterNotLand.getTypes().add(AbilityType.PLAY_LAND);
-        filterNotLand.setZone(Zone.HAND);
-        filterNotLand.setNotFilter(true);
-
-    }
-
     public ComputerPlayer7(String name, RangeOfInfluence range, int skill) {
         super(name, range, skill);
     }
diff --git a/Mage.Server.Plugins/Mage.Player.AIMinimax/src/mage/player/ai/ComputerPlayer3.java b/Mage.Server.Plugins/Mage.Player.AIMinimax/src/mage/player/ai/ComputerPlayer3.java
index 2f36d16879..c151a8d4f9 100644
--- a/Mage.Server.Plugins/Mage.Player.AIMinimax/src/mage/player/ai/ComputerPlayer3.java
+++ b/Mage.Server.Plugins/Mage.Player.AIMinimax/src/mage/player/ai/ComputerPlayer3.java
@@ -28,12 +28,9 @@
 
 package mage.player.ai;
 
-import mage.Constants.AbilityType;
 import mage.Constants.PhaseStep;
 import mage.Constants.RangeOfInfluence;
-import mage.Constants.Zone;
 import mage.abilities.Ability;
-import mage.filter.FilterAbility;
 import mage.game.Game;
 import mage.game.combat.Combat;
 import mage.game.combat.CombatGroup;
@@ -54,19 +51,6 @@ public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
 
     private static final transient Logger logger = Logger.getLogger(ComputerPlayer3.class);
 
-    private static FilterAbility filterLand = new FilterAbility();
-    private static FilterAbility filterNotLand = new FilterAbility();
-
-    static {
-        filterLand.getTypes().add(AbilityType.PLAY_LAND);
-        filterLand.setZone(Zone.HAND);
-
-        filterNotLand.getTypes().add(AbilityType.PLAY_LAND);
-        filterNotLand.setZone(Zone.HAND);
-        filterNotLand.setNotFilter(true);
-
-    }
-
     public ComputerPlayer3(String name, RangeOfInfluence range, int skill) {
         super(name, range, skill);
     }
diff --git a/Mage/src/mage/filter/FilterAbility.java b/Mage/src/mage/filter/FilterAbility.java
index f54f0013cd..13c763a013 100644
--- a/Mage/src/mage/filter/FilterAbility.java
+++ b/Mage/src/mage/filter/FilterAbility.java
@@ -25,105 +25,76 @@
  *  authors and should not be interpreted as representing official policies, either expressed
  *  or implied, of BetaSteward_at_googlemail.com.
  */
-
 package mage.filter;
 
 import mage.Constants.AbilityType;
-import mage.Constants.Outcome;
 import mage.Constants.Zone;
 import mage.abilities.Ability;
+import mage.filter.predicate.Predicate;
 import mage.game.Game;
 
-import java.util.ArrayList;
-import java.util.List;
-
 /**
  *
- * @author BetaSteward_at_googlemail.com
+ * @author North
  */
-public class FilterAbility<T extends Ability> extends FilterImpl<T> {
-
-    protected static ListComparer<Outcome> compOutcome = new ListComparer<Outcome>();
-
-    protected List<Outcome> outcomes = new ArrayList<Outcome>();
-    protected ComparisonScope scopeOutcome = ComparisonScope.All;
-    protected boolean notOutcome;
-    protected List<AbilityType> types = new ArrayList<AbilityType>();
-    protected boolean notType;
-    protected Zone zone;
-    protected boolean notZone;
+public class FilterAbility extends FilterImpl<Ability> {
 
     public FilterAbility() {
         super("");
     }
 
-    public FilterAbility(FilterAbility<T> filter) {
+    public FilterAbility(FilterAbility filter) {
         super(filter);
-        for (Outcome outcome: filter.outcomes) {
-            this.outcomes.add(outcome);
-        }
-        this.scopeOutcome = filter.scopeOutcome;
-        this.notOutcome = filter.notOutcome;
-        for (AbilityType aType: filter.types) {
-            this.types.add(aType);
-        }
-        this.notType = filter.notType;
-        this.zone = filter.zone;
-        this.notZone = filter.notZone;
     }
 
     @Override
-    public boolean match(T object, Game game) {
+    public FilterAbility copy() {
+        return new FilterAbility(this);
+    }
 
-        if (zone != null) {
-            if (object.getZone().match(zone) == notZone)
-                return notFilter;
+    public static Predicate<Ability> zone(Zone zone) {
+        return new AbilityZonePredicate(zone);
+    }
+
+    public static Predicate<Ability> type(AbilityType type) {
+        return new AbilityTypePredicate(type);
+    }
+
+    private static final class AbilityZonePredicate implements Predicate<Ability> {
+
+        private Zone zone;
+
+        public AbilityZonePredicate(Zone zone) {
+            this.zone = zone;
         }
 
-        if (outcomes.size() > 0) {
-            if (!compOutcome.compare(outcomes, object.getEffects().getOutcomes(), scopeOutcome, notOutcome))
-                return notFilter;
+        @Override
+        public boolean apply(Ability input, Game game) {
+            return input.getZone().match(zone);
         }
 
-        if (types.size() > 0) {
-            if (types.contains(object.getAbilityType()) == notType)
-                return notFilter;
+        @Override
+        public String toString() {
+            return "Zone(" + zone.toString() + ")";
+        }
+    }
+
+    private static final class AbilityTypePredicate implements Predicate<Ability> {
+
+        private AbilityType type;
+
+        public AbilityTypePredicate(AbilityType type) {
+            this.type = type;
         }
 
-        return !notFilter;
-    }
+        @Override
+        public boolean apply(Ability input, Game game) {
+            return input.getAbilityType().equals(type);
+        }
 
-    public List<Outcome> getOutcomes() {
-        return this.outcomes;
+        @Override
+        public String toString() {
+            return "AbilityType(" + type + ')';
+        }
     }
-
-    public void setScopeOutcome(ComparisonScope scopeOutcome) {
-        this.scopeOutcome = scopeOutcome;
-    }
-
-    public void setNotOutcome(boolean notOutcome) {
-        this.notOutcome = notOutcome;
-    }
-
-    public List<AbilityType> getTypes() {
-        return types;
-    }
-
-    public void setNotType(boolean notType) {
-        this.notType = notType;
-    }
-
-    public void setZone(Zone zone) {
-        this.zone = zone;
-    }
-
-    public void setNotZone(boolean notZone) {
-        this.notZone = notZone;
-    }
-
-    @Override
-    public FilterAbility<T> copy() {
-        return new FilterAbility<T>(this);
-    }
-
 }