Guava migration - Remove Functions dependency

This commit is contained in:
Jetz
2024-09-02 12:29:50 -04:00
parent bb520b1a60
commit fb16a46389
4 changed files with 37 additions and 15 deletions

View File

@@ -1,10 +1,6 @@
package forge.util; package forge.util;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import com.google.common.base.Function; import com.google.common.base.Function;
@@ -29,6 +25,16 @@ public class Aggregates {
return max; return max;
} }
public static Integer max(final Iterable<Integer> source) {
if (source == null) return null;
int max = Integer.MIN_VALUE;
for (int value : source) {
if (value > max)
max = value;
}
return max;
}
public static final <T> Integer min(final Iterable<T> source, final Function<T, Integer> valueAccessor) { public static final <T> Integer min(final Iterable<T> source, final Function<T, Integer> valueAccessor) {
if (source == null) { return null; } if (source == null) { return null; }
int max = Integer.MAX_VALUE; int max = Integer.MAX_VALUE;
@@ -41,6 +47,16 @@ public class Aggregates {
return max; return max;
} }
public static Integer min(final Iterable<Integer> source) {
if (source == null) return null;
int min = Integer.MAX_VALUE;
for (int value : source) {
if (value < min)
min = value;
}
return min;
}
public static final <T> T itemWithMax(final Iterable<T> source, final Function<T, Integer> valueAccessor) { public static final <T> T itemWithMax(final Iterable<T> source, final Function<T, Integer> valueAccessor) {
if (source == null) { return null; } if (source == null) { return null; }
int max = Integer.MIN_VALUE; int max = Integer.MIN_VALUE;
@@ -83,6 +99,16 @@ public class Aggregates {
return result; return result;
} }
public static int sum(final Iterable<Integer> source) {
int result = 0;
if(source != null) {
for(final Integer value : source) {
result += value;
}
}
return result;
}
public static final <T> T random(final T[] source) { public static final <T> T random(final T[] source) {
if (source == null) { return null; } if (source == null) { return null; }

View File

@@ -1,6 +1,5 @@
package forge.game.ability; package forge.game.ability;
import com.google.common.base.Functions;
import com.google.common.collect.*; import com.google.common.collect.*;
import forge.card.CardStateName; import forge.card.CardStateName;
import forge.card.CardType; import forge.card.CardType;
@@ -674,9 +673,9 @@ public class AbilityUtils {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Iterable<Integer> numbers = (Iterable<Integer>) to; Iterable<Integer> numbers = (Iterable<Integer>) to;
if (calcX[0].endsWith("Max")) { if (calcX[0].endsWith("Max")) {
count = Aggregates.max(numbers, Functions.identity()); count = Aggregates.max(numbers);
} else { } else {
count = Aggregates.sum(numbers, Functions.identity()); count = Aggregates.sum(numbers);
} }
} else { } else {
count = (Integer) to; count = (Integer) to;
@@ -2390,7 +2389,7 @@ public class AbilityUtils {
} else if (sq[0].startsWith("Num")) { } else if (sq[0].startsWith("Num")) {
num = dmgInstances.size(); num = dmgInstances.size();
} else { } else {
num = Aggregates.sum(dmgInstances, Functions.identity()); num = Aggregates.sum(dmgInstances);
} }
return doXMath(num, expr, c, ctb); return doXMath(num, expr, c, ctb);
} }
@@ -3528,7 +3527,7 @@ public class AbilityUtils {
if (value.contains("Counters")) { if (value.contains("Counters")) {
int count = 0; int count = 0;
if (sq[1].equals("ALL")) { if (sq[1].equals("ALL")) {
count = Aggregates.sum(player.getCounters().values(), Functions.identity()); count = Aggregates.sum(player.getCounters().values());
} else { } else {
count = player.getCounters(CounterType.getType(sq[1])); count = player.getCounters(CounterType.getType(sq[1]));
} }

View File

@@ -5,7 +5,6 @@ import java.util.Map;
import forge.game.card.*; import forge.game.card.*;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import com.google.common.base.Functions;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
@@ -233,7 +232,7 @@ public class CountersRemoveEffect extends SpellAbilityEffect {
int max = Math.min(cntToRemove, tgtCounters.get(chosenType)); int max = Math.min(cntToRemove, tgtCounters.get(chosenType));
// remove selection so player can't cheat additional trigger by choosing the same type multiple times // remove selection so player can't cheat additional trigger by choosing the same type multiple times
tgtCounters.remove(chosenType); tgtCounters.remove(chosenType);
int remaining = Aggregates.sum(tgtCounters.values(), Functions.identity()); int remaining = Aggregates.sum(tgtCounters.values());
// player must choose enough so he can still reach the amount with other types // player must choose enough so he can still reach the amount with other types
int min = sa.hasParam("UpTo") ? 0 : Math.max(1, max - remaining); int min = sa.hasParam("UpTo") ? 0 : Math.max(1, max - remaining);
prompt = Localizer.getInstance().getMessage("lblSelectRemoveCountersNumberOfTarget", chosenType.getName()); prompt = Localizer.getInstance().getMessage("lblSelectRemoveCountersNumberOfTarget", chosenType.getName());

View File

@@ -2,8 +2,6 @@ package forge.game.trigger;
import java.util.Map; import java.util.Map;
import com.google.common.base.Functions;
import forge.game.ability.AbilityKey; import forge.game.ability.AbilityKey;
import forge.game.card.Card; import forge.game.card.Card;
import forge.game.card.CounterType; import forge.game.card.CounterType;
@@ -38,7 +36,7 @@ public class TriggerCounterPlayerAddedAll extends Trigger {
@Override @Override
public void setTriggeringObjects(SpellAbility sa, Map<AbilityKey, Object> runParams) { public void setTriggeringObjects(SpellAbility sa, Map<AbilityKey, Object> runParams) {
sa.setTriggeringObjectsFrom(runParams, AbilityKey.Source, AbilityKey.Object, AbilityKey.CounterMap); sa.setTriggeringObjectsFrom(runParams, AbilityKey.Source, AbilityKey.Object, AbilityKey.CounterMap);
sa.setTriggeringObject(AbilityKey.Amount, Aggregates.sum(((Map<CounterType, Integer>) runParams.get(AbilityKey.CounterMap)).values(), Functions.identity())); sa.setTriggeringObject(AbilityKey.Amount, Aggregates.sum(((Map<CounterType, Integer>) runParams.get(AbilityKey.CounterMap)).values()));
} }
@Override @Override