From 45f665eb1d166337d158d4a503342467716af6fc Mon Sep 17 00:00:00 2001 From: Ingmar Goudt Date: Mon, 4 Mar 2019 10:43:20 +0100 Subject: [PATCH] Sonar cleanups 03032019 --- .../src/main/java/mage/client/cards/CardsList.java | 6 ++++-- .../java/mage/client/game/BattlefieldPanel.java | 6 +----- .../util/CardViewEDHPowerLevelComparator.java | 4 ++-- .../java/org/mage/card/arcane/ManaSymbols.java | 2 +- .../card/dl/sources/TokensMtgImageSource.java | 10 +++++----- .../card/dl/sources/WizardCardsImageSource.java | 6 +++--- .../src/main/java/mage/remote/SessionImpl.java | 2 +- .../java/mage/server/util/ServerMessagesUtil.java | 5 +---- Mage.Sets/src/mage/cards/b/BaneOfBalaGed.java | 14 +++++++------- Mage.Sets/src/mage/cards/c/CentaurPeacemaker.java | 12 ++++++------ Mage.Sets/src/mage/cards/k/KavuPredator.java | 2 +- Mage/src/main/java/mage/game/draft/RateCard.java | 7 ++++--- Mage/src/main/java/mage/util/JarVersion.java | 2 +- 13 files changed, 37 insertions(+), 41 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/cards/CardsList.java b/Mage.Client/src/main/java/mage/client/cards/CardsList.java index 31b5b2b688..ab9542ef27 100644 --- a/Mage.Client/src/main/java/mage/client/cards/CardsList.java +++ b/Mage.Client/src/main/java/mage/client/cards/CardsList.java @@ -270,12 +270,14 @@ public class CardsList extends javax.swing.JPanel implements MouseListener, ICar mageCards = new LinkedHashMap<>(); //Find card view - for (UUID uuid : cards.keySet()) { + for (Map.Entry view : cards.entrySet()) { + UUID uuid = view.getKey(); + CardView cardView = view.getValue(); if (oldMageCards.containsKey(uuid)) { mageCards.put(uuid, oldMageCards.get(uuid)); oldMageCards.remove(uuid); } else { - mageCards.put(uuid, addCard(cards.get(uuid), bigCard, gameId)); + mageCards.put(uuid, addCard(cardView, bigCard, gameId)); } } //Remove unused cards diff --git a/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java b/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java index 89025e02cf..08a6e2a41e 100644 --- a/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java +++ b/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java @@ -238,11 +238,7 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane { cardDimension = new Dimension(Config.dimensions.getFrameWidth(), Config.dimensions.getFrameHeight()); } final MagePermanent perm = Plugins.instance.getMagePermanent(permanent, bigCard, cardDimension, gameId, true); - if (!Plugins.instance.isCardPluginLoaded()) { - //perm.setBounds(findEmptySpace(new Dimension(Config.dimensions.frameWidth, Config.dimensions.frameHeight))); - } else { - //perm.setAlpha(0); - } + permanents.put(permanent.getId(), perm); BattlefieldPanel.this.jPanel.add(perm, 10); diff --git a/Mage.Client/src/main/java/mage/client/util/CardViewEDHPowerLevelComparator.java b/Mage.Client/src/main/java/mage/client/util/CardViewEDHPowerLevelComparator.java index 31239d1d95..9d4f614020 100644 --- a/Mage.Client/src/main/java/mage/client/util/CardViewEDHPowerLevelComparator.java +++ b/Mage.Client/src/main/java/mage/client/util/CardViewEDHPowerLevelComparator.java @@ -428,7 +428,7 @@ public class CardViewEDHPowerLevelComparator implements Comparator { || cn.equals("krosan restorer") || cn.equals("laboratory maniac") || cn.equals("leovold, emissary of trest") || cn.equals("leonin relic-warder") || cn.equals("leyline of the void") - || cn.equals("memnarch") || cn.equals("memnarch") + || cn.equals("memnarch") || cn.equals("meren of clan nel toth") || cn.equals("mikaeus, the unhallowed") || cn.equals("mindcrank") || cn.equals("mindslaver") || cn.equals("minion reflector") || cn.equals("mycosynth lattice") @@ -446,7 +446,7 @@ public class CardViewEDHPowerLevelComparator implements Comparator { || cn.equals("sunder") || cn.equals("storm cauldron") || cn.equals("teferi's puzzle box") || cn.equals("tangle wire") - || cn.equals("teferi, mage of zhalfir") || cn.equals("teferi, mage of zhalfir") + || cn.equals("teferi, mage of zhalfir") || cn.equals("tezzeret the seeker") || cn.equals("time stretch") || cn.equals("time warp") || cn.equals("training grounds") || cn.equals("triskelavus") || cn.equals("triskelion") diff --git a/Mage.Client/src/main/java/org/mage/card/arcane/ManaSymbols.java b/Mage.Client/src/main/java/org/mage/card/arcane/ManaSymbols.java index 0a847bf92a..75d361cbf7 100644 --- a/Mage.Client/src/main/java/org/mage/card/arcane/ManaSymbols.java +++ b/Mage.Client/src/main/java/org/mage/card/arcane/ManaSymbols.java @@ -180,7 +180,7 @@ public final class ManaSymbols { codes = EnumSet.of(Rarity.COMMON, Rarity.UNCOMMON, Rarity.RARE, Rarity.MYTHIC); } - Map rarityImages = new HashMap<>(); + Map rarityImages = new EnumMap<>(Rarity.class); setImages.put(set, rarityImages); // load medium size diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/TokensMtgImageSource.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/TokensMtgImageSource.java index 525361ff62..4f8fc8fd10 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/TokensMtgImageSource.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/TokensMtgImageSource.java @@ -25,7 +25,7 @@ public enum TokensMtgImageSource implements CardImageSource { private static final Logger logger = Logger.getLogger(TokensMtgImageSource.class); // [[EXP/Name, TokenData> - private HashMap> tokensData; + private HashMap> tokensData; private static final Set supportedSets = new LinkedHashSet(); private final Object tokensDataSync = new Object(); @@ -177,7 +177,7 @@ public enum TokensMtgImageSource implements CardImageSource { return false; } - private HashMap> getTokensData() throws IOException { + private HashMap> getTokensData() throws IOException { synchronized (tokensDataSync) { if (tokensData == null) { DownloadPicturesService.getInstance().updateAndViewMessage("Find tokens data..."); @@ -188,7 +188,7 @@ public enum TokensMtgImageSource implements CardImageSource { List fileTokensData = parseTokensData(inputStream); for (TokenData tokenData : fileTokensData) { String key = tokenData.getExpansionSetCode() + "/" + tokenData.getName(); - ArrayList list = tokensData.get(key); + List list = tokensData.get(key); if (list == null) { list = new ArrayList<>(); tokensData.put(key, list); @@ -213,7 +213,7 @@ public enum TokensMtgImageSource implements CardImageSource { // logger.info("TOK: " + siteData.getExpansionSetCode() + "/" + siteData.getName()); String key = siteData.getExpansionSetCode() + "/" + siteData.getName(); supportedSets.add(siteData.getExpansionSetCode()); - ArrayList list = tokensData.get(key); + List list = tokensData.get(key); if (list == null) { list = new ArrayList<>(); tokensData.put(key, list); @@ -249,7 +249,7 @@ public enum TokensMtgImageSource implements CardImageSource { BufferedReader reader = new BufferedReader(inputReader)) { // we have to specify encoding to read special comma - reader.readLine(); // skip header + String header = reader.readLine(); // skip header String line = reader.readLine(); // states // 0 - wait set name diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java index 0f335d7e9e..0d434ec1c0 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java @@ -40,7 +40,7 @@ public enum WizardCardsImageSource implements CardImageSource { WizardCardsImageSource() { - languageAliases = new HashMap<>(); + languageAliases = new EnumMap<>(CardLanguage.class); languageAliases.put(CardLanguage.ENGLISH, "English"); languageAliases.put(CardLanguage.SPANISH, "Spanish"); languageAliases.put(CardLanguage.FRENCH, "French"); @@ -544,9 +544,9 @@ public enum WizardCardsImageSource implements CardImageSource { getLandVariations(setLinks, cardSet, multiverseId, cardName); } else { String numberChar = ""; - int pos1 = cardName.indexOf("("); + int pos1 = cardName.indexOf('('); if (pos1 > 0) { - int pos2 = cardName.indexOf("(", pos1 + 1); + int pos2 = cardName.indexOf('(', pos1 + 1); if (pos2 > 0) { numberChar = cardName.substring(pos2 + 1, pos2 + 2); cardName = cardName.substring(0, pos1); diff --git a/Mage.Common/src/main/java/mage/remote/SessionImpl.java b/Mage.Common/src/main/java/mage/remote/SessionImpl.java index c281822a9c..b9045da1b0 100644 --- a/Mage.Common/src/main/java/mage/remote/SessionImpl.java +++ b/Mage.Common/src/main/java/mage/remote/SessionImpl.java @@ -119,7 +119,7 @@ public class SessionImpl implements Session { if (ex.getMessage() != null && ex.getMessage().startsWith("Unable to perform invocation")) { addMessage = "Maybe the server version is not compatible. "; } - client.showMessage("Unable connect to server. " + addMessage + ex.getMessage() != null ? ex.getMessage() : ""); + client.showMessage("Unable connect to server. " + addMessage + (ex.getMessage() != null ? ex.getMessage() : "")); } catch (MageVersionException ex) { if (!canceled) { client.showMessage("Unable connect to server. " + ex.getMessage()); diff --git a/Mage.Server/src/main/java/mage/server/util/ServerMessagesUtil.java b/Mage.Server/src/main/java/mage/server/util/ServerMessagesUtil.java index 942718882d..b80e851806 100644 --- a/Mage.Server/src/main/java/mage/server/util/ServerMessagesUtil.java +++ b/Mage.Server/src/main/java/mage/server/util/ServerMessagesUtil.java @@ -121,10 +121,8 @@ public enum ServerMessagesUtil { return Collections.emptyList(); } - Scanner scanner = null; List newMessages = new ArrayList<>(); - try { - scanner = new Scanner(is); + try(Scanner scanner = new Scanner(is)) { while (scanner.hasNextLine()) { String message = scanner.nextLine(); if (!message.trim().isEmpty()) { @@ -134,7 +132,6 @@ public enum ServerMessagesUtil { } catch (Exception e) { log.error(e, e); } finally { - StreamUtils.closeQuietly(scanner); StreamUtils.closeQuietly(is); } return newMessages; diff --git a/Mage.Sets/src/mage/cards/b/BaneOfBalaGed.java b/Mage.Sets/src/mage/cards/b/BaneOfBalaGed.java index 34c6ee3db4..7df9d2b0a3 100644 --- a/Mage.Sets/src/mage/cards/b/BaneOfBalaGed.java +++ b/Mage.Sets/src/mage/cards/b/BaneOfBalaGed.java @@ -2,8 +2,11 @@ package mage.cards.b; import java.util.HashSet; +import java.util.Objects; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; + import mage.MageInt; import mage.abilities.Ability; import mage.abilities.common.AttacksTriggeredAbility; @@ -65,13 +68,10 @@ class BaneOfBalaGedEffect extends OneShotEffect { if (defendingPlayer != null) { Target target = new TargetControlledPermanent(2); defendingPlayer.chooseTarget(outcome, target, source, game); - Set toExile = new HashSet<>(); - target.getTargets().stream().map((targetId) - -> game.getPermanent(targetId)).filter((permanent) - -> (permanent != null)).forEach((permanent) - -> { - toExile.add(permanent); - }); + Set toExile = target.getTargets().stream() + .map(game::getPermanent) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); defendingPlayer.moveCards(toExile, Zone.EXILED, source, game); return true; } diff --git a/Mage.Sets/src/mage/cards/c/CentaurPeacemaker.java b/Mage.Sets/src/mage/cards/c/CentaurPeacemaker.java index d99c19d977..ebd089f421 100644 --- a/Mage.Sets/src/mage/cards/c/CentaurPeacemaker.java +++ b/Mage.Sets/src/mage/cards/c/CentaurPeacemaker.java @@ -1,5 +1,6 @@ package mage.cards.c; +import java.util.Objects; import java.util.UUID; import mage.MageInt; import mage.abilities.Ability; @@ -61,12 +62,11 @@ class CentaurMediatorEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { game.getState().getPlayersInRange( - source.getControllerId(), game - ).stream().map((playerId) -> game.getPlayer(playerId)).filter( - (player) -> (player != null) - ).forEachOrdered((player) -> { - player.gainLife(4, game, source); - }); + source.getControllerId(), game) + .stream() + .map(game::getPlayer) + .filter(Objects::nonNull) + .forEachOrdered(player -> player.gainLife(4, game, source)); return true; } diff --git a/Mage.Sets/src/mage/cards/k/KavuPredator.java b/Mage.Sets/src/mage/cards/k/KavuPredator.java index d2f829717d..5bf6feeb58 100644 --- a/Mage.Sets/src/mage/cards/k/KavuPredator.java +++ b/Mage.Sets/src/mage/cards/k/KavuPredator.java @@ -72,7 +72,7 @@ class KavuPredatorTriggeredAbility extends TriggeredAbilityImpl { @Override public boolean checkTrigger(GameEvent event, Game game) { if (game.getOpponents(this.controllerId).contains(event.getPlayerId())) { - this.getEffects().get(0).setValue("gainedLife", new Integer(event.getAmount())); + this.getEffects().get(0).setValue("gainedLife", event.getAmount()); return true; } return false; diff --git a/Mage/src/main/java/mage/game/draft/RateCard.java b/Mage/src/main/java/mage/game/draft/RateCard.java index 6b6e444b9e..aa83b50052 100644 --- a/Mage/src/main/java/mage/game/draft/RateCard.java +++ b/Mage/src/main/java/mage/game/draft/RateCard.java @@ -289,8 +289,9 @@ public final class RateCard { } // normalize for the file to [1..100] - for (String name : thisFileRatings.keySet()) { - int r = thisFileRatings.get(name); + for (Map.Entry ratingByName : thisFileRatings.entrySet()) { + int r = ratingByName.getValue(); + String name = ratingByName.getKey(); int newRating = (int) (100.0f * (r - min) / (max - min)); int oldRating = baseRatings.getOrDefault(name, 0); if (newRating > oldRating) { @@ -339,7 +340,7 @@ public final class RateCard { } Integer typeCount = singleCount.get(symbol); if (typeCount == null) { - typeCount = new Integer(0); + typeCount = 0; } typeCount += 1; singleCount.put(symbol, typeCount); diff --git a/Mage/src/main/java/mage/util/JarVersion.java b/Mage/src/main/java/mage/util/JarVersion.java index 5c494ed13b..3089b441ab 100644 --- a/Mage/src/main/java/mage/util/JarVersion.java +++ b/Mage/src/main/java/mage/util/JarVersion.java @@ -28,7 +28,7 @@ public class JarVersion { String manifestPath; if (classPath.startsWith("jar")) { // jar source - manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF"; + manifestPath = classPath.substring(0, classPath.lastIndexOf('!') + 1) + "/META-INF/MANIFEST.MF"; } else { // dir source (e.g. IDE's debug) // it's can be generated by runtime, but need extra code and performance: https://stackoverflow.com/questions/34674073/how-to-generate-manifest-mf-file-during-compile-phase