<commit_before>package mezz.jei.util;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.collect.Table;
import mezz.jei.RecipeRegistry;
import mezz.jei.api.recipe.IRecipeType;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * A RecipeMap efficiently links Recipes, IRecipeTypes, and ItemStacks.
 */
public class RecipeMap {

	@Nonnull
	private final Table<IRecipeType, String, List<Object>> recipeTable = HashBasedTable.create();
	@Nonnull
	private final ArrayListMultimap<String, IRecipeType> typeMap = ArrayListMultimap.create();
	@Nonnull
	private final Comparator<IRecipeType> recipeTypeComparator;

	public RecipeMap(final RecipeRegistry recipeRegistry) {
		this.recipeTypeComparator = new Comparator<IRecipeType>() {
			public int compare(IRecipeType recipeType1, IRecipeType recipeType2) {
				int index1 = recipeRegistry.getRecipeTypeIndex(recipeType1);
				int index2 = recipeRegistry.getRecipeTypeIndex(recipeType2);
				return Integer.compare(index1, index2);
			}
		};
	}

	@Nonnull
	public ImmutableList<IRecipeType> getRecipeTypes(@Nonnull ItemStack itemStack) {
		List<IRecipeType> recipeTypes = new ArrayList<IRecipeType>();
		for (String stackKey : getNamesWithWildcard(itemStack)) {
			recipeTypes.addAll(typeMap.get(stackKey));
		}
		return Ordering.from(recipeTypeComparator).immutableSortedCopy(recipeTypes);
	}

	private void addRecipeType(@Nonnull IRecipeType recipeType, @Nonnull ItemStack itemStack) {
		String stackKey = getName(itemStack);
		List<IRecipeType> recipeTypes = typeMap.get(stackKey);
		if (!recipeTypes.contains(recipeType))
			recipeTypes.add(recipeType);
	}

	@Nonnull
	private List<String> getNamesWithWildcard(@Nonnull ItemStack itemStack) {
		List<String> names = new ArrayList<String>(2);
		names.add(getName(itemStack));
		names.add(getWildcardName(itemStack));
		return names;
	}

	@Nonnull
	private String getName(@Nonnull ItemStack itemStack) {
		int meta = itemStack.getItemDamage();
		if (meta == OreDictionary.WILDCARD_VALUE) {<commit_msg>Remove duplicates from recipe types list<commit_after>package mezz.jei.util;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.collect.Table;
import mezz.jei.RecipeRegistry;
import mezz.jei.api.recipe.IRecipeType;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * A RecipeMap efficiently links Recipes, IRecipeTypes, and ItemStacks.
 */
public class RecipeMap {

	@Nonnull
	private final Table<IRecipeType, String, List<Object>> recipeTable = HashBasedTable.create();
	@Nonnull
	private final ArrayListMultimap<String, IRecipeType> typeMap = ArrayListMultimap.create();
	@Nonnull
	private final Ordering<IRecipeType> recipeTypeOrdering;

	public RecipeMap(final RecipeRegistry recipeRegistry) {
		Comparator<IRecipeType> recipeTypeComparator = new Comparator<IRecipeType>() {
			public int compare(IRecipeType recipeType1, IRecipeType recipeType2) {
				int index1 = recipeRegistry.getRecipeTypeIndex(recipeType1);
				int index2 = recipeRegistry.getRecipeTypeIndex(recipeType2);
				return Integer.compare(index1, index2);
			}
		};
		this.recipeTypeOrdering = Ordering.from(recipeTypeComparator);
	}

	@Nonnull
	public ImmutableList<IRecipeType> getRecipeTypes(@Nonnull ItemStack itemStack) {
		ImmutableSet.Builder<IRecipeType> recipeTypeBuilder = ImmutableSet.builder();
		for (String stackKey : getNamesWithWildcard(itemStack)) {
			recipeTypeBuilder.addAll(typeMap.get(stackKey));
		}
		ImmutableSet<IRecipeType> recipeTypes = recipeTypeBuilder.build();
		return recipeTypeOrdering.immutableSortedCopy(recipeTypes);
	}

	private void addRecipeType(@Nonnull IRecipeType recipeType, @Nonnull ItemStack itemStack) {
		String stackKey = getName(itemStack);
		List<IRecipeType> recipeTypes = typeMap.get(stackKey);
		if (!recipeTypes.contains(recipeType))
			recipeTypes.add(recipeType);
	}

	@Nonnull
	private List<String> getNamesWithWildcard(@Nonnull ItemStack itemStack) {
		List<String> names = new ArrayList<String>(2);
		names.add(getName(itemStack));
		names.add(getWildcardName(itemStack));
		return names;
	}

	@Nonnull
	private String getName(@Nonnull ItemStack itemStack) {
		int meta = itemStack.getItemDamage();
		if (meta == OreDictionary.WILDCARD_VALUE) {
{'fraction_non_alphanumeric': 0.10148415563578018, 'fraction_numerical': 0.0036101083032490976, 'mean_word_length': 6.333823529411765, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5889274', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1591, 'n_words': 302}