Skip to content

Commit

Permalink
Remove some more unneeded collection copies. (#12341)
Browse files Browse the repository at this point in the history
* Remove some more unneeded collection copies.

* Remove import.
  • Loading branch information
asvitkine authored Feb 15, 2024
1 parent 25b5433 commit 9da858f
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package games.strategy.engine.data;

import com.google.common.annotations.VisibleForTesting;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/** A collection of {@link Resource}s keyed on the resource name. */
Expand All @@ -29,7 +29,7 @@ public Resource getResource(final String name) {
return resources.get(name);
}

public List<Resource> getResources() {
return new ArrayList<>(resources.values());
public Collection<Resource> getResources() {
return Collections.unmodifiableCollection(resources.values());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import games.strategy.engine.history.Round;
import games.strategy.triplea.ui.mapdata.MapData;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -45,7 +46,7 @@ private static GameData cloneGameData(GameData gameData) {
}

private static Map<OverTimeStatisticType, IStat> createOverTimeStatisticsMapping(
final List<Resource> resources) {
final Collection<Resource> resources) {
final Map<OverTimeStatisticType, IStat> statisticsMapping =
new HashMap<>(defaultStatisticsMapping);
resources.forEach(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static java.util.function.Predicate.not;

import com.google.common.collect.ImmutableList;
import games.strategy.engine.data.GamePlayer;
import games.strategy.engine.data.GameState;
import games.strategy.engine.data.Territory;
Expand Down Expand Up @@ -247,7 +246,7 @@ public static List<Unit> findBestUnitsToLandTransport(
.thenComparing(getDecreasingAttackComparator(player)));
results.addAll(selectUnitsToTransportFromList(unit, units));
}
return ImmutableList.copyOf(results);
return Collections.unmodifiableList(results);
}

private static Comparator<Unit> getDecreasingAttackComparator(final GamePlayer player) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package games.strategy.triplea.delegate;

import com.google.common.collect.ImmutableList;
import games.strategy.engine.data.GameData;
import games.strategy.engine.data.GamePlayer;
import games.strategy.engine.data.Route;
Expand Down Expand Up @@ -279,7 +278,7 @@ Collection<Territory> getTerritoriesWhereAaWillFire(
territoriesWhereAaWillFire.add(route.getStart());
}
}
return ImmutableList.copyOf(territoriesWhereAaWillFire);
return Collections.unmodifiableList(territoriesWhereAaWillFire);
}

private BattleTracker getBattleTracker() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Collection;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
Expand Down Expand Up @@ -54,7 +54,7 @@ public JPanel getResourcesPanel(final ResourceCollection resources, final GamePl
private JPanel getResourcesPanel(
final ResourceCollection resources, final boolean showEmpty, final GamePlayer player) {
final JPanel resourcePanel = new JPanel();
final List<Resource> resourcesInOrder;
final Collection<Resource> resourcesInOrder;
final GameData data = resources.getData();
try (GameData.Unlocker ignored = data.acquireReadLock()) {
resourcesInOrder = data.getResourceList().getResources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public IStat[] getStatsExtended(final GameState data) {

private void fillExtendedStats(final GameState data) {
// add other resources, other than PUs and tech tokens
final List<Resource> resources = data.getResourceList().getResources();
for (final Resource r : resources) {
for (final Resource r : data.getResourceList().getResources()) {
if (r.getName().equals(Constants.PUS) || r.getName().equals(Constants.TECH_TOKENS)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package games.strategy.engine.framework.startup.ui;

import com.google.common.collect.ImmutableList;
import games.strategy.engine.data.GamePlayer;
import games.strategy.engine.framework.network.ui.BanPlayerAction;
import games.strategy.engine.framework.network.ui.BootPlayerAction;
Expand Down Expand Up @@ -525,7 +524,7 @@ public List<Action> getUserActions() {
actions.add(new EditGameCommentAction(watcher, ServerSetupPanel.this));
actions.add(new RemoveGameFromLobbyAction(watcher));
});
return ImmutableList.copyOf(actions);
return actions;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,9 @@ private List<Unit> chooseUnitsToUnload(
}
}
}
return ImmutableList.copyOf(selectedUnitsToUnload);
// Return an unmodifiable list for consistent semantics for the caller, which sometimes returns
// List.of() and sometimes the result of this functiom.
return Collections.unmodifiableList(selectedUnitsToUnload);
}

public boolean confirmUnitChooserDialog(final UnitChooser chooser, final String title) {
Expand Down

0 comments on commit 9da858f

Please sign in to comment.