From 175a3f1ff5c5375e94127cdd9cf9fc4e7d4ebccf Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Sun, 18 Aug 2024 11:33:52 -0500 Subject: [PATCH] refactor: Add function for streaming bay weapons 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. --- .../megamek/common/equipment/WeaponMounted.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/megamek/src/megamek/common/equipment/WeaponMounted.java b/megamek/src/megamek/common/equipment/WeaponMounted.java index 48b39f59bd1..ca685cc00d4 100644 --- a/megamek/src/megamek/common/equipment/WeaponMounted.java +++ b/megamek/src/megamek/common/equipment/WeaponMounted.java @@ -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 { @@ -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 getBayWeapons() { + public Stream 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 getBayWeapons() { + return streamBayWeapons() .collect(Collectors.toList()); }