From 5f570fc5760f00b8c43ab4f5cf8f4df883229dad Mon Sep 17 00:00:00 2001 From: Pahimar Date: Fri, 20 May 2016 13:56:53 -0400 Subject: [PATCH] Better Singleton implementation of RecipeRegistry, and fix up the getStacksInRange to be more flexible about bounds --- .../com/pahimar/ee3/EquivalentExchange3.java | 2 +- .../exchange/EnergyValueRegistryProxy.java | 3 +- .../ee3/exchange/EnergyValueRegistry.java | 86 +++++++++++-------- .../ee3/handler/WorldEventHandler.java | 2 +- .../pahimar/ee3/recipe/RecipeRegistry.java | 13 +-- .../com/pahimar/ee3/util/FilterUtils.java | 36 ++++++-- 6 files changed, 83 insertions(+), 59 deletions(-) diff --git a/src/main/java/com/pahimar/ee3/EquivalentExchange3.java b/src/main/java/com/pahimar/ee3/EquivalentExchange3.java index f45dd976..290492ef 100644 --- a/src/main/java/com/pahimar/ee3/EquivalentExchange3.java +++ b/src/main/java/com/pahimar/ee3/EquivalentExchange3.java @@ -141,7 +141,7 @@ public class EquivalentExchange3 public RecipeRegistry getRecipeRegistry() { - return RecipeRegistry.getInstance(); + return RecipeRegistry.INSTANCE; } public AludelRecipeManager getAludelRecipeManager() diff --git a/src/main/java/com/pahimar/ee3/api/exchange/EnergyValueRegistryProxy.java b/src/main/java/com/pahimar/ee3/api/exchange/EnergyValueRegistryProxy.java index 68eaa813..d5b3428e 100644 --- a/src/main/java/com/pahimar/ee3/api/exchange/EnergyValueRegistryProxy.java +++ b/src/main/java/com/pahimar/ee3/api/exchange/EnergyValueRegistryProxy.java @@ -3,6 +3,7 @@ package com.pahimar.ee3.api.exchange; import com.pahimar.ee3.EquivalentExchange3; import cpw.mods.fml.common.Mod; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -66,7 +67,7 @@ public final class EnergyValueRegistryProxy { init(); if (ee3Mod != null) { - return EE3Wrapper.ee3mod.getEnergyValueRegistry().getStacksInRange(start, finish); + return new ArrayList<>(EE3Wrapper.ee3mod.getEnergyValueRegistry().getStacksInRange(start, finish)); } return Collections.EMPTY_LIST; diff --git a/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java b/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java index 6899d0a5..e2346cdd 100644 --- a/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java +++ b/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java @@ -7,6 +7,8 @@ import com.pahimar.ee3.api.exchange.EnergyValue; import com.pahimar.ee3.api.exchange.IEnergyValueProvider; import com.pahimar.ee3.handler.ConfigurationHandler; import com.pahimar.ee3.recipe.RecipeRegistry; +import com.pahimar.ee3.reference.Comparators; +import com.pahimar.ee3.util.FilterUtils; import com.pahimar.ee3.util.LoaderHelper; import com.pahimar.ee3.util.LogHelper; import com.pahimar.ee3.util.SerializationHelper; @@ -15,7 +17,6 @@ import cpw.mods.fml.common.Loader; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidContainerRegistry; -import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.oredict.OreDictionary; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; @@ -31,7 +32,7 @@ public class EnergyValueRegistry { public static final EnergyValueRegistry INSTANCE = new EnergyValueRegistry(); private ImmutableSortedMap stackValueMap; - private ImmutableSortedMap> valueStackMap; + private ImmutableSortedMap> valueStackMap; private final Map preCalculationStackValueMap; private final Map postCalculationStackValueMap; @@ -94,7 +95,7 @@ public class EnergyValueRegistry { * assignments) query or not * @return an {@link EnergyValue} if there is one to be found for the provided {@link Object} in the provided Map, null otherwise */ - private static EnergyValue getEnergyValue(Map valueMap, Object object, boolean strict) { + public static EnergyValue getEnergyValue(Map valueMap, Object object, boolean strict) { if (WrappedStack.canBeWrapped(object)) { @@ -401,58 +402,69 @@ public class EnergyValueRegistry { * @param finish * @return */ - public List getStacksInRange(Number start, Number finish) { + public Set getStacksInRange(Number start, Number finish) { return getStacksInRange(new EnergyValue(start), new EnergyValue(finish)); } /** * TODO Finish JavaDoc * - * @param start - * @param finish + * @param lowerBound + * @param upperBound * @return */ - public List getStacksInRange(EnergyValue start, EnergyValue finish) { + public Set getStacksInRange(EnergyValue lowerBound, EnergyValue upperBound) { - List stacksInRange = new ArrayList(); + Set filteredItemStacks = new TreeSet<>(Comparators.ENERGY_VALUE_ITEM_STACK_COMPARATOR); - if (valueStackMap != null) { + Set greaterThanLowerBound = getStacksInRange(getEnergyValues(), lowerBound, false); + Set lesserThanUpperBound = getStacksInRange(getEnergyValues(), upperBound, true); - SortedMap> tailMap = valueStackMap.tailMap(start); - SortedMap> headMap = valueStackMap.headMap(finish); + if (!greaterThanLowerBound.isEmpty() && !lesserThanUpperBound.isEmpty()) { - SortedMap> smallerMap; - SortedMap> biggerMap; + for (ItemStack itemStack : greaterThanLowerBound) { + if (lesserThanUpperBound.contains(itemStack)) { + filteredItemStacks.add(itemStack); + } + } + } + else if (!greaterThanLowerBound.isEmpty()) { + return greaterThanLowerBound; + } + else if (!lesserThanUpperBound.isEmpty()) { + return lesserThanUpperBound; + } - if (!tailMap.isEmpty() && !headMap.isEmpty()) { + return filteredItemStacks; + } - if (tailMap.size() <= headMap.size()) { - smallerMap = tailMap; - biggerMap = headMap; + /** + * TODO Finish JavaDoc + * + * @param valueMap + * @param energyValueBound + * @param isUpperBound + * @return + */ + private static Set getStacksInRange(Map valueMap, EnergyValue energyValueBound, boolean isUpperBound) { + + Set itemStacks = FilterUtils.filterForItemStacks(valueMap.keySet()); + + if (valueMap != null) { + if (energyValueBound != null) { + if (isUpperBound) { + return FilterUtils.filterByEnergyValue(valueMap, itemStacks, energyValueBound, FilterUtils.ValueFilterType.VALUE_LOWER_THAN_BOUND, Comparators.ENERGY_VALUE_ITEM_STACK_COMPARATOR); } else { - smallerMap = headMap; - biggerMap = tailMap; - } - - for (EnergyValue value : smallerMap.keySet()) { - if (biggerMap.containsKey(value)) { - for (WrappedStack wrappedStack : valueStackMap.get(value)) { - if (wrappedStack.getWrappedObject() instanceof ItemStack || wrappedStack.getWrappedObject() instanceof FluidStack) { - stacksInRange.add(wrappedStack.getWrappedObject()); - } - else if (wrappedStack.getWrappedObject() instanceof OreStack) { - stacksInRange.addAll(OreDictionary.getOres(((OreStack) wrappedStack.getWrappedObject()).oreName)); - } - } - } + return FilterUtils.filterByEnergyValue(valueMap, itemStacks, energyValueBound, FilterUtils.ValueFilterType.VALUE_GREATER_THAN_BOUND, Comparators.ENERGY_VALUE_ITEM_STACK_COMPARATOR); } } } - return stacksInRange; + return new TreeSet<>(Collections.EMPTY_SET); } + /** * Sets an {@link EnergyValue} for the provided {@link Object} (if it can be wrapped in a {@link WrappedStack}. * Depending on whether or not this is a pre-calculation value assignment it's also possible for the calculated @@ -565,11 +577,11 @@ public class EnergyValueRegistry { computedMap.putAll(tempComputedMap); tempComputedMap = new TreeMap<>(computedMap); - for (WrappedStack recipeOutput : RecipeRegistry.getInstance().getRecipeMappings().keySet()) { + for (WrappedStack recipeOutput : RecipeRegistry.INSTANCE.getRecipeMappings().keySet()) { // We won't attempt to recalculate values that already have a pre-calculation value assignment if (!stackValueMap.containsKey(WrappedStack.wrap(recipeOutput, 1))) { - for (Set recipeInputs : RecipeRegistry.getInstance().getRecipeMappings().get(recipeOutput)) { + for (Set recipeInputs : RecipeRegistry.INSTANCE.getRecipeMappings().get(recipeOutput)) { EnergyValue currentOutputValue = getEnergyValue(tempComputedMap, WrappedStack.wrap(recipeOutput, 1), false); EnergyValue computedOutputValue = computeFromInputs(tempComputedMap, recipeOutput, recipeInputs); @@ -604,7 +616,7 @@ public class EnergyValueRegistry { private void calculateValueStackMap() { - SortedMap> tempValueMap = new TreeMap<>(); + SortedMap> tempValueMap = new TreeMap<>(); for (WrappedStack wrappedStack : getEnergyValues().keySet()) { @@ -619,7 +631,7 @@ public class EnergyValueRegistry { } } else { - tempValueMap.put(energyValue, new ArrayList<>(Arrays.asList(wrappedStack))); + tempValueMap.put(energyValue, new TreeSet<>(Arrays.asList(wrappedStack))); } } } diff --git a/src/main/java/com/pahimar/ee3/handler/WorldEventHandler.java b/src/main/java/com/pahimar/ee3/handler/WorldEventHandler.java index f32c2593..ba89d910 100644 --- a/src/main/java/com/pahimar/ee3/handler/WorldEventHandler.java +++ b/src/main/java/com/pahimar/ee3/handler/WorldEventHandler.java @@ -18,7 +18,7 @@ public class WorldEventHandler { if (!hasInitilialized && FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) { - RecipeRegistry.getInstance().registerVanillaRecipes(); + RecipeRegistry.INSTANCE.registerVanillaRecipes(); AludelRecipeManager.registerRecipes(); long startTime = System.nanoTime(); diff --git a/src/main/java/com/pahimar/ee3/recipe/RecipeRegistry.java b/src/main/java/com/pahimar/ee3/recipe/RecipeRegistry.java index b07c9215..cdf8ea66 100644 --- a/src/main/java/com/pahimar/ee3/recipe/RecipeRegistry.java +++ b/src/main/java/com/pahimar/ee3/recipe/RecipeRegistry.java @@ -18,7 +18,7 @@ import java.util.TreeSet; public class RecipeRegistry { public static final Marker RECIPE_MARKER = MarkerManager.getMarker("EE3_RECIPE", LogHelper.MOD_MARKER); - private static RecipeRegistry recipeRegistry = null; + public static final RecipeRegistry INSTANCE = new RecipeRegistry(); private Multimap> recipeMap; private ImmutableMultimap> immutableRecipeMap; @@ -27,15 +27,6 @@ public class RecipeRegistry { recipeMap = TreeMultimap.create(WrappedStack.COMPARATOR, Comparators.WRAPPED_STACK_SET_COMPARATOR); } - public static RecipeRegistry getInstance() { - - if (recipeRegistry == null) { - recipeRegistry = new RecipeRegistry(); - } - - return recipeRegistry; - } - public void addRecipe(Object recipeOutput, Collection recipeInputList) { // Wrap the recipe output @@ -86,7 +77,7 @@ public class RecipeRegistry { public Multimap> getRecipeMappings() { if (immutableRecipeMap == null) { - immutableRecipeMap = ImmutableMultimap.copyOf(recipeRegistry.recipeMap); + immutableRecipeMap = ImmutableMultimap.copyOf(INSTANCE.recipeMap); } return immutableRecipeMap; diff --git a/src/main/java/com/pahimar/ee3/util/FilterUtils.java b/src/main/java/com/pahimar/ee3/util/FilterUtils.java index f03628e2..aafce1e6 100644 --- a/src/main/java/com/pahimar/ee3/util/FilterUtils.java +++ b/src/main/java/com/pahimar/ee3/util/FilterUtils.java @@ -1,17 +1,33 @@ package com.pahimar.ee3.util; import com.pahimar.ee3.api.exchange.EnergyValue; -import com.pahimar.ee3.api.exchange.EnergyValueRegistryProxy; +import com.pahimar.ee3.exchange.EnergyValueRegistry; +import com.pahimar.ee3.exchange.OreStack; +import com.pahimar.ee3.exchange.WrappedStack; import com.pahimar.ee3.reference.Comparators; import net.minecraft.item.ItemStack; +import net.minecraftforge.oredict.OreDictionary; -import java.util.Collection; -import java.util.Comparator; -import java.util.Set; -import java.util.TreeSet; +import java.util.*; public class FilterUtils { + public static Set filterForItemStacks(Set wrappedStacks) { + + Set itemStacks = new TreeSet<>(Comparators.ID_COMPARATOR); + + for (WrappedStack wrappedStack : wrappedStacks) { + if (wrappedStack.getWrappedObject() instanceof ItemStack) { + itemStacks.add((ItemStack) wrappedStack.getWrappedObject()); + } + else if (wrappedStack.getWrappedObject() instanceof OreStack) { + itemStacks.addAll(OreDictionary.getOres(((OreStack) wrappedStack.getWrappedObject()).oreName)); + } + } + + return itemStacks; + } + public static Set filterByDisplayName(Set itemStacks, String filterString) { return filterByDisplayName(itemStacks, filterString, NameFilterType.STARTS_WITH, null); } @@ -62,10 +78,14 @@ public class FilterUtils { } public static Set filterByEnergyValue(Collection itemStacks, Number valueBound, ValueFilterType filterType, Comparator comparator) { - return filterByEnergyValue(itemStacks, new EnergyValue(valueBound.floatValue()), filterType, comparator); + return filterByEnergyValue(EnergyValueRegistry.INSTANCE.getEnergyValues(), itemStacks, new EnergyValue(valueBound.floatValue()), filterType, comparator); } public static Set filterByEnergyValue(Collection itemStacks, EnergyValue valueBound, ValueFilterType filterType, Comparator comparator) { + return filterByEnergyValue(EnergyValueRegistry.INSTANCE.getEnergyValues(), itemStacks, valueBound, filterType, comparator); + } + + public static Set filterByEnergyValue(Map valueMap, Collection itemStacks, EnergyValue valueBound, ValueFilterType filterType, Comparator comparator) { Set filteredSet = (comparator != null ? new TreeSet<>(comparator) : new TreeSet<>(Comparators.DISPLAY_NAME_COMPARATOR)); @@ -77,9 +97,9 @@ public class FilterUtils { else { for (ItemStack itemStack : itemStacks) { - EnergyValue energyValue = EnergyValueRegistryProxy.getEnergyValue(itemStack); + EnergyValue energyValue = EnergyValueRegistry.INSTANCE.getEnergyValue(valueMap, itemStack, false); - if (energyValue != null) { + if (energyValue != null && Float.compare(energyValue.getValue(), 0) > 0) { if (filterType == ValueFilterType.VALUE_LOWER_THAN_BOUND && energyValue.compareTo(valueBound) <= 0) { filteredSet.add(itemStack); }