Applied-Energistics-2-tiler.../src/main/java/appeng/util/item/ItemList.java
yueh 1bec11f616 ItemList refactoring
Splitted the ItemList and MeaningfulIterator into an item and fluid
version.
Added an IdentityHashMap as additional item layer to the ItemList for a
faster access.
Refactored FluidList, findFuzzy will now return the same fluid instead of
an empty collection.
2015-09-26 19:00:59 +02:00

256 lines
5.7 KiB
Java

/*
* 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.util.item;
import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import net.minecraft.item.Item;
import net.minecraftforge.oredict.OreDictionary;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
public final class ItemList implements IItemList<IAEItemStack>
{
private final Map<Item, NavigableMap<IAEItemStack, IAEItemStack>> records = new IdentityHashMap<Item, NavigableMap<IAEItemStack, IAEItemStack>>();
@Override
public void add( IAEItemStack option )
{
if( option == null )
{
return;
}
final IAEItemStack st = this.getItemRecord( option.getItem() ).get( option );
if( st != null )
{
st.add( option );
return;
}
final IAEItemStack opt = option.copy();
this.putItemRecord( opt );
}
@Override
public IAEItemStack findPrecise( IAEItemStack itemStack )
{
if( itemStack == null )
{
return null;
}
return this.getItemRecord( itemStack.getItem() ).get( itemStack );
}
@Override
public Collection<IAEItemStack> findFuzzy( IAEItemStack filter, FuzzyMode fuzzy )
{
if( filter == null )
{
return Collections.emptyList();
}
final AEItemStack ais = (AEItemStack) filter;
if( ais.isOre() )
{
final OreReference or = ais.def.isOre;
if( or.getAEEquivalents().size() == 1 )
{
final IAEItemStack is = or.getAEEquivalents().get( 0 );
return this.findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE );
}
else
{
final Collection<IAEItemStack> output = new LinkedList<IAEItemStack>();
for( IAEItemStack is : or.getAEEquivalents() )
{
output.addAll( this.findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE ) );
}
return output;
}
}
return this.findFuzzyDamage( ais, fuzzy, false );
}
@Override
public boolean isEmpty()
{
return !this.iterator().hasNext();
}
@Override
public void addStorage( IAEItemStack option )
{
if( option == null )
{
return;
}
final IAEItemStack st = this.getItemRecord( option.getItem() ).get( option );
if( st != null )
{
st.incStackSize( option.getStackSize() );
return;
}
final IAEItemStack opt = option.copy();
this.putItemRecord( opt );
}
/*
* public void clean() { Iterator<StackType> i = iterator(); while (i.hasNext()) { StackType AEI =
* i.next(); if ( !AEI.isMeaningful() ) i.remove(); } }
*/
@Override
public void addCrafting( IAEItemStack option )
{
if( option == null )
{
return;
}
final IAEItemStack st = this.getItemRecord( option.getItem() ).get( option );
if( st != null )
{
st.setCraftable( true );
return;
}
final IAEItemStack opt = option.copy();
opt.setStackSize( 0 );
opt.setCraftable( true );
this.putItemRecord( opt );
}
@Override
public void addRequestable( IAEItemStack option )
{
if( option == null )
{
return;
}
final IAEItemStack st = this.getItemRecord( option.getItem() ).get( option );
if( st != null )
{
st.setCountRequestable( st.getCountRequestable() + option.getCountRequestable() );
return;
}
final IAEItemStack opt = option.copy();
opt.setStackSize( 0 );
opt.setCraftable( false );
opt.setCountRequestable( option.getCountRequestable() );
this.putItemRecord( opt );
}
@Override
public IAEItemStack getFirstItem()
{
for( IAEItemStack stackType : this )
{
return stackType;
}
return null;
}
@Override
public int size()
{
int size = 0;
for( Map<IAEItemStack, IAEItemStack> element : this.records.values() )
{
size += element.size();
}
return size;
}
@Override
public Iterator<IAEItemStack> iterator()
{
return new MeaningfulItemIterator<IAEItemStack>( this.records.values().iterator() );
}
@Override
public void resetStatus()
{
for( IAEItemStack i : this )
{
i.reset();
}
}
private NavigableMap<IAEItemStack, IAEItemStack> getItemRecord( Item item )
{
NavigableMap<IAEItemStack, IAEItemStack> itemRecords = this.records.get( item );
if( itemRecords == null )
{
itemRecords = new ConcurrentSkipListMap<IAEItemStack, IAEItemStack>();
this.records.put( item, itemRecords );
}
return itemRecords;
}
private IAEItemStack putItemRecord( IAEItemStack itemStack )
{
return this.getItemRecord( itemStack.getItem() ).put( itemStack, itemStack );
}
private Collection<IAEItemStack> findFuzzyDamage( AEItemStack filter, FuzzyMode fuzzy, boolean ignoreMeta )
{
final IAEItemStack low = filter.getLow( fuzzy, ignoreMeta );
final IAEItemStack high = filter.getHigh( fuzzy, ignoreMeta );
return this.getItemRecord( filter.getItem() ).subMap( low, true, high, true ).descendingMap().values();
}
}