Skip to content

Commit

Permalink
refactor: Add function for streaming bay weapons
Browse files Browse the repository at this point in the history
WeaponMounted.getBayWeapons() iterates through the entire bay before
returning, which isn't always necessary. It has been refactored to allow
for the stream to be used directly.
  • Loading branch information
Saklad5 committed Aug 18, 2024
1 parent 4858c16 commit 175a3f1
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions megamek/src/megamek/common/equipment/WeaponMounted.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Objects;
import java.util.Vector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class WeaponMounted extends Mounted<WeaponType> {

Expand Down Expand Up @@ -207,12 +208,19 @@ public void clearBayWeapons() {
}

/**
* @return All the weapon mounts in the bay.
* @return A stream containing the weapon mounts in the bay.
*/
public List<WeaponMounted> getBayWeapons() {
public Stream<WeaponMounted> streamBayWeapons() {
return bayWeapons.stream()
.map(i -> getEntity().getWeapon(i))
.filter(Objects::nonNull)
.filter(Objects::nonNull);
}

/**
* @return All the weapon mounts in the bay.
*/
public List<WeaponMounted> getBayWeapons() {
return streamBayWeapons()
.collect(Collectors.toList());
}

Expand Down

0 comments on commit 175a3f1

Please sign in to comment.