From e3ead6da2d073be611782e490d2f5f7b3c8e92aa 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. --- .../src/megamek/common/equipment/WeaponMounted.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/megamek/src/megamek/common/equipment/WeaponMounted.java b/megamek/src/megamek/common/equipment/WeaponMounted.java index 48b39f59bd1..10f0c14148e 100644 --- a/megamek/src/megamek/common/equipment/WeaponMounted.java +++ b/megamek/src/megamek/common/equipment/WeaponMounted.java @@ -206,13 +206,20 @@ public void clearBayWeapons() { bayWeapons.clear(); } + /** + * @return A stream containing the weapon mounts in the bay. + */ + public Stream streamBayWeapons() { + return bayWeapons.stream() + .map(getEntity().getWeapon) + .filter(Objects::nonNull); + } + /** * @return All the weapon mounts in the bay. */ public List getBayWeapons() { - return bayWeapons.stream() - .map(i -> getEntity().getWeapon(i)) - .filter(Objects::nonNull) + return streamBayWeapons() .collect(Collectors.toList()); }