replace TreeMapOfLists with Guava Multimap

This commit is contained in:
Maxmtg
2013-11-24 06:45:30 +00:00
parent c8de886967
commit 5b5517ad29
4 changed files with 22 additions and 88 deletions

1
.gitattributes vendored
View File

@@ -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/MapOfLists.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/package-info.java -text
forge-gui/src/main/java/forge/util/package-info.java -text

View File

@@ -25,9 +25,9 @@ import java.util.List;
import org.apache.commons.lang3.StringUtils;
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.Multimap;
import forge.card.MagicColor;
import forge.card.spellability.AbilityManaPart;
import forge.card.spellability.SpellAbility;
@@ -38,8 +38,6 @@ import forge.game.event.GameEventZone;
import forge.game.phase.PhaseType;
import forge.game.player.Player;
import forge.game.zone.ZoneType;
import forge.util.maps.MapOfLists;
import forge.util.maps.TreeMapOfLists;
/**
* <p>
@@ -51,11 +49,7 @@ import forge.util.maps.TreeMapOfLists;
*/
public class ManaPool {
private final static Supplier<List<Mana>> listFactory = new Supplier<List<Mana>>(){
@Override public List<Mana> get() { return new ArrayList<Mana>(); }
};
private final MapOfLists<Byte, Mana> floatingMana = new TreeMapOfLists<Byte, Mana>(listFactory);
private final Multimap<Byte, Mana> floatingMana = ArrayListMultimap.create();
/** Constant <code>map</code>. */
private final Player owner;
@@ -78,7 +72,7 @@ public class ManaPool {
}
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));
}
@@ -132,7 +126,7 @@ public class ManaPool {
}
numRemoved += floatingMana.get(b).size() - pMana.size();
floatingMana.get(b).clear();
floatingMana.addAll(b, pMana);
floatingMana.putAll(b, pMana);
} else {
numRemoved += floatingMana.get(b).size();
floatingMana.get(b).clear();
@@ -320,11 +314,7 @@ public class ManaPool {
* @return a int.
*/
public final int totalMana() {
int total = 0;
for (Collection<Mana> cm : floatingMana.values()) {
total += cm.size();
}
return total;
return floatingMana.values().size();
}
/**

View File

@@ -13,6 +13,9 @@ import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Predicate;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import forge.Card;
import forge.CardLists;
import forge.CardUtil;
@@ -38,7 +41,6 @@ import forge.game.zone.ZoneType;
import forge.util.CollectionSuppliers;
import forge.util.maps.EnumMapOfLists;
import forge.util.maps.MapOfLists;
import forge.util.maps.TreeMapOfLists;
/**
* TODO: Write javadoc for this type.
@@ -103,7 +105,7 @@ public class ComputerUtilMana {
}
// 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() ) {
manapool.clearManaPaid(sa, test);
handleOfferingsAI(sa, test, cost.isPaid());
@@ -415,7 +417,7 @@ public class ComputerUtilMana {
* @param foundAllSources
* @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) {
MapOfLists<ManaCostShard, SpellAbility> res = new EnumMapOfLists<ManaCostShard, SpellAbility>(ManaCostShard.class, CollectionSuppliers.<SpellAbility>arrayLists());
@@ -435,9 +437,9 @@ public class ComputerUtilMana {
continue;
}
for(Entry<Integer, Collection<SpellAbility>> kv : manaAbilityMap.entrySet()) {
for(Entry<Integer, SpellAbility> kv : manaAbilityMap.entries()) {
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
private static MapOfLists<Integer, SpellAbility> groupSourcesByManaColor(final Player ai, boolean checkPlayable) {
final MapOfLists<Integer, SpellAbility> manaMap = new TreeMapOfLists<Integer, SpellAbility>(CollectionSuppliers.<SpellAbility>arrayLists());
private static Multimap<Integer, SpellAbility> groupSourcesByManaColor(final Player ai, boolean checkPlayable) {
final Multimap<Integer, SpellAbility> manaMap = ArrayListMultimap.create();
final Game game = ai.getGame();
// 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();
// setup produce mana replacement effects
@@ -667,22 +669,22 @@ public class ComputerUtilMana {
Set<String> reflectedColors = CardUtil.getReflectableManaColors(m);
// find possible colors
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)) {
manaMap.add(ManaAtom.BLUE, m);
manaMap.put(ManaAtom.BLUE, m);
}
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)) {
manaMap.add(ManaAtom.RED, m);
manaMap.put(ManaAtom.RED, m);
}
if (mp.canProduce("G", m) || reflectedColors.contains(MagicColor.Constant.GREEN)) {
manaMap.add(ManaAtom.GREEN, m);
manaMap.put(ManaAtom.GREEN, m);
}
if (mp.isSnow()) {
manaMap.add(ManaAtom.IS_SNOW, m);
manaMap.put(ManaAtom.IS_SNOW, m);
}
} // end of mana abilities loop
} // end of mana sources loop

View File

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