mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 11:48:02 +00:00
replace TreeMapOfLists with Guava Multimap
This commit is contained in:
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -15628,7 +15628,6 @@ forge-gui/src/main/java/forge/util/maps/EnumMapToAmount.java -text
|
|||||||
forge-gui/src/main/java/forge/util/maps/HashMapOfLists.java -text
|
forge-gui/src/main/java/forge/util/maps/HashMapOfLists.java -text
|
||||||
forge-gui/src/main/java/forge/util/maps/MapOfLists.java -text
|
forge-gui/src/main/java/forge/util/maps/MapOfLists.java -text
|
||||||
forge-gui/src/main/java/forge/util/maps/MapToAmount.java -text
|
forge-gui/src/main/java/forge/util/maps/MapToAmount.java -text
|
||||||
forge-gui/src/main/java/forge/util/maps/TreeMapOfLists.java -text
|
|
||||||
forge-gui/src/main/java/forge/util/maps/TreeMapToAmount.java -text
|
forge-gui/src/main/java/forge/util/maps/TreeMapToAmount.java -text
|
||||||
forge-gui/src/main/java/forge/util/maps/package-info.java -text
|
forge-gui/src/main/java/forge/util/maps/package-info.java -text
|
||||||
forge-gui/src/main/java/forge/util/package-info.java -text
|
forge-gui/src/main/java/forge/util/package-info.java -text
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ import java.util.List;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.lang3.tuple.Pair;
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
|
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.collect.ArrayListMultimap;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Multimap;
|
||||||
import forge.card.MagicColor;
|
import forge.card.MagicColor;
|
||||||
import forge.card.spellability.AbilityManaPart;
|
import forge.card.spellability.AbilityManaPart;
|
||||||
import forge.card.spellability.SpellAbility;
|
import forge.card.spellability.SpellAbility;
|
||||||
@@ -38,8 +38,6 @@ import forge.game.event.GameEventZone;
|
|||||||
import forge.game.phase.PhaseType;
|
import forge.game.phase.PhaseType;
|
||||||
import forge.game.player.Player;
|
import forge.game.player.Player;
|
||||||
import forge.game.zone.ZoneType;
|
import forge.game.zone.ZoneType;
|
||||||
import forge.util.maps.MapOfLists;
|
|
||||||
import forge.util.maps.TreeMapOfLists;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@@ -51,11 +49,7 @@ import forge.util.maps.TreeMapOfLists;
|
|||||||
*/
|
*/
|
||||||
public class ManaPool {
|
public class ManaPool {
|
||||||
|
|
||||||
private final static Supplier<List<Mana>> listFactory = new Supplier<List<Mana>>(){
|
private final Multimap<Byte, Mana> floatingMana = ArrayListMultimap.create();
|
||||||
@Override public List<Mana> get() { return new ArrayList<Mana>(); }
|
|
||||||
};
|
|
||||||
|
|
||||||
private final MapOfLists<Byte, Mana> floatingMana = new TreeMapOfLists<Byte, Mana>(listFactory);
|
|
||||||
|
|
||||||
/** Constant <code>map</code>. */
|
/** Constant <code>map</code>. */
|
||||||
private final Player owner;
|
private final Player owner;
|
||||||
@@ -78,7 +72,7 @@ public class ManaPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addMana(final Mana mana) {
|
private void addMana(final Mana mana) {
|
||||||
floatingMana.add(mana.getColorCode(), mana);
|
floatingMana.put(mana.getColorCode(), mana);
|
||||||
owner.getGame().fireEvent(new GameEventManaPool(owner, EventValueChangeType.Added, mana));
|
owner.getGame().fireEvent(new GameEventManaPool(owner, EventValueChangeType.Added, mana));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +126,7 @@ public class ManaPool {
|
|||||||
}
|
}
|
||||||
numRemoved += floatingMana.get(b).size() - pMana.size();
|
numRemoved += floatingMana.get(b).size() - pMana.size();
|
||||||
floatingMana.get(b).clear();
|
floatingMana.get(b).clear();
|
||||||
floatingMana.addAll(b, pMana);
|
floatingMana.putAll(b, pMana);
|
||||||
} else {
|
} else {
|
||||||
numRemoved += floatingMana.get(b).size();
|
numRemoved += floatingMana.get(b).size();
|
||||||
floatingMana.get(b).clear();
|
floatingMana.get(b).clear();
|
||||||
@@ -320,11 +314,7 @@ public class ManaPool {
|
|||||||
* @return a int.
|
* @return a int.
|
||||||
*/
|
*/
|
||||||
public final int totalMana() {
|
public final int totalMana() {
|
||||||
int total = 0;
|
return floatingMana.values().size();
|
||||||
for (Collection<Mana> cm : floatingMana.values()) {
|
|
||||||
total += cm.size();
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ import java.util.Set;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import com.google.common.base.Predicate;
|
import com.google.common.base.Predicate;
|
||||||
|
import com.google.common.collect.ArrayListMultimap;
|
||||||
|
import com.google.common.collect.Multimap;
|
||||||
|
|
||||||
import forge.Card;
|
import forge.Card;
|
||||||
import forge.CardLists;
|
import forge.CardLists;
|
||||||
import forge.CardUtil;
|
import forge.CardUtil;
|
||||||
@@ -38,7 +41,6 @@ import forge.game.zone.ZoneType;
|
|||||||
import forge.util.CollectionSuppliers;
|
import forge.util.CollectionSuppliers;
|
||||||
import forge.util.maps.EnumMapOfLists;
|
import forge.util.maps.EnumMapOfLists;
|
||||||
import forge.util.maps.MapOfLists;
|
import forge.util.maps.MapOfLists;
|
||||||
import forge.util.maps.TreeMapOfLists;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: Write javadoc for this type.
|
* TODO: Write javadoc for this type.
|
||||||
@@ -103,7 +105,7 @@ public class ComputerUtilMana {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// arrange all mana abilities by color produced.
|
// arrange all mana abilities by color produced.
|
||||||
final MapOfLists<Integer, SpellAbility> manaAbilityMap = ComputerUtilMana.groupSourcesByManaColor(ai, checkPlayable);
|
final Multimap<Integer, SpellAbility> manaAbilityMap = ComputerUtilMana.groupSourcesByManaColor(ai, checkPlayable);
|
||||||
if ( manaAbilityMap.isEmpty() ) {
|
if ( manaAbilityMap.isEmpty() ) {
|
||||||
manapool.clearManaPaid(sa, test);
|
manapool.clearManaPaid(sa, test);
|
||||||
handleOfferingsAI(sa, test, cost.isPaid());
|
handleOfferingsAI(sa, test, cost.isPaid());
|
||||||
@@ -415,7 +417,7 @@ public class ComputerUtilMana {
|
|||||||
* @param foundAllSources
|
* @param foundAllSources
|
||||||
* @return Were all mana sources found?
|
* @return Were all mana sources found?
|
||||||
*/
|
*/
|
||||||
private static MapOfLists<ManaCostShard, SpellAbility> groupAndOrderToPayShards(final Player ai, final MapOfLists<Integer, SpellAbility> manaAbilityMap,
|
private static MapOfLists<ManaCostShard, SpellAbility> groupAndOrderToPayShards(final Player ai, final Multimap<Integer, SpellAbility> manaAbilityMap,
|
||||||
final ManaCostBeingPaid cost) {
|
final ManaCostBeingPaid cost) {
|
||||||
MapOfLists<ManaCostShard, SpellAbility> res = new EnumMapOfLists<ManaCostShard, SpellAbility>(ManaCostShard.class, CollectionSuppliers.<SpellAbility>arrayLists());
|
MapOfLists<ManaCostShard, SpellAbility> res = new EnumMapOfLists<ManaCostShard, SpellAbility>(ManaCostShard.class, CollectionSuppliers.<SpellAbility>arrayLists());
|
||||||
|
|
||||||
@@ -435,9 +437,9 @@ public class ComputerUtilMana {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(Entry<Integer, Collection<SpellAbility>> kv : manaAbilityMap.entrySet()) {
|
for(Entry<Integer, SpellAbility> kv : manaAbilityMap.entries()) {
|
||||||
if( shard.canBePaidWithManaOfColor(kv.getKey().byteValue()) )
|
if( shard.canBePaidWithManaOfColor(kv.getKey().byteValue()) )
|
||||||
res.addAll(shard, kv.getValue());
|
res.add(shard, kv.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -620,8 +622,8 @@ public class ComputerUtilMana {
|
|||||||
|
|
||||||
|
|
||||||
//This method is currently used by AI to estimate mana available
|
//This method is currently used by AI to estimate mana available
|
||||||
private static MapOfLists<Integer, SpellAbility> groupSourcesByManaColor(final Player ai, boolean checkPlayable) {
|
private static Multimap<Integer, SpellAbility> groupSourcesByManaColor(final Player ai, boolean checkPlayable) {
|
||||||
final MapOfLists<Integer, SpellAbility> manaMap = new TreeMapOfLists<Integer, SpellAbility>(CollectionSuppliers.<SpellAbility>arrayLists());
|
final Multimap<Integer, SpellAbility> manaMap = ArrayListMultimap.create();
|
||||||
final Game game = ai.getGame();
|
final Game game = ai.getGame();
|
||||||
|
|
||||||
// Loop over all current available mana sources
|
// Loop over all current available mana sources
|
||||||
@@ -640,7 +642,7 @@ public class ComputerUtilMana {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
manaMap.add(ManaAtom.COLORLESS, m); // add to colorless source list
|
manaMap.put(ManaAtom.COLORLESS, m); // add to colorless source list
|
||||||
AbilityManaPart mp = m.getManaPart();
|
AbilityManaPart mp = m.getManaPart();
|
||||||
|
|
||||||
// setup produce mana replacement effects
|
// setup produce mana replacement effects
|
||||||
@@ -667,22 +669,22 @@ public class ComputerUtilMana {
|
|||||||
Set<String> reflectedColors = CardUtil.getReflectableManaColors(m);
|
Set<String> reflectedColors = CardUtil.getReflectableManaColors(m);
|
||||||
// find possible colors
|
// find possible colors
|
||||||
if (mp.canProduce("W", m) || reflectedColors.contains(MagicColor.Constant.WHITE)) {
|
if (mp.canProduce("W", m) || reflectedColors.contains(MagicColor.Constant.WHITE)) {
|
||||||
manaMap.add(ManaAtom.WHITE, m);
|
manaMap.put(ManaAtom.WHITE, m);
|
||||||
}
|
}
|
||||||
if (mp.canProduce("U", m) || reflectedColors.contains(MagicColor.Constant.BLUE)) {
|
if (mp.canProduce("U", m) || reflectedColors.contains(MagicColor.Constant.BLUE)) {
|
||||||
manaMap.add(ManaAtom.BLUE, m);
|
manaMap.put(ManaAtom.BLUE, m);
|
||||||
}
|
}
|
||||||
if (mp.canProduce("B", m) || reflectedColors.contains(MagicColor.Constant.BLACK)) {
|
if (mp.canProduce("B", m) || reflectedColors.contains(MagicColor.Constant.BLACK)) {
|
||||||
manaMap.add(ManaAtom.BLACK, m);
|
manaMap.put(ManaAtom.BLACK, m);
|
||||||
}
|
}
|
||||||
if (mp.canProduce("R", m) || reflectedColors.contains(MagicColor.Constant.RED)) {
|
if (mp.canProduce("R", m) || reflectedColors.contains(MagicColor.Constant.RED)) {
|
||||||
manaMap.add(ManaAtom.RED, m);
|
manaMap.put(ManaAtom.RED, m);
|
||||||
}
|
}
|
||||||
if (mp.canProduce("G", m) || reflectedColors.contains(MagicColor.Constant.GREEN)) {
|
if (mp.canProduce("G", m) || reflectedColors.contains(MagicColor.Constant.GREEN)) {
|
||||||
manaMap.add(ManaAtom.GREEN, m);
|
manaMap.put(ManaAtom.GREEN, m);
|
||||||
}
|
}
|
||||||
if (mp.isSnow()) {
|
if (mp.isSnow()) {
|
||||||
manaMap.add(ManaAtom.IS_SNOW, m);
|
manaMap.put(ManaAtom.IS_SNOW, m);
|
||||||
}
|
}
|
||||||
} // end of mana abilities loop
|
} // end of mana abilities loop
|
||||||
} // end of mana sources loop
|
} // end of mana sources loop
|
||||||
|
|||||||
@@ -1,57 +0,0 @@
|
|||||||
package forge.util.maps;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.SortedMap;
|
|
||||||
import java.util.TreeMap;
|
|
||||||
|
|
||||||
import com.google.common.base.Supplier;
|
|
||||||
|
|
||||||
public class TreeMapOfLists<K, V> extends TreeMap<K, Collection<V>> implements MapOfLists<K, V> {
|
|
||||||
private static final long serialVersionUID = -5881288393640446185L;
|
|
||||||
private final Supplier<? extends Collection<V>> factory;
|
|
||||||
|
|
||||||
public TreeMapOfLists(Supplier<? extends Collection<V>> factory) {
|
|
||||||
super();
|
|
||||||
this.factory = factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TreeMapOfLists(Comparator<? super K> comparator, Supplier<? extends Collection<V>> factory) {
|
|
||||||
super(comparator);
|
|
||||||
this.factory = factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TreeMapOfLists(Map<? extends K, ? extends List<V>> m, Supplier<? extends Collection<V>> factory) {
|
|
||||||
super(m);
|
|
||||||
this.factory = factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TreeMapOfLists(SortedMap<K, ? extends List<V>> m, Supplier<? extends Collection<V>> factory) {
|
|
||||||
super(m);
|
|
||||||
this.factory = factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Collection<V> ensureCollectionFor(K key) {
|
|
||||||
Collection<V> value = get(key);
|
|
||||||
if ( value == null ) {
|
|
||||||
value = factory.get();
|
|
||||||
put(key, value);
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void add(K key, V element) {
|
|
||||||
ensureCollectionFor(key).add(element);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addAll(K key, Collection<V> elements) {
|
|
||||||
ensureCollectionFor(key).addAll(elements);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user