From 2e83930ace8adabc9045db7d981fe1b28ee7dc14 Mon Sep 17 00:00:00 2001 From: vraskulin Date: Mon, 19 Dec 2016 12:23:23 +0300 Subject: [PATCH] Creating new ArrayList is unneccesarry We shouldn't create new arraylist with default size and no element in it to show that no elements found in database. It's one more object in heap, which will be never used. There is special method Collections.emptyList() - it's more readeable and returns empty immutable list --- .../src/mage/player/ai/SimulatedPlayer2.java | 19 ++--- .../effects/common/DevourEffect.java | 72 ++++++++++--------- .../java/mage/cards/repository/CardInfo.java | 10 +-- .../mage/cards/repository/CardRepository.java | 17 ++--- .../cards/repository/ExpansionRepository.java | 5 +- .../target/targetpointer/FixedTarget.java | 10 +-- 6 files changed, 67 insertions(+), 66 deletions(-) diff --git a/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/SimulatedPlayer2.java b/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/SimulatedPlayer2.java index 7a9aa0eee7..6ae7612a78 100644 --- a/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/SimulatedPlayer2.java +++ b/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/SimulatedPlayer2.java @@ -27,15 +27,6 @@ */ package mage.player.ai; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.concurrent.ConcurrentLinkedQueue; import mage.MageObject; import mage.abilities.Ability; import mage.abilities.TriggeredAbility; @@ -56,6 +47,9 @@ import mage.players.net.UserData; import mage.target.Target; import org.apache.log4j.Logger; +import java.util.*; +import java.util.concurrent.ConcurrentLinkedQueue; + /** * * @author BetaSteward_at_googlemail.com @@ -63,12 +57,11 @@ import org.apache.log4j.Logger; public class SimulatedPlayer2 extends ComputerPlayer { private static final Logger logger = Logger.getLogger(SimulatedPlayer2.class); + private static PassAbility pass = new PassAbility(); private final boolean isSimulatedPlayer; + private final List suggested; private transient ConcurrentLinkedQueue allActions; private boolean forced; - private static PassAbility pass = new PassAbility(); - - private final List suggested; public SimulatedPlayer2(UUID id, boolean isSimulatedPlayer, List suggested) { super(id); @@ -375,7 +368,7 @@ public class SimulatedPlayer2 extends ComputerPlayer { Map engagements = new HashMap<>(); int numGroups = game.getCombat().getGroups().size(); if (numGroups == 0) { - return new ArrayList<>(); + return Collections.emptyList(); } //add a node with no blockers diff --git a/Mage/src/main/java/mage/abilities/effects/common/DevourEffect.java b/Mage/src/main/java/mage/abilities/effects/common/DevourEffect.java index e7b40e0e74..1f060e3720 100644 --- a/Mage/src/main/java/mage/abilities/effects/common/DevourEffect.java +++ b/Mage/src/main/java/mage/abilities/effects/common/DevourEffect.java @@ -27,9 +27,6 @@ */ package mage.abilities.effects.common; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; import mage.abilities.Ability; import mage.abilities.Mode; import mage.abilities.effects.ReplacementEffectImpl; @@ -46,6 +43,11 @@ import mage.players.Player; import mage.target.Target; import mage.target.common.TargetControlledCreaturePermanent; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.UUID; + /** * Effect for the DevourAbility * @@ -68,37 +70,6 @@ public class DevourEffect extends ReplacementEffectImpl { } private final DevourFactor devourFactor; - public enum DevourFactor { - - Devour1("Devour 1", "that many +1/+1 counters on it", 1), - Devour2("Devour 2", "twice that many +1/+1 counters on it", 2), - Devour3("Devour 3", "three times that many +1/+1 counters on it", 3), - DevourX("Devour X, where X is the number of creatures devoured this way", "X +1/+1 counters on it for each of those creatures", Integer.MAX_VALUE); - - private final String text; - private final String ruleText; - private final int factor; - - DevourFactor(String text, String ruleText, int factor) { - this.text = text; - this.ruleText = ruleText; - this.factor = factor; - } - - @Override - public String toString() { - return text; - } - - public String getRuleText() { - return ruleText; - } - - public int getFactor() { - return factor; - } - } - public DevourEffect(DevourFactor devourFactor) { super(Duration.EndOfGame, Outcome.Detriment); this.devourFactor = devourFactor; @@ -184,7 +155,7 @@ public class DevourEffect extends ReplacementEffectImpl { if (object != null) { return (List>) object; } - return new ArrayList<>(); + return Collections.emptyList(); } public int getDevouredCreaturesAmount(Game game, UUID permanentId) { @@ -199,4 +170,35 @@ public class DevourEffect extends ReplacementEffectImpl { public DevourEffect copy() { return new DevourEffect(this); } + + public enum DevourFactor { + + Devour1("Devour 1", "that many +1/+1 counters on it", 1), + Devour2("Devour 2", "twice that many +1/+1 counters on it", 2), + Devour3("Devour 3", "three times that many +1/+1 counters on it", 3), + DevourX("Devour X, where X is the number of creatures devoured this way", "X +1/+1 counters on it for each of those creatures", Integer.MAX_VALUE); + + private final String text; + private final String ruleText; + private final int factor; + + DevourFactor(String text, String ruleText, int factor) { + this.text = text; + this.ruleText = ruleText; + this.factor = factor; + } + + @Override + public String toString() { + return text; + } + + public String getRuleText() { + return ruleText; + } + + public int getFactor() { + return factor; + } + } } diff --git a/Mage/src/main/java/mage/cards/repository/CardInfo.java b/Mage/src/main/java/mage/cards/repository/CardInfo.java index 29ab851bd3..3358bfa1fe 100644 --- a/Mage/src/main/java/mage/cards/repository/CardInfo.java +++ b/Mage/src/main/java/mage/cards/repository/CardInfo.java @@ -30,9 +30,6 @@ package mage.cards.repository; import com.j256.ormlite.field.DataType; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import mage.ObjectColor; import mage.abilities.Ability; import mage.abilities.SpellAbility; @@ -48,6 +45,11 @@ import mage.constants.Rarity; import mage.constants.SpellAbilityType; import org.apache.log4j.Logger; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + /** * * @author North @@ -244,7 +246,7 @@ public class CardInfo { private List parseList(String list) { if (list.isEmpty()) { - return new ArrayList<>(); + return Collections.emptyList(); } return Arrays.asList(list.split(SEPARATOR)); } diff --git a/Mage/src/main/java/mage/cards/repository/CardRepository.java b/Mage/src/main/java/mage/cards/repository/CardRepository.java index 388e4e100b..5a01631bbf 100644 --- a/Mage/src/main/java/mage/cards/repository/CardRepository.java +++ b/Mage/src/main/java/mage/cards/repository/CardRepository.java @@ -37,16 +37,17 @@ import com.j256.ormlite.stmt.Where; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.support.DatabaseConnection; import com.j256.ormlite.table.TableUtils; -import java.io.File; -import java.sql.SQLException; -import java.util.*; -import java.util.concurrent.Callable; import mage.cards.CardSetInfo; import mage.constants.CardType; import mage.constants.SetType; import mage.util.RandomUtil; import org.apache.log4j.Logger; +import java.io.File; +import java.sql.SQLException; +import java.util.*; +import java.util.concurrent.Callable; + /** * * @author North @@ -339,7 +340,7 @@ public enum CardRepository { return cardDao.query(queryBuilder.prepare()); } catch (SQLException ex) { } - return new ArrayList<>(); + return Collections.emptyList(); } /** @@ -392,7 +393,7 @@ public enum CardRepository { return cardDao.query(queryBuilder.prepare()); } catch (SQLException ex) { } - return new ArrayList<>(); + return Collections.emptyList(); } public List findCardsCaseInsensitive(String name) { @@ -409,7 +410,7 @@ public enum CardRepository { } catch (SQLException ex) { Logger.getLogger(CardRepository.class).error("Error during execution of raw sql statement", ex); } - return new ArrayList<>(); + return Collections.emptyList(); } public List findCards(CardCriteria criteria) { @@ -421,7 +422,7 @@ public enum CardRepository { } catch (SQLException ex) { Logger.getLogger(CardRepository.class).error("Error during execution of card repository query statement", ex); } - return new ArrayList<>(); + return Collections.emptyList(); } public long getContentVersionFromDB() { diff --git a/Mage/src/main/java/mage/cards/repository/ExpansionRepository.java b/Mage/src/main/java/mage/cards/repository/ExpansionRepository.java index a6a94d618e..b1fcecfda8 100644 --- a/Mage/src/main/java/mage/cards/repository/ExpansionRepository.java +++ b/Mage/src/main/java/mage/cards/repository/ExpansionRepository.java @@ -12,6 +12,7 @@ import org.apache.log4j.Logger; import java.io.File; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -144,7 +145,7 @@ public enum ExpansionRepository { return expansionDao.query(qb.prepare()); } catch (SQLException ex) { } - return new ArrayList<>(); + return Collections.emptyList(); } public List getAllSetNames() { @@ -159,7 +160,7 @@ public enum ExpansionRepository { return setNames; } catch (SQLException ex) { } - return new ArrayList<>(); + return Collections.emptyList(); } public long getContentVersionFromDB() { diff --git a/Mage/src/main/java/mage/target/targetpointer/FixedTarget.java b/Mage/src/main/java/mage/target/targetpointer/FixedTarget.java index 9c9a755bcf..397e4e7de3 100644 --- a/Mage/src/main/java/mage/target/targetpointer/FixedTarget.java +++ b/Mage/src/main/java/mage/target/targetpointer/FixedTarget.java @@ -1,14 +1,16 @@ package mage.target.targetpointer; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; import mage.abilities.Ability; import mage.cards.Card; import mage.constants.Zone; import mage.game.Game; import mage.game.permanent.Permanent; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.UUID; + public class FixedTarget implements TargetPointer { private final UUID targetId; @@ -76,7 +78,7 @@ public class FixedTarget implements TargetPointer { if (this.zoneChangeCounter > 0) { // will be zero if not defined in init Card card = game.getCard(targetId); if (card != null && card.getZoneChangeCounter(game) != this.zoneChangeCounter) { - return new ArrayList<>(); // return empty + return Collections.emptyList(); // return empty } }