From b3e6d092e40bf569245af52db806fde90e8dc231 Mon Sep 17 00:00:00 2001 From: asvitkine Date: Sun, 27 Aug 2023 16:09:05 -0400 Subject: [PATCH] Reduce use of expensive getTransporting(). (#11913) * Reduce use of expensive getTransporting(). * Remove now unnecessary stubbing. * Removed unused imports. --- .../triplea/delegate/MovePerformer.java | 13 ++++++----- .../move/validation/MoveValidator.java | 22 ++++--------------- .../retreat/DefensiveSubsRetreatTest.java | 9 -------- 3 files changed, 12 insertions(+), 32 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/delegate/MovePerformer.java b/game-app/game-core/src/main/java/games/strategy/triplea/delegate/MovePerformer.java index e56aa2e9048..0beb72aaf1d 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/delegate/MovePerformer.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/delegate/MovePerformer.java @@ -385,10 +385,13 @@ private void markTransportsMovement( final boolean paratroopsLanding = arrived.stream().anyMatch(paratroopNAirTransports) && MoveValidator.allLandUnitsAreBeingParatroopered(arrived); - final Map> dependentAirTransportableUnits = - new HashMap<>( - MoveValidator.getDependents( - CollectionUtils.getMatches(arrived, Matches.unitCanTransport()))); + final Map> dependentAirTransportableUnits = new HashMap<>(); + for (final Unit unit : arrived) { + Unit transport = unit.getTransportedBy(); + if (transport != null) { + dependentAirTransportableUnits.computeIfAbsent(transport, u -> new ArrayList<>()).add(unit); + } + } // add newly created dependents for (final Entry> entry : airTransportDependents.entrySet()) { Collection dependents = dependentAirTransportableUnits.get(entry.getKey()); @@ -414,7 +417,7 @@ private void markTransportsMovement( // mark transports as having transported for (final Unit load : transporting.keySet()) { final Unit transport = transporting.get(load); - if (!transport.getTransporting().contains(load)) { + if (!transport.equals(load.getTransportedBy())) { final Change change = TransportTracker.loadTransportChange(transport, load); currentMove.addChange(change); currentMove.load(transport); diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/delegate/move/validation/MoveValidator.java b/game-app/game-core/src/main/java/games/strategy/triplea/delegate/move/validation/MoveValidator.java index 554b9f30f6f..fc11c5a4103 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/delegate/move/validation/MoveValidator.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/delegate/move/validation/MoveValidator.java @@ -865,17 +865,12 @@ private static Collection findNonDependentUnits( final Collection units, final Route route, final Map> airTransportDependents) { - final Map> dependentsMap = - getDependents(CollectionUtils.getMatches(units, Matches.unitCanTransport())); - final Set dependents = - dependentsMap.values().stream().flatMap(Collection::stream).collect(Collectors.toSet()); - dependents.addAll( - airTransportDependents.values().stream() - .flatMap(Collection::stream) - .collect(Collectors.toSet())); final Collection unitsWithoutDependents = new ArrayList<>(); unitsWithoutDependents.addAll(route.getStart().isWater() ? getNonLand(units) : units); - unitsWithoutDependents.removeAll(dependents); + unitsWithoutDependents.removeIf(u -> u.getTransportedBy() != null); + for (Collection deps : airTransportDependents.values()) { + unitsWithoutDependents.removeAll(deps); + } return unitsWithoutDependents; } @@ -943,15 +938,6 @@ private static IntegerMap getLandTransportsWithCapacity( return map; } - public static Map> getDependents(final Collection units) { - // just worry about transports - final Map> dependents = new HashMap<>(); - for (final Unit unit : units) { - dependents.put(unit, unit.getTransporting()); - } - return dependents; - } - /** * Checks that there are no enemy units on the route except possibly at the end. Submerged enemy * units are not considered as they don't affect movement. AA and factory dont count as enemy. diff --git a/game-app/game-core/src/test/java/games/strategy/triplea/delegate/battle/steps/retreat/DefensiveSubsRetreatTest.java b/game-app/game-core/src/test/java/games/strategy/triplea/delegate/battle/steps/retreat/DefensiveSubsRetreatTest.java index 8cdc8eef8f7..b0535f25bfd 100644 --- a/game-app/game-core/src/test/java/games/strategy/triplea/delegate/battle/steps/retreat/DefensiveSubsRetreatTest.java +++ b/game-app/game-core/src/test/java/games/strategy/triplea/delegate/battle/steps/retreat/DefensiveSubsRetreatTest.java @@ -1,6 +1,5 @@ package games.strategy.triplea.delegate.battle.steps.retreat; -import static games.strategy.triplea.Constants.UNIT_ATTACHMENT_NAME; import static games.strategy.triplea.delegate.battle.BattleState.Side.DEFENSE; import static games.strategy.triplea.delegate.battle.FakeBattleState.givenBattleStateBuilder; import static games.strategy.triplea.delegate.battle.steps.BattleStepsTest.givenUnitCanEvade; @@ -28,7 +27,6 @@ import games.strategy.engine.delegate.IDelegateBridge; import games.strategy.engine.display.IDisplay; import games.strategy.engine.history.IDelegateHistoryWriter; -import games.strategy.triplea.attachments.UnitAttachment; import games.strategy.triplea.delegate.ExecutionStack; import games.strategy.triplea.delegate.battle.BattleActions; import games.strategy.triplea.delegate.battle.BattleState; @@ -225,9 +223,6 @@ void retreatHappensWhenNotSubmersibleButHasRetreatTerritories() { final GameData gameData = givenGameData().build(); final Unit unit = givenRealUnitCanEvade(gameData, defender); - final UnitAttachment unitAttachment = - (UnitAttachment) unit.getType().getAttachment(UNIT_ATTACHMENT_NAME); - when(unitAttachment.getTransportCapacity()).thenReturn(-1); final Collection retreatingUnits = List.of(unit); final Territory retreatTerritory = mock(Territory.class); @@ -265,7 +260,6 @@ void retreatHappensWhenNotSubmersibleButHasRetreatTerritories() { @Test void retreatHappensWhenDefendingIsSubmersibleAndHasRetreatTerritories() { - final Territory retreatTerritory = mock(Territory.class); when(retreatTerritory.isWater()).thenReturn(true); final UnitCollection retreatTerritoryCollection = mock(UnitCollection.class); @@ -280,9 +274,6 @@ void retreatHappensWhenDefendingIsSubmersibleAndHasRetreatTerritories() { .build(); final Unit unit = givenRealUnitCanEvade(gameData, defender); - final UnitAttachment unitAttachment = - (UnitAttachment) unit.getType().getAttachment(UNIT_ATTACHMENT_NAME); - when(unitAttachment.getTransportCapacity()).thenReturn(-1); final Collection retreatingUnits = List.of(unit); final BattleState battleState =