Merge pull request #1604 from yueh/e-immutable-inscriber-recipes

Changed to immutable list to prevent direct modifcations
This commit is contained in:
yueh 2015-07-14 21:28:41 +02:00
commit ab9f4e7fd7
2 changed files with 45 additions and 16 deletions

View file

@ -1,10 +1,15 @@
package appeng.api.features; package appeng.api.features;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import com.google.common.base.Optional;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -12,22 +17,26 @@ import net.minecraft.item.ItemStack;
* Lets you manipulate Inscriber Recipes, by adding or editing existing ones. * Lets you manipulate Inscriber Recipes, by adding or editing existing ones.
* *
* @author thatsIch * @author thatsIch
* @version rv2 * @version rv3
* @since rv2 * @since rv2
*/ */
public interface IInscriberRegistry public interface IInscriberRegistry
{ {
/** /**
* Current list of registered recipes, you can modify this if you want too. * An immutable copy of currently registered recipes.
* Will never contain a null recipe *
* Use the provided methods to actually modify the inscriber recipes.
*
* @see IInscriberRegistry#addRecipe(IInscriberRecipe)
* @see IInscriberRegistry#removeRecipe(IInscriberRecipe)
* *
* @return currentlyRegisteredRecipes * @return currentlyRegisteredRecipes
*/ */
@Nonnull @Nonnull
List<IInscriberRecipe> getRecipes(); Collection<IInscriberRecipe> getRecipes();
/** /**
* Optional items which are used in the top or bottom slot * Optional items which are used in the top or bottom slot.
* *
* @return set of all optional items * @return set of all optional items
*/ */
@ -35,7 +44,7 @@ public interface IInscriberRegistry
Set<ItemStack> getOptionals(); Set<ItemStack> getOptionals();
/** /**
* Get all registered items which are valid inputs * Get all registered items which are valid inputs.
* *
* @return set of all input items * @return set of all input items
*/ */
@ -43,7 +52,7 @@ public interface IInscriberRegistry
Set<ItemStack> getInputs(); Set<ItemStack> getInputs();
/** /**
* Extensible way to create an inscriber recipe * Extensible way to create an inscriber recipe.
* *
* @return builder for inscriber recipes * @return builder for inscriber recipes
*/ */
@ -61,9 +70,10 @@ public interface IInscriberRegistry
void addRecipe( IInscriberRecipe recipe ); void addRecipe( IInscriberRecipe recipe );
/** /**
* Removes a recipe from the registry * Removes all equal recipes from the registry.
* *
* @param toBeRemovedRecipe to be removed recipe, can be null, makes just no sense * @param toBeRemovedRecipe to be removed recipe, can be null, makes just no sense.
*/ */
void removeRecipe( IInscriberRecipe toBeRemovedRecipe ); void removeRecipe( IInscriberRecipe toBeRemovedRecipe );
} }

View file

@ -1,8 +1,27 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.core.features.registries; package appeng.core.features.registries;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -21,12 +40,12 @@ import appeng.core.features.registries.entries.InscriberRecipe;
/** /**
* @author thatsIch * @author thatsIch
* @version rv2 * @version rv3
* @since rv2 * @since rv2
*/ */
public final class InscriberRegistry implements IInscriberRegistry public final class InscriberRegistry implements IInscriberRegistry
{ {
private final List<IInscriberRecipe> recipes; private final Set<IInscriberRecipe> recipes;
private final Set<ItemStack> optionals; private final Set<ItemStack> optionals;
private final Set<ItemStack> inputs; private final Set<ItemStack> inputs;
@ -34,14 +53,14 @@ public final class InscriberRegistry implements IInscriberRegistry
{ {
this.inputs = new HashSet<ItemStack>(); this.inputs = new HashSet<ItemStack>();
this.optionals = new HashSet<ItemStack>(); this.optionals = new HashSet<ItemStack>();
this.recipes = new ArrayList<IInscriberRecipe>(); this.recipes = new HashSet<IInscriberRecipe>();
} }
@Nonnull @Nonnull
@Override @Override
public List<IInscriberRecipe> getRecipes() public Collection<IInscriberRecipe> getRecipes()
{ {
return this.recipes; return Collections.unmodifiableCollection( this.recipes );
} }
@Nonnull @Nonnull
@ -168,11 +187,11 @@ public final class InscriberRegistry implements IInscriberRegistry
{ {
throw new IllegalStateException( "Output must be defined." ); throw new IllegalStateException( "Output must be defined." );
} }
if ( this.topOptional == null && this.bottomOptional == null ) if( this.topOptional == null && this.bottomOptional == null )
{ {
throw new IllegalStateException( "One optional must be defined." ); throw new IllegalStateException( "One optional must be defined." );
} }
if ( this.type == null ) if( this.type == null )
{ {
throw new IllegalStateException( "Process type must be defined." ); throw new IllegalStateException( "Process type must be defined." );
} }