Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Gas Registry Lookup Performance #138

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
181 changes: 75 additions & 106 deletions src/main/java/mekanism/api/gas/GasRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,109 +5,78 @@
import java.util.ArrayList;
import java.util.List;

public class GasRegistry
{
private static ArrayList<Gas> registeredGasses = new ArrayList<Gas>();

/**
* Register a new gas into GasRegistry.
* @param gas - Gas to register
* @return the gas that has been registered, pulled right out of GasRegistry
*/
public static Gas register(Gas gas)
{
if(gas == null)
{
return null;
}

registeredGasses.add(gas);

return getGas(gas.getName());
}

/**
* Gets the gas associated with the defined ID.
* @param id - ID to check
* @return gas associated with defined ID
*/
public static Gas getGas(int id)
{
if(id == -1)
{
return null;
}

return registeredGasses.get(id);
}

/**
* Gets the gas associated with the defined fluid.
* @param f - fluid to check
* @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;
}

/**
* Whether or not GasRegistry contains a gas with the specified name
* @param name - name to check
* @return if GasRegistry contains a gas with the defined name
*/
public static boolean containsGas(String name)
{
return getGas(name) != null;
}

/**
* Gets the list of all gasses registered in GasRegistry.
* @return a cloned list of all registered gasses
*/
public static List<Gas> getRegisteredGasses()
{
return (List<Gas>)registeredGasses.clone();
}

/**
* Gets the gas associated with the specified name.
* @param name - name of the gas to get
* @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;
}

/**
* Gets the gas ID of a specified gas.
* @param gas - gas to get the ID from
* @return gas ID
*/
public static int getGasID(Gas gas)
{
if(gas == null || !containsGas(gas.getName()))
{
return -1;
}

return registeredGasses.indexOf(gas);
}
}
public class GasRegistry {
private static ArrayList<Gas> registeredGasses = new ArrayList<Gas>();

/**
* Register a new gas into GasRegistry.
* @param gas - Gas to register
* @return the gas that has been registered, pulled right out of GasRegistry
*/
public static Gas register(Gas gas) {
if (gas == null) {
return null;
}
registeredGasses.add(gas);
return getGas(gas.getName());
}

/**
* Gets the gas associated with the defined ID.
* @param id - ID to check
* @return gas associated with defined ID
*/
public static Gas getGas(int id) {
return id == -1 ? null : registeredGasses.get(id);
}

/**
* Gets the gas associated with the defined fluid.
* @param f - fluid to check
* @return the gas associated with the fluid
*/
public static Gas getGas(Fluid f) {
return registeredGasses.stream()
.filter(gas -> gas.hasFluid() && gas.getFluid() == f)
.findAny()
.orElse(null);
}

/**
* Whether or not GasRegistry contains a gas with the specified name
* @param name - name to check
* @return if GasRegistry contains a gas with the defined name
*/
public static boolean containsGas(String name) {
return getGas(name) != null;
}

/**
* Gets the list of all gasses registered in GasRegistry.
* @return a cloned list of all registered gasses
*/
public static List<Gas> getRegisteredGasses() {
return (List<Gas>)registeredGasses.clone();
}

/**
* Gets the gas associated with the specified name.
* @param name - name of the gas to get
* @return gas associated with the name
*/
public static Gas getGas(String name) {
return registeredGasses.stream()
.filter(gas -> gas.getName().equalsIgnoreCase(name))
.findAny()
.orElse(null);
}

/**
* Gets the gas ID of a specified gas.
* @param gas - gas to get the ID from
* @return gas ID
*/
public static int getGasID(Gas gas) {
return gas == null || !containsGas(gas.getName()) ? -1 : registeredGasses.indexOf(gas);
}
}