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.
This commit is contained in:
yueh 2015-09-15 16:12:16 +02:00 committed by thatsIch
parent 71223a9d58
commit 3a30ca7570
5 changed files with 398 additions and 116 deletions

View file

@ -40,6 +40,7 @@ import appeng.util.Platform;
import appeng.util.item.AEFluidStack; import appeng.util.item.AEFluidStack;
import appeng.util.item.AEItemStack; import appeng.util.item.AEItemStack;
import appeng.util.item.ItemList; import appeng.util.item.ItemList;
import appeng.util.item.FluidList;
public class ApiStorage implements IStorageHelper public class ApiStorage implements IStorageHelper
@ -66,13 +67,13 @@ public class ApiStorage implements IStorageHelper
@Override @Override
public IItemList<IAEItemStack> createItemList() public IItemList<IAEItemStack> createItemList()
{ {
return new ItemList<IAEItemStack>( IAEItemStack.class ); return new ItemList();
} }
@Override @Override
public IItemList<IAEFluidStack> createFluidList() public IItemList<IAEFluidStack> createFluidList()
{ {
return new ItemList<IAEFluidStack>( IAEFluidStack.class ); return new FluidList();
} }
@Override @Override

View file

@ -0,0 +1,201 @@
/*
* 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.HashMap;
import java.util.Iterator;
import java.util.Map;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IItemList;
public final class FluidList implements IItemList<IAEFluidStack>
{
private final Map<IAEFluidStack, IAEFluidStack> records = new HashMap<IAEFluidStack, IAEFluidStack>();
@Override
public void add( IAEFluidStack option )
{
if( option == null )
{
return;
}
final IAEFluidStack st = this.getFluidRecord( option );
if( st != null )
{
st.add( option );
return;
}
final IAEFluidStack opt = option.copy();
this.putFluidRecord( opt );
}
@Override
public IAEFluidStack findPrecise( IAEFluidStack fluidStack )
{
if( fluidStack == null )
{
return null;
}
return this.getFluidRecord( fluidStack );
}
@Override
public Collection<IAEFluidStack> findFuzzy( IAEFluidStack filter, FuzzyMode fuzzy )
{
if( filter == null )
{
return Collections.emptyList();
}
return Collections.singletonList( this.findPrecise( filter ) );
}
@Override
public boolean isEmpty()
{
return !this.iterator().hasNext();
}
@Override
public void addStorage( IAEFluidStack option )
{
if( option == null )
{
return;
}
final IAEFluidStack st = this.getFluidRecord( option );
if( st != null )
{
st.incStackSize( option.getStackSize() );
return;
}
final IAEFluidStack opt = option.copy();
this.putFluidRecord( opt );
}
/*
* public synchronized void clean() { Iterator<StackType> i = iterator(); while (i.hasNext()) { StackType AEI =
* i.next(); if ( !AEI.isMeaningful() ) i.remove(); } }
*/
@Override
public void addCrafting( IAEFluidStack option )
{
if( option == null )
{
return;
}
final IAEFluidStack st = this.getFluidRecord( option );
if( st != null )
{
st.setCraftable( true );
return;
}
final IAEFluidStack opt = option.copy();
opt.setStackSize( 0 );
opt.setCraftable( true );
this.putFluidRecord( opt );
}
@Override
public void addRequestable( IAEFluidStack option )
{
if( option == null )
{
return;
}
final IAEFluidStack st = this.getFluidRecord( option );
if( st != null )
{
st.setCountRequestable( st.getCountRequestable() + option.getCountRequestable() );
return;
}
final IAEFluidStack opt = option.copy();
opt.setStackSize( 0 );
opt.setCraftable( false );
opt.setCountRequestable( option.getCountRequestable() );
this.putFluidRecord( opt );
}
@Override
public IAEFluidStack getFirstItem()
{
for( IAEFluidStack stackType : this )
{
return stackType;
}
return null;
}
@Override
public int size()
{
return this.records.values().size();
}
@Override
public Iterator<IAEFluidStack> iterator()
{
return new MeaningfulFluidIterator<IAEFluidStack>( this.records.values().iterator() );
}
@Override
public void resetStatus()
{
for( IAEFluidStack i : this )
{
i.reset();
}
}
private IAEFluidStack getFluidRecord( IAEFluidStack fluid )
{
return this.records.get( fluid );
}
private IAEFluidStack putFluidRecord( IAEFluidStack fluid )
{
return this.records.put( fluid, fluid );
}
}

View file

@ -1,6 +1,6 @@
/* /*
* This file is part of Applied Energistics 2. * This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved. * Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
* *
* Applied Energistics 2 is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Lesser General Public License as published by
@ -19,125 +19,84 @@
package appeng.util.item; package appeng.util.item;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.Map;
import java.util.NavigableMap; import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ConcurrentSkipListMap;
import net.minecraft.item.Item;
import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.OreDictionary;
import appeng.api.config.FuzzyMode; import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList; import appeng.api.storage.data.IItemList;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
public final class ItemList<StackType extends IAEStack> implements IItemList<StackType> public final class ItemList implements IItemList<IAEItemStack>
{ {
private final NavigableMap<StackType, StackType> records = new ConcurrentSkipListMap<StackType, StackType>(); private final Map<Item, NavigableMap<IAEItemStack, IAEItemStack>> records = new IdentityHashMap<Item, NavigableMap<IAEItemStack, IAEItemStack>>();
private final Class<? extends IAEStack> clz;
// private int currentPriority = Integer.MIN_VALUE;
public Throwable stacktrace;
int iteration = Integer.MIN_VALUE;
public ItemList( Class<? extends IAEStack> cla )
{
this.clz = cla;
}
@Override @Override
public synchronized void add( StackType option ) public void add( IAEItemStack option )
{ {
if( this.checkStackType( option ) ) if( option == null )
{ {
return; return;
} }
StackType st = this.records.get( option ); final IAEItemStack st = this.getItemRecord( option.getItem() ).get( option );
if( st != null ) if( st != null )
{ {
// st.setPriority( currentPriority );
st.add( option ); st.add( option );
return; return;
} }
StackType opt = (StackType) option.copy(); final IAEItemStack opt = option.copy();
// opt.setPriority( currentPriority );
this.records.put( opt, opt );
}
private boolean checkStackType( StackType st ) this.putItemRecord( opt );
{
if( st == null )
{
return true;
}
if( !this.clz.isInstance( st ) )
{
throw new IllegalArgumentException( "WRONG TYPE - got " + st.getClass().getName() + " expected " + this.clz.getName() );
}
return false;
} }
@Override @Override
public synchronized StackType findPrecise( StackType i ) public IAEItemStack findPrecise( IAEItemStack itemStack )
{ {
if( this.checkStackType( i ) ) if( itemStack == null )
{ {
return null; return null;
} }
StackType is = this.records.get( i ); return this.getItemRecord( itemStack.getItem() ).get( itemStack );
if( is != null )
{
return is;
}
return null;
} }
@Override @Override
public Collection<StackType> findFuzzy( StackType filter, FuzzyMode fuzzy ) public Collection<IAEItemStack> findFuzzy( IAEItemStack filter, FuzzyMode fuzzy )
{ {
if( this.checkStackType( filter ) ) if( filter == null )
{ {
return new ArrayList<StackType>(); return Collections.emptyList();
} }
if( filter instanceof IAEFluidStack ) final AEItemStack ais = (AEItemStack) filter;
{
List<StackType> result = Lists.newArrayList();
if( filter.equals( this ) )
{
result.add( filter );
}
return result;
}
AEItemStack ais = (AEItemStack) filter;
if( ais.isOre() ) if( ais.isOre() )
{ {
OreReference or = ais.def.isOre; final OreReference or = ais.def.isOre;
if( or.getAEEquivalents().size() == 1 ) if( or.getAEEquivalents().size() == 1 )
{ {
IAEItemStack is = or.getAEEquivalents().get( 0 ); final IAEItemStack is = or.getAEEquivalents().get( 0 );
return this.findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE ); return this.findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE );
} }
else else
{ {
Collection<StackType> output = new LinkedList<StackType>(); final Collection<IAEItemStack> output = new LinkedList<IAEItemStack>();
for( IAEItemStack is : or.getAEEquivalents() ) for( IAEItemStack is : or.getAEEquivalents() )
{ {
@ -157,123 +116,141 @@ public final class ItemList<StackType extends IAEStack> implements IItemList<Sta
return !this.iterator().hasNext(); return !this.iterator().hasNext();
} }
public Collection<StackType> findFuzzyDamage( AEItemStack filter, FuzzyMode fuzzy, boolean ignoreMeta )
{
StackType low = (StackType) filter.getLow( fuzzy, ignoreMeta );
StackType high = (StackType) filter.getHigh( fuzzy, ignoreMeta );
return this.records.subMap( low, true, high, true ).descendingMap().values();
}
@Override @Override
public synchronized void addStorage( StackType option ) // adds a stack as public void addStorage( IAEItemStack option )
// stored.
{ {
if( this.checkStackType( option ) ) if( option == null )
{ {
return; return;
} }
StackType st = this.records.get( option ); final IAEItemStack st = this.getItemRecord( option.getItem() ).get( option );
if( st != null ) if( st != null )
{ {
// st.setPriority( currentPriority );
st.incStackSize( option.getStackSize() ); st.incStackSize( option.getStackSize() );
return; return;
} }
StackType opt = (StackType) option.copy(); final IAEItemStack opt = option.copy();
// opt.setPriority( currentPriority );
this.records.put( opt, opt ); this.putItemRecord( opt );
} }
/* /*
* public synchronized void clean() { Iterator<StackType> i = iterator(); while (i.hasNext()) { StackType AEI = * public void clean() { Iterator<StackType> i = iterator(); while (i.hasNext()) { StackType AEI =
* i.next(); if ( !AEI.isMeaningful() ) i.remove(); } } * i.next(); if ( !AEI.isMeaningful() ) i.remove(); } }
*/ */
@Override @Override
public synchronized void addCrafting( StackType option ) // adds a stack as public void addCrafting( IAEItemStack option )
// craftable.
{ {
if( this.checkStackType( option ) ) if( option == null )
{ {
return; return;
} }
StackType st = this.records.get( option ); final IAEItemStack st = this.getItemRecord( option.getItem() ).get( option );
if( st != null ) if( st != null )
{ {
// st.setPriority( currentPriority );
st.setCraftable( true ); st.setCraftable( true );
return; return;
} }
StackType opt = (StackType) option.copy(); final IAEItemStack opt = option.copy();
// opt.setPriority( currentPriority );
opt.setStackSize( 0 ); opt.setStackSize( 0 );
opt.setCraftable( true ); opt.setCraftable( true );
this.records.put( opt, opt ); this.putItemRecord( opt );
} }
@Override @Override
public synchronized void addRequestable( StackType option ) // adds a stack public void addRequestable( IAEItemStack option )
// as
// requestable.
{ {
if( this.checkStackType( option ) ) if( option == null )
{ {
return; return;
} }
StackType st = this.records.get( option ); final IAEItemStack st = this.getItemRecord( option.getItem() ).get( option );
if( st != null ) if( st != null )
{ {
// st.setPriority( currentPriority ); st.setCountRequestable( st.getCountRequestable() + option.getCountRequestable() );
( (IAEItemStack) st ).setCountRequestable( st.getCountRequestable() + option.getCountRequestable() );
return; return;
} }
StackType opt = (StackType) option.copy(); final IAEItemStack opt = option.copy();
// opt.setPriority( currentPriority );
opt.setStackSize( 0 ); opt.setStackSize( 0 );
opt.setCraftable( false ); opt.setCraftable( false );
opt.setCountRequestable( opt.getCountRequestable() ); opt.setCountRequestable( option.getCountRequestable() );
this.records.put( opt, opt ); this.putItemRecord( opt );
} }
@Override @Override
public synchronized StackType getFirstItem() public IAEItemStack getFirstItem()
{ {
for( StackType stackType : this ) for( IAEItemStack stackType : this )
{ {
return stackType; return stackType;
} }
return null; return null;
} }
@Override @Override
public synchronized int size() public int size()
{ {
return this.records.values().size(); int size = 0;
for( Map<IAEItemStack, IAEItemStack> element : this.records.values() )
{
size += element.size();
}
return size;
} }
@Override @Override
public synchronized Iterator<StackType> iterator() public Iterator<IAEItemStack> iterator()
{ {
return new MeaningfulIterator<StackType>( this.records.values().iterator() ); return new MeaningfulItemIterator<IAEItemStack>( this.records.values().iterator() );
} }
@Override @Override
public synchronized void resetStatus() public void resetStatus()
{ {
for( StackType i : this ) for( IAEItemStack i : this )
{ {
i.reset(); 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();
}
} }

View file

@ -1,6 +1,6 @@
/* /*
* This file is part of Applied Energistics 2. * This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved. * Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
* *
* Applied Energistics 2 is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Lesser General Public License as published by
@ -20,17 +20,18 @@ package appeng.util.item;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException;
import appeng.api.storage.data.IAEStack; import appeng.api.storage.data.IAEStack;
public class MeaningfulIterator<StackType extends IAEStack> implements Iterator<StackType> public class MeaningfulFluidIterator<T extends IAEStack> implements Iterator<T>
{ {
private final Iterator<StackType> parent; private final Iterator<T> parent;
private StackType next; private T next;
public MeaningfulIterator( Iterator<StackType> iterator ) public MeaningfulFluidIterator( Iterator<T> iterator )
{ {
this.parent = iterator; this.parent = iterator;
} }
@ -51,12 +52,18 @@ public class MeaningfulIterator<StackType extends IAEStack> implements Iterator<
} }
} }
this.next = null;
return false; return false;
} }
@Override @Override
public StackType next() public T next()
{ {
if( this.next == null )
{
throw new NoSuchElementException();
}
return this.next; return this.next;
} }

View file

@ -0,0 +1,96 @@
/*
* 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.Iterator;
import java.util.NavigableMap;
import java.util.NoSuchElementException;
import appeng.api.storage.data.IAEItemStack;
public class MeaningfulItemIterator<T extends IAEItemStack> implements Iterator<T>
{
private final Iterator<NavigableMap<T, T>> parent;
private Iterator<T> innerIterater = null;
private T next;
public MeaningfulItemIterator( Iterator<NavigableMap<T, T>> iterator )
{
this.parent = iterator;
if( this.parent.hasNext() )
{
this.innerIterater = this.parent.next().values().iterator();
}
}
@Override
public boolean hasNext()
{
if( this.innerIterater == null )
{
return false;
}
while( this.innerIterater.hasNext() || this.parent.hasNext() )
{
if( this.innerIterater.hasNext() )
{
this.next = this.innerIterater.next();
if( this.next.isMeaningful() )
{
return true;
}
else
{
this.innerIterater.remove(); // self cleaning :3
}
}
if( this.parent.hasNext() )
{
this.innerIterater = this.parent.next().values().iterator();
}
}
this.next = null;
return false;
}
@Override
public T next()
{
if( this.next == null )
{
throw new NoSuchElementException();
}
return this.next;
}
@Override
public void remove()
{
this.parent.remove();
}
}