From b7161d4e77cfda66ebbea146440be7943b5bd1c5 Mon Sep 17 00:00:00 2001 From: Spoilers <33331567+ace10102@users.noreply.github.com> Date: Thu, 20 Jul 2023 12:47:47 -0400 Subject: [PATCH] Improve Gas Registry lookup performance Remove unnecessary overuse of String.toLowerCase() and improved efficiency and speed of registry access Fixes Thorfusion/Mekanism-Community-Edition#137 --- .../java/mekanism/api/gas/GasRegistry.java | 30 +++++++------------ 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/src/main/java/mekanism/api/gas/GasRegistry.java b/src/main/java/mekanism/api/gas/GasRegistry.java index 9852e677c..a6737aff7 100644 --- a/src/main/java/mekanism/api/gas/GasRegistry.java +++ b/src/main/java/mekanism/api/gas/GasRegistry.java @@ -27,10 +27,7 @@ public class GasRegistry { * @return gas associated with defined ID */ public static Gas getGas(int id) { - if (id == -1) { - return null; - } - return registeredGasses.get(id); + return id == -1 ? null : registeredGasses.get(id); } /** @@ -39,12 +36,10 @@ public class GasRegistry { * @return the gas associated with the fluid */ public static Gas getGas(Fluid f) { - for (Gas gas : getRegisteredGasses()) { - if (gas.hasFluid() && gas.getFluid() == f) { - return gas; - } - } - return null; + return registeredGasses.stream() + .filter(gas -> gas.hasFluid() && gas.getFluid() == f) + .findAny() + .orElse(null); } /** @@ -70,12 +65,10 @@ public class GasRegistry { * @return gas associated with the name */ public static Gas getGas(String name) { - for (Gas gas : registeredGasses) { - if (gas.getName().toLowerCase().equals(name.toLowerCase())) { - return gas; - } - } - return null; + return registeredGasses.stream() + .filter(gas -> gas.getName().equalsIgnoreCase(name)) + .findAny() + .orElse(null); } /** @@ -84,9 +77,6 @@ public class GasRegistry { * @return gas ID */ public static int getGasID(Gas gas) { - if (gas == null || !containsGas(gas.getName())) { - return -1; - } - return registeredGasses.indexOf(gas); + return gas == null || !containsGas(gas.getName()) ? -1 : registeredGasses.indexOf(gas); } } \ No newline at end of file