Make some changes to comparators, switch the recipe registry to a TreeMultimap (from HashMultimap), and dump WrappedStack's hashCode as we are no longer using HashMaps/etc (and it wasn't a great implementation anyways)

This commit is contained in:
Pahimar 2016-05-19 21:10:55 -04:00
parent 8d40883473
commit 429a0fc51b
14 changed files with 105 additions and 142 deletions

View File

@ -7,6 +7,11 @@ import java.util.List;
public final class RecipeRegistryProxy {
/**
*
* @param recipeOutput
* @param recipeInputList
*/
public static void addRecipe(Object recipeOutput, List<?> recipeInputList) {
init();

View File

@ -216,18 +216,18 @@ public class EnergyValueRegistry {
}
/**
* Calculates an {@link EnergyValue} for the provided {@link WrappedStack} output from the provided {@link List} of
* WrappedStack inputs and {@link Map} of energy value mappings to objects. We calculate the energy value for the
* output by, for each input, summing the input's energy value * the input's stack size. That sum is then divided
* by the stack size of the output. If <strong>any</strong> of the inputs do not have an energy value then no
* energy value can be calculated for the output - therefore we return null
* Calculates an {@link EnergyValue} for the provided {@link WrappedStack} output from the provided
* {@link Collection} of WrappedStack inputs and {@link Map} of energy value mappings to objects. We calculate the
* energy value for the output by, for each input, summing the input's energy value * the input's stack size. That
* sum is then divided by the stack size of the output. If <strong>any</strong> of the inputs do not have an energy
* value then no energy value can be calculated for the output - therefore we return null
*
* @param valueMap a {@link Map} of {@link EnergyValue}'s mapped to {@link WrappedStack}'s
* @param wrappedOutput the {@link WrappedStack} output for that the inputs "create"
* @param wrappedInputs a {@link List} of {@link WrappedStack}s that "create" the output
* @param wrappedInputs a {@link Collection} of {@link WrappedStack}s that "create" the output
* @return an {@link EnergyValue} if there is one that can be calculated, null otherwise
*/
private static EnergyValue computeFromInputs(Map<WrappedStack, EnergyValue> valueMap, WrappedStack wrappedOutput, List<WrappedStack> wrappedInputs) {
private static EnergyValue computeFromInputs(Map<WrappedStack, EnergyValue> valueMap, WrappedStack wrappedOutput, Collection<WrappedStack> wrappedInputs) {
float sumOfValues = 0f;
@ -569,7 +569,7 @@ public class EnergyValueRegistry {
// We won't attempt to recalculate values that already have a pre-calculation value assignment
if (!stackValueMap.containsKey(WrappedStack.wrap(recipeOutput, 1))) {
for (List<WrappedStack> recipeInputs : RecipeRegistry.getInstance().getRecipeMappings().get(recipeOutput)) {
for (Set<WrappedStack> recipeInputs : RecipeRegistry.getInstance().getRecipeMappings().get(recipeOutput)) {
EnergyValue currentOutputValue = getEnergyValue(tempComputedMap, WrappedStack.wrap(recipeOutput, 1), false);
EnergyValue computedOutputValue = computeFromInputs(tempComputedMap, recipeOutput, recipeInputs);

View File

@ -67,7 +67,7 @@ public class OreStack implements Comparable<OreStack> {
public static OreStack getOreStackFromList(List<?> objectList) {
if (objectList.size() > 0) {
Map<String, Integer> oreNameCountMap = new TreeMap<>(Comparators.stringComparator);
Map<String, Integer> oreNameCountMap = new TreeMap<>(Comparators.STRING_COMPARATOR);
for (Object listElement : objectList) {
if (listElement instanceof ItemStack) {
ItemStack itemStack = (ItemStack) listElement;

View File

@ -441,39 +441,6 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
return jsonWrappedStack;
}
/**
*
*/
@Override
public int hashCode() {
int hashCode = 1;
hashCode = (37 * hashCode) + stackSize;
if (wrappedStack instanceof ItemStack) {
hashCode = (37 * hashCode) + Item.getIdFromItem(((ItemStack) wrappedStack).getItem());
hashCode = (37 * hashCode) + ((ItemStack) wrappedStack).getItemDamage();
if (((ItemStack) wrappedStack).getTagCompound() != null) {
hashCode = (37 * hashCode) + ((ItemStack) wrappedStack).getTagCompound().hashCode();
}
}
else if (wrappedStack instanceof OreStack) {
if (((OreStack) wrappedStack).oreName != null) {
hashCode = (37 * hashCode) + ((OreStack) wrappedStack).oreName.hashCode();
}
}
else if (wrappedStack instanceof FluidStack) {
hashCode = (37 * hashCode) + wrappedStack.hashCode();
if (((FluidStack) wrappedStack).tag != null) {
hashCode = (37 * hashCode) + ((FluidStack) wrappedStack).tag.hashCode();
}
}
return hashCode;
}
@Override
public boolean equals(Object object)
{
@ -488,7 +455,7 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
@Override
public int compareTo(WrappedStack wrappedStack)
{
return comparator.compare(this, wrappedStack);
return COMPARATOR.compare(this, wrappedStack);
}
/**
@ -536,7 +503,7 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
}
}
public static Comparator<WrappedStack> comparator = new Comparator<WrappedStack>()
public static final Comparator<WrappedStack> COMPARATOR = new Comparator<WrappedStack>()
{
@Override

View File

@ -31,7 +31,7 @@ public class ContainerAlchenomicon extends ContainerEE implements IElementButton
public ContainerAlchenomicon(EntityPlayer entityPlayer, ItemStack itemStack)
{
TreeSet<ItemStack> knownTransmutations = new TreeSet<ItemStack>(Comparators.displayNameComparator);
TreeSet<ItemStack> knownTransmutations = new TreeSet<ItemStack>(Comparators.DISPLAY_NAME_COMPARATOR);
if (itemStack.getItem() instanceof ItemAlchenomicon && ItemHelper.hasOwnerUUID(itemStack))
{
@ -188,7 +188,7 @@ public class ContainerAlchenomicon extends ContainerEE implements IElementButton
this.requiresUpdate = true;
boolean shouldUpdateInventory = false;
ItemStack[] newInventory = new ItemStack[80];
List<ItemStack> filteredList = new ArrayList(FilterUtils.filterByDisplayName(inventoryAlchenomicon.getKnownTransmutations(), searchTerm, FilterUtils.NameFilterType.CONTAINS, Comparators.displayNameComparator));
List<ItemStack> filteredList = new ArrayList(FilterUtils.filterByDisplayName(inventoryAlchenomicon.getKnownTransmutations(), searchTerm, FilterUtils.NameFilterType.CONTAINS, Comparators.DISPLAY_NAME_COMPARATOR));
maxPageOffset = filteredList.size() / 80;
if (pageOffset > maxPageOffset)

View File

@ -44,7 +44,7 @@ public class ContainerTransmutationTablet extends ContainerEE implements IElemen
{
this.tileEntityTransmutationTablet = tileEntityTransmutationTablet;
TreeSet<ItemStack> knownTransmutations = new TreeSet<ItemStack>(Comparators.displayNameComparator);
TreeSet<ItemStack> knownTransmutations = new TreeSet<ItemStack>(Comparators.DISPLAY_NAME_COMPARATOR);
if (tileEntityTransmutationTablet.getStackInSlot(TileEntityTransmutationTablet.ALCHENOMICON_INDEX) != null)
{
ItemStack itemStack = tileEntityTransmutationTablet.getStackInSlot(TileEntityTransmutationTablet.ALCHENOMICON_INDEX);
@ -198,8 +198,8 @@ public class ContainerTransmutationTablet extends ContainerEE implements IElemen
}
}
private void updateInventory()
{
private void updateInventory() {
ItemStack[] newInventory = new ItemStack[30];
Set<ItemStack> filteredSet = FilterUtils.filterByDisplayName(this.inventoryTransmutationTablet.getKnownTransmutations(), searchTerm, FilterUtils.NameFilterType.CONTAINS);
@ -207,55 +207,45 @@ public class ContainerTransmutationTablet extends ContainerEE implements IElemen
int adjustedStartIndex = (int) ((scrollBarPosition / 187f) * filteredList.size());
if (sortOption == 0)
{
if (sortOrder == 0)
{
Collections.sort(filteredList, Comparators.displayNameComparator);
if (sortOption == 0) {
if (sortOrder == 0) {
Collections.sort(filteredList, Comparators.DISPLAY_NAME_COMPARATOR);
}
else
{
Collections.sort(filteredList, Comparators.reverseDisplayNameComparator);
else {
Collections.sort(filteredList, Comparators.DISPLAY_NAME_COMPARATOR.reversed());
}
}
else if (sortOption == 1)
{
if (sortOrder == 0)
{
Collections.sort(filteredList, Comparators.energyValueItemStackComparator);
else if (sortOption == 1) {
if (sortOrder == 0) {
Collections.sort(filteredList, Comparators.ENERGY_VALUE_ITEM_STACK_COMPARATOR);
}
else
{
Collections.sort(filteredList, Comparators.reverseEnergyValueComparator);
else {
Collections.sort(filteredList, Comparators.ENERGY_VALUE_ITEM_STACK_COMPARATOR.reversed());
}
}
else if (sortOption == 2)
{
if (sortOrder == 0)
{
Collections.sort(filteredList, Comparators.idComparator);
else if (sortOption == 2) {
if (sortOrder == 0) {
Collections.sort(filteredList, Comparators.ID_COMPARATOR);
}
else
{
Collections.sort(filteredList, Comparators.reverseIdComparator);
else {
Collections.sort(filteredList, Comparators.ID_COMPARATOR.reversed());
}
}
if (filteredList.size() <= 30)
{
if (filteredList.size() <= 30) {
newInventory = filteredList.toArray(newInventory);
}
else if (adjustedStartIndex + 30 <= filteredList.size())
{
else if (adjustedStartIndex + 30 <= filteredList.size()) {
newInventory = filteredList.subList(adjustedStartIndex, adjustedStartIndex + 30).toArray(newInventory);
}
else
{
else {
newInventory = filteredList.subList(filteredList.size() - 30, filteredList.size()).toArray(newInventory);
}
for (int i = 0; i < 30; i++)
{
for (int i = 0; i < 30; i++) {
this.getSlot(i + 10).putStack(newInventory[i]);
}
}

View File

@ -23,7 +23,7 @@ public class InventoryAlchenomicon implements IInventory
}
else
{
this.knownTransmutations = new TreeSet<ItemStack>(Comparators.idComparator);
this.knownTransmutations = new TreeSet<ItemStack>(Comparators.ID_COMPARATOR);
}
inventory = knownTransmutations.toArray(inventory);
}

View File

@ -22,7 +22,7 @@ public class InventoryTransmutationTablet implements IInventory
{
inventory = new ItemStack[30];
this.knownTransmutations = new TreeSet<ItemStack>(Comparators.idComparator);
this.knownTransmutations = new TreeSet<ItemStack>(Comparators.ID_COMPARATOR);
if (knownTransmutations != null)
{
this.knownTransmutations.addAll(knownTransmutations);

View File

@ -23,7 +23,7 @@ public class PlayerKnowledge {
public PlayerKnowledge(Collection<ItemStack> itemStacks) {
version = VERSION;
knownItemStacks = new TreeSet<>(Comparators.idComparator);
knownItemStacks = new TreeSet<>(Comparators.ID_COMPARATOR);
if (itemStacks != null) {
for (ItemStack itemStack : itemStacks) {

View File

@ -22,12 +22,12 @@ public class TransmutationKnowledge implements JsonSerializer<TransmutationKnowl
public TransmutationKnowledge()
{
this(new TreeSet<ItemStack>(Comparators.idComparator));
this(new TreeSet<ItemStack>(Comparators.ID_COMPARATOR));
}
public TransmutationKnowledge(Collection<ItemStack> knownTransmutations)
{
this.knownTransmutations = new TreeSet<ItemStack>(Comparators.idComparator);
this.knownTransmutations = new TreeSet<ItemStack>(Comparators.ID_COMPARATOR);
this.knownTransmutations.addAll(knownTransmutations);
hasBeenModified = false;
}
@ -143,7 +143,7 @@ public class TransmutationKnowledge implements JsonSerializer<TransmutationKnowl
{
JsonObject jsonObject = (JsonObject) json;
Set<ItemStack> itemStacks = new TreeSet<ItemStack>(Comparators.idComparator);
Set<ItemStack> itemStacks = new TreeSet<ItemStack>(Comparators.ID_COMPARATOR);
if (jsonObject.has("knownTransmutations") && jsonObject.get("knownTransmutations").isJsonArray())
{

View File

@ -1,28 +1,30 @@
package com.pahimar.ee3.recipe;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.TreeMultimap;
import com.pahimar.ee3.exchange.WrappedStack;
import com.pahimar.ee3.reference.Comparators;
import com.pahimar.ee3.util.LoaderHelper;
import com.pahimar.ee3.util.LogHelper;
import cpw.mods.fml.common.Loader;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.util.Set;
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;
private Multimap<WrappedStack, List<WrappedStack>> recipeMap;
private ImmutableMultimap<WrappedStack, List<WrappedStack>> immutableRecipeMap;
private Multimap<WrappedStack, Set<WrappedStack>> recipeMap;
private ImmutableMultimap<WrappedStack, Set<WrappedStack>> immutableRecipeMap;
private RecipeRegistry() {
recipeMap = HashMultimap.create(); // TODO Switch this to a TreeMultimap
recipeMap = TreeMultimap.create(WrappedStack.COMPARATOR, Comparators.WRAPPED_STACK_SET_COMPARATOR);
}
public static RecipeRegistry getInstance() {
@ -34,8 +36,7 @@ public class RecipeRegistry {
return recipeRegistry;
}
// FIXME Prevent adding multiple equivalent entries {@link https://github.com/pahimar/Equivalent-Exchange-3/issues/1046}
public void addRecipe(Object recipeOutput, List<?> recipeInputList) {
public void addRecipe(Object recipeOutput, Collection<?> recipeInputList) {
// Wrap the recipe output
WrappedStack wrappedRecipeOutput = WrappedStack.wrap(recipeOutput);
@ -43,7 +44,7 @@ public class RecipeRegistry {
return;
}
List<WrappedStack> wrappedRecipeInputList = new ArrayList<>();
Set<WrappedStack> wrappedRecipeInputList = new TreeSet<>();
StringBuilder stringBuilder = new StringBuilder();
for (Object recipeInputObject : recipeInputList) {
@ -62,7 +63,7 @@ public class RecipeRegistry {
// Check to see if we already have this recipe in the map
boolean existsAlready = false;
for (List<WrappedStack> recipeInputs : recipeMap.get(wrappedRecipeOutput)) {
for (Set<WrappedStack> recipeInputs : recipeMap.get(wrappedRecipeOutput)) {
if (recipeInputs.containsAll(wrappedRecipeInputList) && wrappedRecipeInputList.containsAll(recipeInputs)) {
existsAlready = true;
}
@ -82,7 +83,7 @@ public class RecipeRegistry {
RecipesPotions.registerRecipes();
}
public Multimap<WrappedStack, List<WrappedStack>> getRecipeMappings() {
public Multimap<WrappedStack, Set<WrappedStack>> getRecipeMappings() {
if (immutableRecipeMap == null) {
immutableRecipeMap = ImmutableMultimap.copyOf(recipeRegistry.recipeMap);
@ -96,7 +97,7 @@ public class RecipeRegistry {
for (WrappedStack wrappedStack : getRecipeMappings().keySet()) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(String.format("Output: %s, Inputs: ", wrappedStack.toString()));
for (List<WrappedStack> listStacks : getRecipeMappings().get(wrappedStack)) {
for (Set<WrappedStack> listStacks : getRecipeMappings().get(wrappedStack)) {
for (WrappedStack listStack : listStacks) {
stringBuilder.append(listStack.toString() + " ");
}

View File

@ -1,33 +1,50 @@
package com.pahimar.ee3.reference;
import com.pahimar.ee3.api.exchange.EnergyValueRegistryProxy;
import com.pahimar.ee3.exchange.WrappedStack;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
import java.util.Comparator;
import java.util.Set;
public class Comparators
{
public static Comparator<String> stringComparator = new Comparator<String>()
{
public class Comparators {
public static final Comparator<Set<WrappedStack>> WRAPPED_STACK_SET_COMPARATOR = new Comparator<Set<WrappedStack>>() {
@Override
public int compare(String string1, String string2)
{
return string1.compareToIgnoreCase(string2);
public int compare(Set<WrappedStack> collection1, Set<WrappedStack> collection2) {
if (collection1 != null && collection2 != null) {
if (collection1.size() == collection2.size()) {
if (collection1.containsAll(collection2)) {
if (collection2.containsAll(collection1)) {
return 0;
}
else {
return 1;
}
}
else {
return -1;
}
}
else {
return collection1.size() - collection2.size();
}
}
else if (collection1 != null) {
return 1;
}
else {
return -1;
}
}
};
public static Comparator<ItemStack> reverseIdComparator = new Comparator<ItemStack>()
{
@Override
public int compare(ItemStack itemStack1, ItemStack itemStack2)
{
return idComparator.compare(itemStack1, itemStack2) * -1;
}
};
public static final Comparator<String> STRING_COMPARATOR = String::compareToIgnoreCase;
public static Comparator<ItemStack> idComparator = new Comparator<ItemStack>() {
public static final Comparator<ItemStack> ID_COMPARATOR = new Comparator<ItemStack>() {
@Override
public int compare(ItemStack itemStack1, ItemStack itemStack2) {
@ -83,15 +100,16 @@ public class Comparators
}
};
public static Comparator<ItemStack> displayNameComparator = new Comparator<ItemStack>()
{
public static final Comparator<ItemStack> DISPLAY_NAME_COMPARATOR = new Comparator<ItemStack>() {
@Override
public int compare(ItemStack itemStack1, ItemStack itemStack2)
{
if (itemStack1 != null && itemStack2 != null)
{
if (itemStack1.getDisplayName().equalsIgnoreCase(itemStack2.getDisplayName()))
{
return idComparator.compare(itemStack1, itemStack2);
return ID_COMPARATOR.compare(itemStack1, itemStack2);
}
else
{
@ -113,17 +131,8 @@ public class Comparators
}
};
public static Comparator<ItemStack> reverseDisplayNameComparator = new Comparator<ItemStack>()
{
@Override
public int compare(ItemStack itemStack1, ItemStack itemStack2)
{
return displayNameComparator.compare(itemStack1, itemStack2) * -1;
}
};
public static final Comparator<ItemStack> ENERGY_VALUE_ITEM_STACK_COMPARATOR = new Comparator<ItemStack>() {
public static Comparator<ItemStack> energyValueItemStackComparator = new Comparator<ItemStack>()
{
@Override
public int compare(ItemStack itemStack1, ItemStack itemStack2)
{
@ -135,7 +144,7 @@ public class Comparators
}
else
{
return idComparator.compare(itemStack1, itemStack2);
return ID_COMPARATOR.compare(itemStack1, itemStack2);
}
}
else if (itemStack1 != null)
@ -152,13 +161,4 @@ public class Comparators
}
}
};
public static Comparator<ItemStack> reverseEnergyValueComparator = new Comparator<ItemStack>()
{
@Override
public int compare(ItemStack itemStack1, ItemStack itemStack2)
{
return energyValueItemStackComparator.compare(itemStack1, itemStack2) * -1;
}
};
}

View File

@ -22,7 +22,7 @@ public class FilterUtils {
public static Set<ItemStack> filterByDisplayName(Collection<ItemStack> itemStacks, String filterString, NameFilterType filterType, Comparator comparator) {
Set<ItemStack> filteredSet = (comparator != null ? new TreeSet<>(comparator) : new TreeSet<>(Comparators.displayNameComparator));
Set<ItemStack> filteredSet = (comparator != null ? new TreeSet<>(comparator) : new TreeSet<>(Comparators.DISPLAY_NAME_COMPARATOR));
if (itemStacks != null) {
if (filterString == null || filterString.isEmpty()) {
@ -67,7 +67,7 @@ public class FilterUtils {
public static Set<ItemStack> filterByEnergyValue(Collection<ItemStack> itemStacks, EnergyValue valueBound, ValueFilterType filterType, Comparator comparator) {
Set<ItemStack> filteredSet = (comparator != null ? new TreeSet<>(comparator) : new TreeSet<>(Comparators.displayNameComparator));
Set<ItemStack> filteredSet = (comparator != null ? new TreeSet<>(comparator) : new TreeSet<>(Comparators.DISPLAY_NAME_COMPARATOR));
if (itemStacks != null) {

View File

@ -30,7 +30,7 @@ public class ItemHelper {
* @return true if the two ItemStacks are equivalent, false otherwise
*/
public static boolean equals(ItemStack first, ItemStack second) {
return (Comparators.idComparator.compare(first, second) == 0);
return (Comparators.ID_COMPARATOR.compare(first, second) == 0);
}
public static boolean equalsIgnoreStackSize(ItemStack itemStack1, ItemStack itemStack2) {
@ -38,7 +38,7 @@ public class ItemHelper {
}
public static int compare(ItemStack itemStack1, ItemStack itemStack2) {
return Comparators.idComparator.compare(itemStack1, itemStack2);
return Comparators.ID_COMPARATOR.compare(itemStack1, itemStack2);
}
public static String toString(ItemStack itemStack) {