diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/UnitAttachment.java b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/UnitAttachment.java index bc2a29f2292..43a1da55879 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/UnitAttachment.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/UnitAttachment.java @@ -908,6 +908,13 @@ public String[] getUnitPlacementRestrictions() { return unitPlacementRestrictions; } + public boolean unitPlacementRestrictionsContain(Territory territory) { + if (unitPlacementRestrictions == null) { + return false; + } + return Arrays.asList(unitPlacementRestrictions).contains(territory.getName()); + } + private void resetUnitPlacementRestrictions() { unitPlacementRestrictions = null; } diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/delegate/AbstractPlaceDelegate.java b/game-app/game-core/src/main/java/games/strategy/triplea/delegate/AbstractPlaceDelegate.java index d01a168e984..61b7277b57c 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/delegate/AbstractPlaceDelegate.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/delegate/AbstractPlaceDelegate.java @@ -28,6 +28,7 @@ import java.util.Collection; import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -459,7 +460,7 @@ private void freePlacementCapacity( boolean unusedSplitPlacements = false; if (foundSpaceTotal < freeSize) { // we need to split some placement moves - final Collection usedUndoablePlacements = new ArrayList<>(); + final var usedUndoablePlacements = new HashSet(); for (final Tuple tuple : splitPlacements) { final UndoablePlacement placement = tuple.getFirst(); if (usedUndoablePlacements.contains(placement)) { @@ -880,9 +881,7 @@ public String canUnitsBePlaced( + to.getName() + " due to Unit Placement Restrictions on Territory Value"; } - final String[] terrs = ua.getUnitPlacementRestrictions(); - final Collection listedTerrs = getListedTerritories(terrs); - if (listedTerrs.contains(to)) { + if (ua.unitPlacementRestrictionsContain(to)) { return "Cannot place these units in " + to.getName() + " due to Unit Placement Restrictions"; @@ -945,7 +944,7 @@ protected Collection getUnitsToBePlaced( if (units.stream().anyMatch(Matches.unitIsConstruction())) { final IntegerMap constructionsMap = howManyOfEachConstructionCanPlace(to, to, units, player); - final Collection skipUnits = new ArrayList<>(); + final var skipUnits = new HashSet(); for (final Unit currentUnit : CollectionUtils.getMatches(units, Matches.unitIsConstruction())) { final int maxUnits = howManyOfConstructionUnit(currentUnit, constructionsMap); @@ -979,7 +978,7 @@ protected Collection getUnitsToBePlaced( } // now check stacking limits final Collection placeableUnits2 = new ArrayList<>(); - final Collection typesAlreadyChecked = new ArrayList<>(); + final var typesAlreadyChecked = new HashSet(); for (final Unit currentUnit : placeableUnits) { final UnitType ut = currentUnit.getType(); if (typesAlreadyChecked.contains(ut)) { @@ -1013,9 +1012,7 @@ protected Collection getUnitsToBePlaced( continue; } // account for any unit placement restrictions by territory - final String[] terrs = ua.getUnitPlacementRestrictions(); - final Collection listedTerrs = getListedTerritories(terrs); - if (!listedTerrs.contains(to)) { + if (!ua.unitPlacementRestrictionsContain(to)) { placeableUnits3.add(currentUnit); } } @@ -1367,9 +1364,7 @@ IntegerMap howManyOfEachConstructionCanPlace( final UnitAttachment ua = currentUnit.getUnitAttachment(); // account for any unit placement restrictions by territory if (Properties.getUnitPlacementRestrictions(getData().getProperties())) { - final String[] terrs = ua.getUnitPlacementRestrictions(); - final Collection listedTerrs = getListedTerritories(terrs); - if (listedTerrs.contains(to)) { + if (ua.unitPlacementRestrictionsContain(to)) { continue; } if (ua.getCanOnlyBePlacedInTerritoryValuedAtX() != -1 @@ -1744,22 +1739,6 @@ private static boolean isPlacementInCapitalRestricted(final GamePlayer player) { return ra != null && ra.getPlacementInCapitalRestricted(); } - private Collection getListedTerritories(final String[] list) { - final List territories = new ArrayList<>(); - if (list == null) { - return territories; - } - for (final String name : list) { - // Validate all territories exist - final Territory territory = getData().getMap().getTerritory(name); - if (territory == null) { - throw new IllegalStateException("Rules & Conditions: No territory called:" + name); - } - territories.add(territory); - } - return territories; - } - @Override public Class getRemoteType() { return IAbstractPlaceDelegate.class;