Resolve "Use hash set instead of enum set for Keyword sets"

This commit is contained in:
Hans Mackowiak
2020-12-20 08:40:16 +00:00
parent 3c243556e2
commit f670e131f9
4 changed files with 30 additions and 35 deletions

View File

@@ -124,7 +124,8 @@ public class Card extends GameEntity implements Comparable<Card>, IHasSVars {
private final PlayerCollection mayLookFaceDownExile = new PlayerCollection(); private final PlayerCollection mayLookFaceDownExile = new PlayerCollection();
private final PlayerCollection mayLookTemp = new PlayerCollection(); private final PlayerCollection mayLookTemp = new PlayerCollection();
private final Multimap<Long, Keyword> cantHaveKeywords = MultimapBuilder.hashKeys().enumSetValues(Keyword.class).build(); // don't use Enum Set Values or it causes a slow down
private final Multimap<Long, Keyword> cantHaveKeywords = MultimapBuilder.hashKeys().hashSetValues().build();
private final Map<CounterType, Long> counterTypeTimestamps = Maps.newHashMap(); private final Map<CounterType, Long> counterTypeTimestamps = Maps.newHashMap();

View File

@@ -1,22 +1,22 @@
package forge.game.keyword; package forge.game.keyword;
import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder; import com.google.common.collect.MultimapBuilder;
import forge.game.card.Card; import forge.game.card.Card;
public class KeywordCollection implements Iterable<String>, Serializable { public class KeywordCollection implements Iterable<KeywordInterface> {
private static final long serialVersionUID = -2882986558147844702L;
private boolean hidden = false; private boolean hidden = false;
private transient KeywordCollectionView view; private transient KeywordCollectionView view;
private final Multimap<Keyword, KeywordInterface> map = MultimapBuilder.enumKeys(Keyword.class) // don't use enumKeys it causes a slow down
private final Multimap<Keyword, KeywordInterface> map = MultimapBuilder.hashKeys()
.arrayListValues().build(); .arrayListValues().build();
public KeywordCollection() { public KeywordCollection() {
@@ -157,35 +157,20 @@ public class KeywordCollection implements Iterable<String>, Serializable {
return map.get(keyword); return map.get(keyword);
} }
public List<String> asStringList() {
List<String> result = Lists.newArrayList();
for (KeywordInterface kw : getValues()) {
result.add(kw.getOriginal());
}
return result;
}
public void setHostCard(final Card host) { public void setHostCard(final Card host) {
for (KeywordInterface k : map.values()) { for (KeywordInterface k : map.values()) {
k.setHostCard(host); k.setHostCard(host);
} }
} }
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
private final Iterator<KeywordInterface> iterator = map.values().iterator();
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public String next() {
KeywordInterface entry = iterator.next();
return entry.getOriginal();
}
@Override
public void remove() {
//Don't support this
}
};
}
/* (non-Javadoc) /* (non-Javadoc)
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */
@@ -204,8 +189,7 @@ public class KeywordCollection implements Iterable<String>, Serializable {
return view; return view;
} }
public class KeywordCollectionView implements Iterable<String>, Serializable { public class KeywordCollectionView implements Iterable<KeywordInterface> {
private static final long serialVersionUID = 7536969077044188264L;
protected KeywordCollectionView() { protected KeywordCollectionView() {
} }
@@ -229,9 +213,18 @@ public class KeywordCollection implements Iterable<String>, Serializable {
return KeywordCollection.this.contains(keyword); return KeywordCollection.this.contains(keyword);
} }
public List<String> asStringList() {
return KeywordCollection.this.asStringList();
}
@Override @Override
public Iterator<String> iterator() { public Iterator<KeywordInterface> iterator() {
return KeywordCollection.this.iterator(); return KeywordCollection.this.iterator();
} }
} }
@Override
public Iterator<KeywordInterface> iterator() {
return this.map.values().iterator();
}
} }

View File

@@ -1236,7 +1236,8 @@ public class Player extends GameEntity implements Comparable<Player> {
public boolean hasProtectionFrom(final Card source, final boolean checkSBA, final boolean damageSource) { public boolean hasProtectionFrom(final Card source, final boolean checkSBA, final boolean damageSource) {
final boolean colorlessDamage = damageSource && source.hasKeyword("Colorless Damage Source"); final boolean colorlessDamage = damageSource && source.hasKeyword("Colorless Damage Source");
for (String kw : keywords) { for (KeywordInterface ki : keywords) {
String kw = ki.getOriginal();
if (kw.startsWith("Protection")) { if (kw.startsWith("Protection")) {
if (kw.startsWith("Protection:")) { // uses isValid if (kw.startsWith("Protection:")) { // uses isValid
final String characteristic = kw.split(":")[1]; final String characteristic = kw.split(":")[1];
@@ -3226,7 +3227,7 @@ public class Player extends GameEntity implements Comparable<Player> {
keywordEffect.updateAbilityTextForView(); keywordEffect.updateAbilityTextForView();
boolean headerAdded = false; boolean headerAdded = false;
StringBuilder kw = new StringBuilder(); StringBuilder kw = new StringBuilder();
for(String k : keywords) { for(KeywordInterface k : keywords) {
if(!headerAdded) { if(!headerAdded) {
headerAdded = true; headerAdded = true;
kw.append(this.getName()).append(" has: \n"); kw.append(this.getName()).append(" has: \n");

View File

@@ -311,7 +311,7 @@ public class PlayerView extends GameEntityView {
return getKeywords().contains(keyword); return getKeywords().contains(keyword);
} }
void updateKeywords(Player p) { void updateKeywords(Player p) {
set(TrackableProperty.Keywords, ImmutableMultiset.copyOf(p.getKeywords())); set(TrackableProperty.Keywords, ImmutableMultiset.copyOf(p.getKeywords().asStringList()));
} }
public List<CardView> getCommanders() { public List<CardView> getCommanders() {