From b0c1c0c894896ecb96c96a7c2bbbce3a4469ef64 Mon Sep 17 00:00:00 2001 From: asvitkine Date: Sun, 27 Aug 2023 16:09:51 -0400 Subject: [PATCH] Speed up canal validation code. --- .../move/validation/MoveValidator.java | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) 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..d5e9cbccab9 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 @@ -291,7 +291,7 @@ private MoveValidationResult validateCanal( */ public @Nullable String validateCanal( final Route route, @Nullable final Collection units, final GamePlayer player) { - return validateCanal(route, units, new HashMap<>(), player); + return validateCanal(route, units, Map.of(), player); } @Nullable @@ -300,32 +300,36 @@ String validateCanal( @Nullable final Collection units, final Map> airTransportDependents, final GamePlayer player) { + List canals = + route.getAllTerritories().stream() + .map(t -> CanalAttachment.get(t)) + .flatMap(Collection::stream) + // Only check canals that are on the route + .filter(c -> CanalAttachment.isCanalOnRoute(c.getCanalName(), route)) + .collect(Collectors.toList()); + if (canals.isEmpty()) { + return null; + } + final boolean mustControlAllCanals = + Properties.getControlAllCanalsBetweenTerritoriesToPass(data.getProperties()); // Check each unit 1 by 1 to see if they can move through necessary canals on route String result = null; final Set unitsThatFailCanal = new HashSet<>(); - final Set setWithNull = new HashSet<>(); - setWithNull.add(null); final Collection unitsWithoutDependents = - (units == null) ? setWithNull : findNonDependentUnits(units, route, airTransportDependents); + (units == null) + ? Set.of((Unit) null) + : findNonDependentUnits(units, route, airTransportDependents); for (final Unit unit : unitsWithoutDependents) { - for (final Territory t : route.getAllTerritories()) { - Optional failureMessage = Optional.empty(); - for (final CanalAttachment canalAttachment : CanalAttachment.get(t)) { - if (!CanalAttachment.isCanalOnRoute(canalAttachment.getCanalName(), route)) { - continue; // Only check canals that are on the route - } - failureMessage = canPassThroughCanal(canalAttachment, unit, player); - final boolean canPass = failureMessage.isEmpty(); - final boolean mustControlAllCanals = - Properties.getControlAllCanalsBetweenTerritoriesToPass(data.getProperties()); - if (mustControlAllCanals != canPass) { - // If need to control any canal and can pass OR need to control all and can't pass. - break; + for (CanalAttachment canalAttachment : canals) { + Optional failureMessage = canPassThroughCanal(canalAttachment, unit, player); + final boolean canPass = failureMessage.isEmpty(); + if (mustControlAllCanals != canPass) { + // If need to control any canal and can pass OR need to control all and can't pass. + if (!canPass) { + result = failureMessage.get(); + unitsThatFailCanal.add(unit); } - } - if (failureMessage.isPresent()) { - result = failureMessage.get(); - unitsThatFailCanal.add(unit); + break; } } }