From fbfd13d16310f0d6508a162d50db8ba8cc7f69f1 Mon Sep 17 00:00:00 2001 From: yueh Date: Sat, 28 Nov 2015 11:12:31 +0100 Subject: [PATCH] Reverts some of the changes to ItemList. Removes the split collection as in some rare corner cases the insertion order is important to maintain the correct data in terms of self cleaning. Keeps the general cleanups and split of Fluid/ItemList. Fixes #1964 --- .../java/appeng/me/storage/CellInventory.java | 6 +- src/main/java/appeng/util/item/ItemList.java | 41 +--- .../util/item/MeaningfulItemIterator.java | 36 +--- .../appeng/util/item/UnsortedItemList.java | 179 ------------------ 4 files changed, 20 insertions(+), 242 deletions(-) delete mode 100644 src/main/java/appeng/util/item/UnsortedItemList.java diff --git a/src/main/java/appeng/me/storage/CellInventory.java b/src/main/java/appeng/me/storage/CellInventory.java index 7ee587bd..d744373c 100644 --- a/src/main/java/appeng/me/storage/CellInventory.java +++ b/src/main/java/appeng/me/storage/CellInventory.java @@ -29,6 +29,7 @@ import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.oredict.OreDictionary; +import appeng.api.AEApi; import appeng.api.config.Actionable; import appeng.api.config.FuzzyMode; import appeng.api.exceptions.AppEngException; @@ -43,7 +44,6 @@ import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IItemList; import appeng.util.Platform; import appeng.util.item.AEItemStack; -import appeng.util.item.UnsortedItemList; public class CellInventory implements ICellInventory @@ -191,7 +191,7 @@ public class CellInventory implements ICellInventory private boolean isEmpty( final IMEInventory meInventory ) { - return meInventory.getAvailableItems( new UnsortedItemList() ).isEmpty(); + return meInventory.getAvailableItems( AEApi.instance().storage().createItemList() ).isEmpty(); } @Override @@ -446,7 +446,7 @@ public class CellInventory implements ICellInventory { if( this.cellItems == null ) { - this.cellItems = new UnsortedItemList(); + this.cellItems = AEApi.instance().storage().createItemList(); } this.cellItems.resetStatus(); // clears totals and stuff. diff --git a/src/main/java/appeng/util/item/ItemList.java b/src/main/java/appeng/util/item/ItemList.java index e0900ebb..61ae3990 100644 --- a/src/main/java/appeng/util/item/ItemList.java +++ b/src/main/java/appeng/util/item/ItemList.java @@ -21,14 +21,11 @@ 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; @@ -39,7 +36,7 @@ import appeng.api.storage.data.IItemList; public final class ItemList implements IItemList { - private final Map> records = new IdentityHashMap>(); + private final NavigableMap records = new ConcurrentSkipListMap(); @Override public void add( final IAEItemStack option ) @@ -49,7 +46,7 @@ public final class ItemList implements IItemList return; } - final IAEItemStack st = this.getItemRecord( option.getItem() ).get( option ); + final IAEItemStack st = this.records.get( option ); if( st != null ) { @@ -70,7 +67,7 @@ public final class ItemList implements IItemList return null; } - return this.getItemRecord( itemStack.getItem() ).get( itemStack ); + return this.records.get( itemStack ); } @Override @@ -123,7 +120,7 @@ public final class ItemList implements IItemList return; } - final IAEItemStack st = this.getItemRecord( option.getItem() ).get( option ); + final IAEItemStack st = this.records.get( option ); if( st != null ) { @@ -149,7 +146,7 @@ public final class ItemList implements IItemList return; } - final IAEItemStack st = this.getItemRecord( option.getItem() ).get( option ); + final IAEItemStack st = this.records.get( option ); if( st != null ) { @@ -172,7 +169,7 @@ public final class ItemList implements IItemList return; } - final IAEItemStack st = this.getItemRecord( option.getItem() ).get( option ); + final IAEItemStack st = this.records.get( option ); if( st != null ) { @@ -202,14 +199,7 @@ public final class ItemList implements IItemList @Override public int size() { - int size = 0; - - for( final Map element : this.records.values() ) - { - size += element.size(); - } - - return size; + return this.records.size(); } @Override @@ -227,22 +217,9 @@ public final class ItemList implements IItemList } } - private NavigableMap getItemRecord( final Item item ) - { - NavigableMap itemRecords = this.records.get( item ); - - if( itemRecords == null ) - { - itemRecords = new ConcurrentSkipListMap(); - this.records.put( item, itemRecords ); - } - - return itemRecords; - } - private IAEItemStack putItemRecord( final IAEItemStack itemStack ) { - return this.getItemRecord( itemStack.getItem() ).put( itemStack, itemStack ); + return this.records.put( itemStack, itemStack ); } private Collection findFuzzyDamage( final AEItemStack filter, final FuzzyMode fuzzy, final boolean ignoreMeta ) @@ -250,6 +227,6 @@ public final class ItemList implements IItemList 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(); + return this.records.subMap( low, true, high, true ).descendingMap().values(); } } diff --git a/src/main/java/appeng/util/item/MeaningfulItemIterator.java b/src/main/java/appeng/util/item/MeaningfulItemIterator.java index 94d389cb..7d7c8b01 100644 --- a/src/main/java/appeng/util/item/MeaningfulItemIterator.java +++ b/src/main/java/appeng/util/item/MeaningfulItemIterator.java @@ -20,7 +20,6 @@ package appeng.util.item; import java.util.Iterator; -import java.util.NavigableMap; import java.util.NoSuchElementException; import appeng.api.storage.data.IAEItemStack; @@ -29,47 +28,28 @@ import appeng.api.storage.data.IAEItemStack; public class MeaningfulItemIterator implements Iterator { - private final Iterator> parent; - private Iterator innerIterater = null; + private final Iterator parent; private T next; - public MeaningfulItemIterator( final Iterator> iterator ) + public MeaningfulItemIterator( final Iterator iterator ) { this.parent = iterator; - - if( this.parent.hasNext() ) - { - this.innerIterater = this.parent.next().values().iterator(); - } } @Override public boolean hasNext() { - if( this.innerIterater == null ) + while( this.parent.hasNext() ) { - return false; - } + this.next = this.parent.next(); - while( this.innerIterater.hasNext() || this.parent.hasNext() ) - { - if( this.innerIterater.hasNext() ) + if( this.next.isMeaningful() ) { - this.next = this.innerIterater.next(); - - if( this.next.isMeaningful() ) - { - return true; - } - else - { - this.innerIterater.remove(); // self cleaning :3 - } + return true; } - - if( this.parent.hasNext() ) + else { - this.innerIterater = this.parent.next().values().iterator(); + this.parent.remove(); // self cleaning :3 } } diff --git a/src/main/java/appeng/util/item/UnsortedItemList.java b/src/main/java/appeng/util/item/UnsortedItemList.java deleted file mode 100644 index 5e913bcf..00000000 --- a/src/main/java/appeng/util/item/UnsortedItemList.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * 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 . - */ - -package appeng.util.item; - - -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -import javax.annotation.Nonnull; - -import com.google.common.base.Predicate; -import com.google.common.collect.Iterators; - -import appeng.api.config.FuzzyMode; -import appeng.api.storage.data.IAEItemStack; -import appeng.api.storage.data.IItemList; - - -/** - * An unsorted {@link IItemList} providing constant access time instead of logarithmic time. - * - * As tradeoff it will no longer support fuzzy operations. - * Also no advanced features like storing craftable or requestable items is supported. - * - */ -public final class UnsortedItemList implements IItemList -{ - - /** - * {@link Predicate} to filter all meaningful entries with {@link Iterators} - */ - private static final Predicate MEANINGFUL_PREDICATE = new Predicate() - { - - @Override - public boolean apply( @Nonnull final IAEItemStack input ) - { - return input.isMeaningful(); - } - }; - - private final Map records = new HashMap(); - - @Override - public void add( final IAEItemStack option ) - { - if( option == null ) - { - return; - } - - final IAEItemStack st = this.records.get( option ); - - if( st != null ) - { - st.add( option ); - return; - } - - final IAEItemStack opt = option.copy(); - - this.records.put( opt, opt ); - } - - @Override - public IAEItemStack findPrecise( final IAEItemStack itemStack ) - { - if( itemStack == null ) - { - return null; - } - - return this.records.get( itemStack ); - } - - @Override - public boolean isEmpty() - { - return !this.iterator().hasNext(); - } - - @Override - public int size() - { - return this.records.size(); - } - - @Override - public Iterator iterator() - { - return Iterators.filter( this.records.values().iterator(), MEANINGFUL_PREDICATE ); - } - - @Override - public IAEItemStack getFirstItem() - { - for( final IAEItemStack stackType : this ) - { - return stackType; - } - - return null; - } - - @Override - public void resetStatus() - { - for( final IAEItemStack i : this ) - { - i.reset(); - } - } - - /** - * Unsupported due to being a unsorted collection and thus only solvable in linear or worse time. - * - * @deprecated to indicate this method is unsupported. - */ - @Override - @Deprecated - public Collection findFuzzy( final IAEItemStack filter, final FuzzyMode fuzzy ) - { - throw new UnsupportedOperationException( "Unsupported on an unsorted collection" ); - } - - /** - * Unsupported to avoid being used as anything but a plain collection to store real item stacks. - * - * @deprecated to indicate this method is unsupported. - */ - @Override - @Deprecated - public void addStorage( final IAEItemStack option ) - { - throw new UnsupportedOperationException( "Purely designed for item storage" ); - } - - /** - * Unsupported to avoid being used as anything but a plain collection to store real item stacks. - * - * @deprecated to indicate this method is unsupported. - */ - @Override - @Deprecated - public void addCrafting( final IAEItemStack option ) - { - throw new UnsupportedOperationException( "Purely designed for item storage" ); - } - - /** - * Unsupported to avoid being used as anything but a plain collection to store real item stacks. - * - * @deprecated to indicate this method is unsupported. - */ - @Override - @Deprecated - public void addRequestable( final IAEItemStack option ) - { - throw new UnsupportedOperationException( "Purely designed for item storage" ); - } -}