Cleanup - Map.getOrDefault and Map.computeIfAbsent

This commit is contained in:
Jetz
2024-08-10 12:48:52 -04:00
parent bdc890c32c
commit 4614c6c0c9
16 changed files with 22 additions and 77 deletions

View File

@@ -15,11 +15,7 @@ public class FTrace {
} }
public static FTrace get(String name0) { public static FTrace get(String name0) {
FTrace trace = traces.get(name0); FTrace trace = traces.computeIfAbsent(name0, FTrace::new);
if (trace == null) {
trace = new FTrace(name0);
traces.put(name0, trace);
}
return trace; return trace;
} }

View File

@@ -655,7 +655,7 @@ public abstract class CardTraitBase extends GameObject implements IHasCardView,
Map<String, String> result = Maps.newHashMap(output); Map<String, String> result = Maps.newHashMap(output);
for (Map.Entry<String, String> e : input.entrySet()) { for (Map.Entry<String, String> e : input.entrySet()) {
String value = e.getValue(); String value = e.getValue();
result.put(e.getKey(), output.containsKey(value) ? output.get(value) : value); result.put(e.getKey(), output.getOrDefault(value, value));
} }
return result; return result;
} }

View File

@@ -78,7 +78,7 @@ public class GameEntityCounterTable extends ForwardingTable<Optional<Player>, Ga
} }
Map<CounterType, Integer> alreadyRemoved = column(ge).get(Optional.absent()); Map<CounterType, Integer> alreadyRemoved = column(ge).get(Optional.absent());
for (Map.Entry<CounterType, Integer> e : ge.getCounters().entrySet()) { for (Map.Entry<CounterType, Integer> e : ge.getCounters().entrySet()) {
int rest = e.getValue() - (alreadyRemoved.containsKey(e.getKey()) ? alreadyRemoved.get(e.getKey()) : 0); int rest = e.getValue() - (alreadyRemoved.getOrDefault(e.getKey(), 0));
if (rest > 0) { if (rest > 0) {
result.put(e.getKey(), rest); result.put(e.getKey(), rest);
} }

View File

@@ -362,7 +362,7 @@ public class CountersMoveEffect extends SpellAbilityEffect {
if (cnum > 0) { if (cnum > 0) {
src.subtractCounter(cType, cnum, activator); src.subtractCounter(cType, cnum, activator);
game.updateLastStateForCard(src); game.updateLastStateForCard(src);
countersToAdd.put(cType, (countersToAdd.containsKey(cType) ? countersToAdd.get(cType) : 0) + cnum); countersToAdd.put(cType, (countersToAdd.getOrDefault(cType, 0)) + cnum);
} }
} }
} }

View File

@@ -7950,26 +7950,14 @@ public class Card extends GameEntity implements Comparable<Card>, IHasSVars {
} }
} }
} else { } else {
List<String> result = chosenModesTurn.get(original); List<String> result = chosenModesTurn.computeIfAbsent(original, k -> Lists.newArrayList());
if (result == null) {
result = Lists.newArrayList();
chosenModesTurn.put(original, result);
}
result.add(mode); result.add(mode);
result = chosenModesGame.get(original); result = chosenModesGame.computeIfAbsent(original, k -> Lists.newArrayList());
if (result == null) {
result = Lists.newArrayList();
chosenModesGame.put(original, result);
}
result.add(mode); result.add(mode);
if (yourCombat) { if (yourCombat) {
result = chosenModesYourCombat.get(original); result = chosenModesYourCombat.computeIfAbsent(original, k -> Lists.newArrayList());
if (result == null) {
result = Lists.newArrayList();
chosenModesYourCombat.put(original, result);
}
result.add(mode); result.add(mode);
} }
} }

View File

@@ -263,11 +263,7 @@ public class ManaCostBeingPaid {
private void increaseShard(final ManaCostShard shard, final int toAdd, final boolean forX) { private void increaseShard(final ManaCostShard shard, final int toAdd, final boolean forX) {
if (toAdd <= 0) { return; } if (toAdd <= 0) { return; }
ShardCount sc = unpaidShards.get(shard); ShardCount sc = unpaidShards.computeIfAbsent(shard, k -> new ShardCount());
if (sc == null) {
sc = new ShardCount();
unpaidShards.put(shard, sc);
}
if (forX) { if (forX) {
sc.xCount += toAdd; sc.xCount += toAdd;
} }

View File

@@ -445,7 +445,7 @@ public class ImportSourceAnalyzer {
analyzeListedDir(root, ForgeConstants.CACHE_ICON_PICS_DIR, new ListedAnalyzer() { analyzeListedDir(root, ForgeConstants.CACHE_ICON_PICS_DIR, new ListedAnalyzer() {
@Override @Override
public String map(final String filename) { public String map(final String filename) {
return iconFileNames.containsKey(filename) ? iconFileNames.get(filename) : null; return iconFileNames.getOrDefault(filename, null);
} }
@Override @Override
@@ -617,7 +617,7 @@ public class ImportSourceAnalyzer {
analyzeListedDir(root, targetDir, new ListedAnalyzer() { analyzeListedDir(root, targetDir, new ListedAnalyzer() {
@Override @Override
public String map(final String filename) { public String map(final String filename) {
return fileDb.containsKey(filename) ? fileDb.get(filename) : null; return fileDb.getOrDefault(filename, null);
} }
@Override @Override

View File

@@ -826,11 +826,7 @@ public abstract class ItemManager<T extends InventoryItem> extends JPanel implem
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void addFilter(final ItemFilter<? extends T> filter) { public void addFilter(final ItemFilter<? extends T> filter) {
final Class<? extends ItemFilter<? extends T>> filterClass = (Class<? extends ItemFilter<? extends T>>) filter.getClass(); final Class<? extends ItemFilter<? extends T>> filterClass = (Class<? extends ItemFilter<? extends T>>) filter.getClass();
List<ItemFilter<? extends T>> classFilters = this.filters.get(filterClass); List<ItemFilter<? extends T>> classFilters = this.filters.computeIfAbsent(filterClass, k -> new ArrayList<>());
if (classFilters == null) {
classFilters = new ArrayList<>();
this.filters.put(filterClass, classFilters);
}
if (classFilters.size() > 0) { if (classFilters.size() > 0) {
//if filter with the same class already exists, try to merge if allowed //if filter with the same class already exists, try to merge if allowed
//NOTE: can always use first filter for these checks since if //NOTE: can always use first filter for these checks since if

View File

@@ -44,7 +44,7 @@ class AsyncSoundRegistry {
} }
public synchronized static int getNumIterations(String soundName) { public synchronized static int getNumIterations(String soundName) {
return soundsPlayed.containsKey(soundName) ? soundsPlayed.get(soundName) : 0; return soundsPlayed.getOrDefault(soundName, 0);
} }
} }

View File

@@ -940,11 +940,7 @@ public class FSkin {
/** @return {@link java.awt.font} */ /** @return {@link java.awt.font} */
private static Font getFixedFont(final int size) { private static Font getFixedFont(final int size) {
Font fixedFont = fixedFonts.get(size); Font fixedFont = fixedFonts.computeIfAbsent(size, s -> new Font("Monospaced", Font.PLAIN, s));
if (fixedFont == null) {
fixedFont = new Font("Monospaced", Font.PLAIN, size);
fixedFonts.put(size, fixedFont);
}
return fixedFont; return fixedFont;
} }

View File

@@ -147,7 +147,7 @@ public class PointOfInterestChanges implements SaveFileContent {
public void addObjectReputation(int id, int delta) public void addObjectReputation(int id, int delta)
{ {
reputation.put(id, (reputation.containsKey(id)?reputation.get(id):0) + delta); reputation.put(id, (reputation.getOrDefault(id, 0)) + delta);
} }
public int getMapReputation(){ public int getMapReputation(){

View File

@@ -119,11 +119,7 @@ public class DeckPreferences {
public static DeckPreferences getPrefs(DeckProxy deck) { public static DeckPreferences getPrefs(DeckProxy deck) {
String key = deck.getUniqueKey(); String key = deck.getUniqueKey();
DeckPreferences prefs = allPrefs.get(key); DeckPreferences prefs = allPrefs.computeIfAbsent(key, k -> new DeckPreferences());
if (prefs == null) {
prefs = new DeckPreferences();
allPrefs.put(key, prefs);
}
return prefs; return prefs;
} }

View File

@@ -300,11 +300,7 @@ public class QuestEventDraft implements IQuestEvent {
int value; int value;
final String boosterName = FModel.getMagicDb().getEditions().get(boosterSet).getName() + " Booster Pack"; final String boosterName = FModel.getMagicDb().getEditions().get(boosterSet).getName() + " Booster Pack";
if (MAP_PRICES.containsKey(boosterName)) { value = MAP_PRICES.getOrDefault(boosterName, 395);
value = MAP_PRICES.get(boosterName);
} else {
value = 395;
}
boosterPrices += value; boosterPrices += value;
} }
@@ -529,11 +525,7 @@ public class QuestEventDraft implements IQuestEvent {
final String boosterName = booster.getName(); final String boosterName = booster.getName();
if (MAP_PRICES.containsKey(boosterName)) { value = MAP_PRICES.getOrDefault(boosterName, 395);
value = MAP_PRICES.get(boosterName);
} else {
value = 395;
}
return value; return value;
@@ -961,11 +953,7 @@ public class QuestEventDraft implements IQuestEvent {
int value; int value;
final String boosterName = FModel.getMagicDb().getEditions().get(boosterSet).getName() + " Booster Pack"; final String boosterName = FModel.getMagicDb().getEditions().get(boosterSet).getName() + " Booster Pack";
if (MAP_PRICES.containsKey(boosterName)) { value = MAP_PRICES.getOrDefault(boosterName, 395);
value = MAP_PRICES.get(boosterName);
} else {
value = 395;
}
entryFee += value; entryFee += value;

View File

@@ -92,11 +92,7 @@ public class QuestPetStorage {
* Refactoring this to List<QuestPetController> list = this.petsBySlot.computeIfAbsent(Integer.valueOf(iSlot), k -> new ArrayList<QuestPetController>()); * Refactoring this to List<QuestPetController> list = this.petsBySlot.computeIfAbsent(Integer.valueOf(iSlot), k -> new ArrayList<QuestPetController>());
* will cause Android not to compile * will cause Android not to compile
* */ * */
List<QuestPetController> list = this.petsBySlot.get(iSlot); List<QuestPetController> list = this.petsBySlot.computeIfAbsent(iSlot, k -> new ArrayList<>());
if (null == list) {
list = new ArrayList<>();
this.petsBySlot.put(iSlot, list);
}
this.petsByName.put(petCtrl.getName(), petCtrl); this.petsByName.put(petCtrl.getName(), petCtrl);
list.add(petCtrl); list.add(petCtrl);
} }

View File

@@ -157,11 +157,8 @@ public class QuestAssets {
* @param level int * @param level int
*/ */
public final void setPetLevel(final String name, final int level) { public final void setPetLevel(final String name, final int level) {
QuestItemCondition cond = this.combatPets.get(name); QuestItemCondition cond = this.combatPets.computeIfAbsent(name, k -> new QuestItemCondition());
if (null == cond) { // pets have only level that should be serialized for now
cond = new QuestItemCondition(); // pets have only level that should be serialized for now
this.combatPets.put(name, cond);
}
cond.setLevel(level); cond.setLevel(level);
} }

View File

@@ -28,11 +28,7 @@ public class CardPreferences {
public static CardPreferences getPrefs(IPaperCard card) { public static CardPreferences getPrefs(IPaperCard card) {
String cardName = card.getName(); String cardName = card.getName();
CardPreferences prefs = allPrefs.get(cardName); CardPreferences prefs = allPrefs.computeIfAbsent(cardName, CardPreferences::new);
if (prefs == null) {
prefs = new CardPreferences(cardName);
allPrefs.put(cardName, prefs);
}
return prefs; return prefs;
} }