Skip to content

Commit

Permalink
Various code clean ups. (#11901)
Browse files Browse the repository at this point in the history
No functional changes.

Clean ups:

- Rewrap some comments that were poorly wrapped due to past code reformatting.
- Use GridBagConstraintsBuilder in BattleCalculatorPanel to make the code much more concise
- Switch some countMatches() calls to noneMatch() and anyMatch().
- Remove some unnecessary logic prior to calling getMaxUnitsToBePlacedFrom(), since getMaxUnitsToBePlacedFrom() does that logic already.
  • Loading branch information
asvitkine authored Aug 22, 2023
1 parent 26d7866 commit 4ad1583
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 687 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1074,15 +1074,10 @@ private IntegerMap<Territory> getMaxUnitsToBePlacedMap(
final Collection<Territory> notUsableAsOtherProducers = new ArrayList<>(producers);
final Map<Territory, Integer> currentAvailablePlacementForOtherProducers = new HashMap<>();
for (final Territory producerTerritory : producers) {
final Collection<Unit> unitsCanBePlacedByThisProducer =
(Properties.getUnitPlacementRestrictions(getData().getProperties())
? CollectionUtils.getMatches(
units, unitWhichRequiresUnitsHasRequiredUnits(producerTerritory, true))
: new ArrayList<>(units));
final int prodT =
getMaxUnitsToBePlacedFrom(
producerTerritory,
unitsCanBePlacedByThisProducer,
units,
to,
player,
true,
Expand Down Expand Up @@ -1158,8 +1153,7 @@ units, unitWhichRequiresUnitsHasRequiredUnits(producer, true))
return Math.max(0, ra.getMaxPlacePerTerritory() - unitCountAlreadyProduced);
}
// a factory can produce the same number of units as the number of PUs the territory generates
// each turn (or not, if
// it has canProduceXUnits)
// each turn (or not, if it has canProduceXUnits)
final int maxConstructions =
howManyOfEachConstructionCanPlace(to, producer, unitsCanBePlacedByThisProducer, player)
.totalValues();
Expand Down Expand Up @@ -1601,7 +1595,7 @@ public Collection<Unit> unitsAtStartOfStepInTerritory(final Territory to) {
return new ArrayList<>();
}
final Collection<Unit> unitsPlacedAlready = getAlreadyProduced(to);
if (Matches.territoryIsWater().test(to)) {
if (to.isWater()) {
for (final Territory current : getAllProducers(to, player, null, true)) {
unitsPlacedAlready.addAll(getAlreadyProduced(current));
}
Expand Down Expand Up @@ -1637,7 +1631,7 @@ private boolean wasOwnedUnitThatCanProduceUnitsOrIsFactoryInTerritoryAtStartOfSt
// land factories in water can't produce, and sea factories in land can't produce.
// air can produce like land if in land, and like sea if in water.
.and(to.isWater() ? Matches.unitIsLand().negate() : Matches.unitIsSea().negate());
return CollectionUtils.countMatches(unitsAtStartOfTurnInTo, factoryMatch) > 0;
return unitsAtStartOfTurnInTo.stream().anyMatch(factoryMatch);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ private void defenderWins(final IDelegateBridge bridge) {
whoWon = WhoWon.DEFENDER;
bridge.getDisplayChannelBroadcaster().battleEnd(battleId, defender.getName() + " win");
if (Properties.getAbandonedTerritoriesMayBeTakenOverImmediately(gameData.getProperties())) {
if (CollectionUtils.countMatches(defendingUnits, Matches.unitIsNotInfrastructure()) == 0) {
if (defendingUnits.stream().noneMatch(Matches.unitIsNotInfrastructure())) {
final List<Unit> allyOfAttackerUnits =
battleSite.getUnitCollection().getMatches(Matches.unitIsNotInfrastructure());
if (!allyOfAttackerUnits.isEmpty()) {
Expand All @@ -1378,14 +1378,12 @@ private void defenderWins(final IDelegateBridge bridge) {
+ " as there are no defenders left",
allyOfAttackerUnits);
// should we create a new battle records to show the ally capturing the territory (in the
// case where they
// didn't already own/allied it)?
// case where they didn't already own/allied it)?
battleTracker.takeOver(battleSite, abandonedToPlayer, bridge, null, allyOfAttackerUnits);
}
} else {
// should we create a new battle records to show the defender capturing the territory (in
// the case where they
// didn't already own/allied it)?
// the case where they didn't already own/allied it)?
battleTracker.takeOver(battleSite, defender, bridge, null, defendingUnits);
}
}
Expand Down
Loading

0 comments on commit 4ad1583

Please sign in to comment.