From 316137b5e2eb9633c07f7e2b70c3f9f1e38df94b Mon Sep 17 00:00:00 2001 From: igoudt Date: Mon, 9 Jan 2017 13:07:32 +0100 Subject: [PATCH] replaced various !contains -> put with putIfAbsent --- Mage/src/main/java/mage/cards/CardsImpl.java | 4 +-- .../src/main/java/mage/counters/Counters.java | 27 +++++++++---------- Mage/src/main/java/mage/game/LookedAt.java | 4 +-- .../main/java/mage/players/PlayerImpl.java | 4 +-- .../src/main/java/mage/watchers/Watchers.java | 4 +-- 5 files changed, 16 insertions(+), 27 deletions(-) diff --git a/Mage/src/main/java/mage/cards/CardsImpl.java b/Mage/src/main/java/mage/cards/CardsImpl.java index 55123325f3..c7228776be 100644 --- a/Mage/src/main/java/mage/cards/CardsImpl.java +++ b/Mage/src/main/java/mage/cards/CardsImpl.java @@ -230,9 +230,7 @@ public class CardsImpl extends LinkedHashSet implements Cards, Serializabl Map cards = new HashMap<>(); for (UUID cardId : this) { Card card = game.getCard(cardId); - if (!cards.containsKey(card.getName())) { - cards.put(card.getName(), card); - } + cards.putIfAbsent(card.getName(), card); } return cards.values(); } diff --git a/Mage/src/main/java/mage/counters/Counters.java b/Mage/src/main/java/mage/counters/Counters.java index c52c7d6cef..4d0590fbe6 100644 --- a/Mage/src/main/java/mage/counters/Counters.java +++ b/Mage/src/main/java/mage/counters/Counters.java @@ -35,15 +35,15 @@ import java.util.List; import java.util.Map; /** - * * @author BetaSteward_at_googlemail.com */ public class Counters extends HashMap implements Serializable { - public Counters() {} + public Counters() { + } public Counters(final Counters counters) { - for (Map.Entry entry: counters.entrySet()) { + for (Map.Entry entry : counters.entrySet()) { this.put(entry.getKey(), entry.getValue().copy()); } } @@ -53,16 +53,12 @@ public class Counters extends HashMap implements Serializable { } public void addCounter(String name) { - if (!this.containsKey(name)) { - this.put(name, new Counter(name)); - } + putIfAbsent(name, new Counter(name)); this.get(name).increase(); - } + } public void addCounter(String name, int amount) { - if (!this.containsKey(name)) { - this.put(name, new Counter(name)); - } + putIfAbsent(name, new Counter(name)); this.get(name).add(amount); } @@ -100,13 +96,14 @@ public class Counters extends HashMap implements Serializable { return false; } - public void removeAllCounters(CounterType counterType){ + public void removeAllCounters(CounterType counterType) { removeAllCounters(counterType.getName()); } - public void removeAllCounters(String name){ - if (this.containsKey(name)){ + public void removeAllCounters(String name) { + if (this.containsKey(name)) { this.remove(name); + } } @@ -130,9 +127,9 @@ public class Counters extends HashMap implements Serializable { public List getBoostCounters() { List boosters = new ArrayList<>(); - for (Counter counter: this.values()) { + for (Counter counter : this.values()) { if (counter instanceof BoostCounter) { - boosters.add((BoostCounter)counter); + boosters.add((BoostCounter) counter); } } return boosters; diff --git a/Mage/src/main/java/mage/game/LookedAt.java b/Mage/src/main/java/mage/game/LookedAt.java index 501cb34c70..8bab425822 100644 --- a/Mage/src/main/java/mage/game/LookedAt.java +++ b/Mage/src/main/java/mage/game/LookedAt.java @@ -64,9 +64,7 @@ public class LookedAt extends HashMap implements Serializable, Co } public Cards createLookedAt(String name) { - if (!this.containsKey(name)) { - this.put(name, new CardsImpl()); - } + putIfAbsent(name, new CardsImpl()); return this.get(name); } diff --git a/Mage/src/main/java/mage/players/PlayerImpl.java b/Mage/src/main/java/mage/players/PlayerImpl.java index 9efa581425..fc5357dc51 100644 --- a/Mage/src/main/java/mage/players/PlayerImpl.java +++ b/Mage/src/main/java/mage/players/PlayerImpl.java @@ -2748,9 +2748,7 @@ public abstract class PlayerImpl implements Player, Serializable { for (Permanent permanent : game.getBattlefield().getAllActivePermanents(playerId)) { LinkedHashMap useableAbilities = getUseableActivatedAbilities(permanent, Zone.BATTLEFIELD, game); for (ActivatedAbility ability : useableAbilities.values()) { - if (!playableActivated.containsKey(ability.toString())) { - playableActivated.put(ability.toString(), ability); - } + playableActivated.putIfAbsent(ability.toString(), ability); } } // activated abilities from stack objects diff --git a/Mage/src/main/java/mage/watchers/Watchers.java b/Mage/src/main/java/mage/watchers/Watchers.java index 70227e9d2d..a117423de2 100644 --- a/Mage/src/main/java/mage/watchers/Watchers.java +++ b/Mage/src/main/java/mage/watchers/Watchers.java @@ -51,9 +51,7 @@ public class Watchers extends HashMap { } public void add(Watcher watcher) { - if (!this.containsKey(watcher.getKey())) { - this.put(watcher.getKey(), watcher); - } + putIfAbsent(watcher.getKey(), watcher); } public void watch(GameEvent event, Game game) {