Skip to content

Commit ca01859

Browse files
author
bytekeeper
committed
Use emptyList() were possible. Also, assign unmodifiableList as soon as possible instead of created in new ArrayLists to return or wrap in unmodifiableCollection in getters.
1 parent c214b3d commit ca01859

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

src/main/java/bwapi/Game.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ void init() {
201201
battleNet = gameData.isBattleNet();
202202
startLocations = IntStream.range(0, gameData.getStartLocationCount())
203203
.mapToObj(i -> new TilePosition(gameData.getStartLocations(i)))
204-
.collect(Collectors.toList());
204+
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
205205
mapWidth = gameData.getMapWidth();
206206
mapHeight = gameData.getMapHeight();
207207
mapFileName = gameData.getMapFileName();
@@ -264,9 +264,9 @@ void unitHide(final int id) {
264264

265265
void onFrame(final int frame) {
266266
if (frame > 0) {
267-
allUnits = Collections.unmodifiableList(visibleUnits.stream()
267+
allUnits = visibleUnits.stream()
268268
.map(i -> units[i])
269-
.collect(Collectors.toList()));
269+
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
270270
}
271271
getAllUnits().forEach(u -> u.updatePosition(frame));
272272
}
@@ -1010,7 +1010,7 @@ public boolean canUpgrade(final UpgradeType type, final Unit unit, final boolean
10101010
}
10111011

10121012
public List<TilePosition> getStartLocations() {
1013-
return new ArrayList<>(startLocations);
1013+
return startLocations;
10141014
}
10151015

10161016
public void printf(final String cstr_format) {

src/main/java/bwapi/Unit.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ public Unit getTransport() {
437437

438438
public List<Unit> getLoadedUnits() {
439439
if (getType().spaceProvided() < 1) {
440-
return new ArrayList<>();
440+
return Collections.emptyList();
441441
}
442442
return game.getAllUnits().stream()
443443
.filter(u -> equals(u.getTransport()))
@@ -460,7 +460,7 @@ public Unit getCarrier() {
460460

461461
public List<Unit> getInterceptors() {
462462
if (getType() != Protoss_Carrier && getType() != Hero_Gantrithor) {
463-
return new ArrayList<>();
463+
return Collections.emptyList();
464464
}
465465
return game.getAllUnits().stream()
466466
.filter(u -> equals(u.getCarrier()))
@@ -473,7 +473,7 @@ public Unit getHatchery() {
473473

474474
public List<Unit> getLarva() {
475475
if (!getType().producesLarva()) {
476-
return new ArrayList<>();
476+
return Collections.emptyList();
477477
}
478478
return game.getAllUnits().stream()
479479
.filter(u -> equals(u.getHatchery()))
@@ -482,7 +482,7 @@ public List<Unit> getLarva() {
482482

483483
public List<Unit> getUnitsInRadius(final int radius) {
484484
if (!exists()) {
485-
return new ArrayList<>();
485+
return Collections.emptyList();
486486
}
487487
return game.getUnitsInRectangle(
488488
getLeft() - radius,
@@ -495,7 +495,7 @@ public List<Unit> getUnitsInRadius(final int radius) {
495495
public List<Unit> getUnitsInWeaponRange(final WeaponType weapon) {
496496
// Return if this unit does not exist
497497
if (!exists()) {
498-
return new ArrayList<>();
498+
return Collections.emptyList();
499499
}
500500

501501
final int max = getPlayer().weaponMaxRange(weapon);

src/main/java/bwta/BWTA.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,31 @@ public static void analyze() {
3333
for (final Area a : bwem.getMap().getAreas()) {
3434
regionMap.put(a, new Region(a));
3535
}
36-
regions = new ArrayList<>(regionMap.values());
36+
regions = Collections.unmodifiableList(new ArrayList<>(regionMap.values()));
3737

3838
chokeMap = new HashMap<>();
3939
for (final ChokePoint c : bwem.getMap().getChokePoints()) {
4040
chokeMap.put(c, new Chokepoint(c));
4141
}
42-
chokepoints = new ArrayList<>(chokeMap.values());
42+
chokepoints = Collections.unmodifiableList(new ArrayList<>(chokeMap.values()));
4343

4444
baseMap = new HashMap<>();
4545
for (final Base b : bwem.getMap().getBases()) {
4646
baseMap.put(b, new BaseLocation(b));
4747
}
48-
baseLocations = new ArrayList<>(baseMap.values());
48+
baseLocations = Collections.unmodifiableList(new ArrayList<>(baseMap.values()));
4949
}
5050

5151
public static List<Region> getRegions() {
52-
return new ArrayList<>(regions);
52+
return regions;
5353
}
5454

5555
public static List<Chokepoint> getChokepoints() {
56-
return new ArrayList<>(chokepoints);
56+
return chokepoints;
5757
}
5858

5959
public static List<BaseLocation> getBaseLocations() {
60-
return new ArrayList<>(baseLocations);
60+
return baseLocations;
6161
}
6262

6363
public static List<BaseLocation> getStartLocations() {

src/main/java/bwta/BaseLocation.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package bwta;
22

3+
import static java.util.stream.Collectors.collectingAndThen;
4+
35
import bwapi.Position;
46
import bwapi.TilePosition;
57
import bwapi.Unit;
68
import bwem.Base;
79

10+
import bwem.unit.NeutralImpl;
811
import java.util.ArrayList;
12+
import java.util.Collections;
913
import java.util.List;
1014
import java.util.stream.Collectors;
1115

@@ -28,8 +32,10 @@ public class BaseLocation {
2832
this.tilePosition = base.getLocation();
2933
this.minerals = 1;
3034
this.gas = 1;
31-
this.mineralSet = base.getMinerals().stream().map(m -> m.getUnit()).collect(Collectors.toList());
32-
this.geyserSet = base.getGeysers().stream().map(g -> g.getUnit()).collect(Collectors.toList());
35+
this.mineralSet = base.getMinerals().stream().map(NeutralImpl::getUnit).collect(
36+
collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
37+
this.geyserSet = base.getGeysers().stream().map(NeutralImpl::getUnit).collect(
38+
collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
3339
this.island = base.getArea().getAccessibleNeighbors().isEmpty();
3440
this.mineralOnly = !mineralSet.isEmpty() && geyserSet.isEmpty();
3541
this.startLocation = base.isStartingLocation();
@@ -56,11 +62,11 @@ public int gas() {
5662
}
5763

5864
public List<Unit> getMinerals() {
59-
return new ArrayList<>(mineralSet);
65+
return mineralSet;
6066
}
6167

6268
public List<Unit> getGeysers() {
63-
return new ArrayList<>(geyserSet);
69+
return geyserSet;
6470
}
6571

6672
public double getGroundDistance(final BaseLocation other) {

0 commit comments

Comments
 (0)