From 060255e9c77e8d6a43e8f912cb2f0fbfc2efb119 Mon Sep 17 00:00:00 2001 From: thatsIch Date: Sun, 5 Oct 2014 15:38:56 +0200 Subject: [PATCH 1/4] Reduce visibility of fields Implement against interfaces Enforce immutability instead resetting it hard via new instantiation. --- .../appeng/me/cache/CraftingGridCache.java | 40 ++++++------------- .../java/appeng/me/cache/NetworkMonitor.java | 3 +- .../implementations/CraftingCPUCluster.java | 2 +- .../me/helpers/GenericInterestManager.java | 8 ++-- 4 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/main/java/appeng/me/cache/CraftingGridCache.java b/src/main/java/appeng/me/cache/CraftingGridCache.java index f9d16f8f..a3f94cc2 100644 --- a/src/main/java/appeng/me/cache/CraftingGridCache.java +++ b/src/main/java/appeng/me/cache/CraftingGridCache.java @@ -1,21 +1,13 @@ package appeng.me.cache; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; +import java.util.*; import java.util.Map.Entry; -import java.util.Set; -import java.util.TreeSet; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; +import com.google.common.collect.*; import net.minecraft.world.World; import appeng.api.config.AccessRestriction; import appeng.api.config.Actionable; @@ -58,31 +50,25 @@ import appeng.tile.crafting.TileCraftingStorageTile; import appeng.tile.crafting.TileCraftingTile; import appeng.util.ItemSorters; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.ImmutableCollection; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.SetMultimap; - public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper, ICellProvider, IMEInventoryHandler { - final HashSet cpuClusters = new HashSet(); - final HashSet providers = new HashSet(); + private final Set cpuClusters = new HashSet(); + private final Set providers = new HashSet(); - private final HashMap watchers = new HashMap(); + private final Map watchers = new HashMap(); - final IGrid grid; + private final IGrid grid; IStorageGrid sg; IEnergyGrid eg; - final HashMap> craftingMethods = new HashMap>(); - HashMap> craftableItems = new HashMap>(); - final HashSet emitableItems = new HashSet(); - final HashMap links = new HashMap(); + private final Map> craftingMethods = new HashMap>(); + private final Map> craftableItems = new HashMap>(); + private final Set emitableItems = new HashSet(); + private final Map links = new HashMap(); boolean updateList = false; - final private SetMultimap interests = HashMultimap.create(); + private final Multimap interests = HashMultimap.create(); final public GenericInterestManager interestManager = new GenericInterestManager( interests ); static class ActiveCpuIterator implements Iterator @@ -296,11 +282,11 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper private void updatePatterns() { - HashMap> oldItems = craftableItems; + Map> oldItems = craftableItems; // erase list. craftingMethods.clear(); - craftableItems = new HashMap>(); + craftableItems.clear(); emitableItems.clear(); // update the stuff that was in the list... diff --git a/src/main/java/appeng/me/cache/NetworkMonitor.java b/src/main/java/appeng/me/cache/NetworkMonitor.java index 8e2c5bb7..d7b294af 100644 --- a/src/main/java/appeng/me/cache/NetworkMonitor.java +++ b/src/main/java/appeng/me/cache/NetworkMonitor.java @@ -1,5 +1,6 @@ package appeng.me.cache; +import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.Map.Entry; @@ -75,7 +76,7 @@ public class NetworkMonitor> extends MEMonitorHandler if ( myGridCache.interestManager.containsKey( changedItem ) ) { - Set list = myGridCache.interestManager.get( changedItem ); + Collection list = myGridCache.interestManager.get( changedItem ); if ( !list.isEmpty() ) { IAEStack fullStack = myStorageList.findPrecise( changedItem ); diff --git a/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java b/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java index 8ac7a60f..0b5dd529 100644 --- a/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java +++ b/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java @@ -285,7 +285,7 @@ public class CraftingCPUCluster implements IAECluster, ICraftingCPU if ( sg.interestManager.containsKey( diff ) ) { - Set list = sg.interestManager.get( diff ); + Collection list = sg.interestManager.get( diff ); if ( !list.isEmpty() ) { diff --git a/src/main/java/appeng/me/helpers/GenericInterestManager.java b/src/main/java/appeng/me/helpers/GenericInterestManager.java index 37dd217c..ff0483d8 100644 --- a/src/main/java/appeng/me/helpers/GenericInterestManager.java +++ b/src/main/java/appeng/me/helpers/GenericInterestManager.java @@ -1,10 +1,12 @@ package appeng.me.helpers; +import java.util.Collection; import java.util.LinkedList; import java.util.Set; import appeng.api.storage.data.IAEStack; +import com.google.common.collect.Multimap; import com.google.common.collect.SetMultimap; public class GenericInterestManager @@ -24,11 +26,11 @@ public class GenericInterestManager } } - private final SetMultimap container; + private final Multimap container; private LinkedList transactions = null; private int transDepth = 0; - public GenericInterestManager(SetMultimap interests) { + public GenericInterestManager(Multimap interests) { container = interests; } @@ -64,7 +66,7 @@ public class GenericInterestManager return container.containsKey( stack ); } - public Set get(IAEStack stack) + public Collection get(IAEStack stack) { return container.get( stack ); } From 9717450f13bd3e6bfcf8504ca3a5a2f7d27d2872 Mon Sep 17 00:00:00 2001 From: thatsIch Date: Sun, 5 Oct 2014 15:43:52 +0200 Subject: [PATCH 2/4] Added missing Type --- src/main/java/appeng/me/cache/CraftingGridCache.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/appeng/me/cache/CraftingGridCache.java b/src/main/java/appeng/me/cache/CraftingGridCache.java index a3f94cc2..b1330d87 100644 --- a/src/main/java/appeng/me/cache/CraftingGridCache.java +++ b/src/main/java/appeng/me/cache/CraftingGridCache.java @@ -50,7 +50,7 @@ import appeng.tile.crafting.TileCraftingStorageTile; import appeng.tile.crafting.TileCraftingTile; import appeng.util.ItemSorters; -public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper, ICellProvider, IMEInventoryHandler +public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper, ICellProvider, IMEInventoryHandler { private final Set cpuClusters = new HashSet(); @@ -69,7 +69,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper boolean updateList = false; private final Multimap interests = HashMultimap.create(); - final public GenericInterestManager interestManager = new GenericInterestManager( interests ); + public final GenericInterestManager interestManager = new GenericInterestManager( this.interests ); static class ActiveCpuIterator implements Iterator { @@ -362,7 +362,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper } @Override - public IItemList getAvailableItems(IItemList out) + public IItemList getAvailableItems(IItemList out) { // add craftable items! for (IAEItemStack st : craftableItems.keySet()) From f32010a5389218c91dba590a529a5d29b00b0292 Mon Sep 17 00:00:00 2001 From: thatsIch Date: Sun, 5 Oct 2014 15:57:30 +0200 Subject: [PATCH 3/4] Renamed to a more readable version --- .../appeng/me/cache/CraftingGridCache.java | 93 ++++++++++--------- 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/src/main/java/appeng/me/cache/CraftingGridCache.java b/src/main/java/appeng/me/cache/CraftingGridCache.java index b1330d87..fbd0c172 100644 --- a/src/main/java/appeng/me/cache/CraftingGridCache.java +++ b/src/main/java/appeng/me/cache/CraftingGridCache.java @@ -53,19 +53,19 @@ import appeng.util.ItemSorters; public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper, ICellProvider, IMEInventoryHandler { - private final Set cpuClusters = new HashSet(); - private final Set providers = new HashSet(); + private final Set craftingCPUClusters = new HashSet(); + private final Set craftingProviders = new HashSet(); - private final Map watchers = new HashMap(); + private final Map craftingWatchers = new HashMap(); private final IGrid grid; - IStorageGrid sg; - IEnergyGrid eg; + IStorageGrid storageGrid; + IEnergyGrid energyGrid; private final Map> craftingMethods = new HashMap>(); private final Map> craftableItems = new HashMap>(); private final Set emitableItems = new HashSet(); - private final Map links = new HashMap(); + private final Map craftingLinks = new HashMap(); boolean updateList = false; private final Multimap interests = HashMultimap.create(); @@ -74,36 +74,41 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper static class ActiveCpuIterator implements Iterator { - final Iterator i; - CraftingCPUCluster c = null; + private final Iterator iterator; + private CraftingCPUCluster cpuCluster; public ActiveCpuIterator(Collection o) { - i = o.iterator(); + this.iterator = o.iterator(); + this.cpuCluster = null; } @Override public boolean hasNext() { findNext(); - return c != null; + + return this.cpuCluster != null; } private void findNext() { - while (i.hasNext() && c == null) + while (this.iterator.hasNext() && this.cpuCluster == null) { - c = i.next(); - if ( !c.isActive() || c.isDestroyed ) - c = null; + this.cpuCluster = this.iterator.next(); + if ( !this.cpuCluster.isActive() || this.cpuCluster.isDestroyed ) + { + this.cpuCluster = null; + } } } @Override public ICraftingCPU next() { - ICraftingCPU o = c; - c = null; + final ICraftingCPU o = this.cpuCluster; + this.cpuCluster = null; + return o; } @@ -118,21 +123,21 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper @Override public ImmutableSet getCpus() { - return ImmutableSet.copyOf( new ActiveCpuIterator( cpuClusters ) ); + return ImmutableSet.copyOf( new ActiveCpuIterator( this.craftingCPUClusters ) ); } - public CraftingGridCache(IGrid g) + public CraftingGridCache(IGrid grid) { - grid = g; + this.grid = grid; } @MENetworkEventSubscribe - public void afterCacheConstruction(MENetworkPostCacheConstruction cc) + public void afterCacheConstruction(MENetworkPostCacheConstruction cacheConstruction) { - sg = grid.getCache( IStorageGrid.class ); - eg = grid.getCache( IEnergyGrid.class ); + this.storageGrid = this.grid.getCache( IStorageGrid.class ); + this.energyGrid = this.grid.getCache( IEnergyGrid.class ); - sg.registerCellProvider( this ); + this.storageGrid.registerCellProvider( this ); } public void addLink(CraftingLink l) @@ -140,9 +145,9 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper if ( l.isStandalone() ) return; - CraftingLinkNexus n = links.get( l.getCraftingID() ); + CraftingLinkNexus n = craftingLinks.get( l.getCraftingID() ); if ( n == null ) - links.put( l.getCraftingID(), n = new CraftingLinkNexus( l.getCraftingID() ) ); + craftingLinks.put( l.getCraftingID(), n = new CraftingLinkNexus( l.getCraftingID() ) ); l.setNexus( n ); } @@ -156,15 +161,15 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper updateCPUClusters(); } - Iterator i = links.values().iterator(); + Iterator i = craftingLinks.values().iterator(); while (i.hasNext()) { if ( i.next().isDead( grid, this ) ) i.remove(); } - for (CraftingCPUCluster cpu : cpuClusters) - cpu.updateCraftingLogic( grid, eg, this ); + for (CraftingCPUCluster cpu : craftingCPUClusters) + cpu.updateCraftingLogic( grid, energyGrid, this ); } @MENetworkEventSubscribe @@ -184,17 +189,17 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper { if ( machine instanceof ICraftingWatcherHost ) { - ICraftingWatcher myWatcher = watchers.get( machine ); + ICraftingWatcher myWatcher = craftingWatchers.get( machine ); if ( myWatcher != null ) { myWatcher.clear(); - watchers.remove( machine ); + craftingWatchers.remove( machine ); } } if ( machine instanceof ICraftingRequester ) { - for (CraftingLinkNexus n : links.values()) + for (CraftingLinkNexus n : craftingLinks.values()) { if ( n.isMachine( machine ) ) { @@ -207,7 +212,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper updateList = true; if ( machine instanceof ICraftingProvider ) { - providers.remove( machine ); + craftingProviders.remove( machine ); updatePatterns(); } } @@ -219,7 +224,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper { ICraftingWatcherHost swh = (ICraftingWatcherHost) machine; CraftingWatcher iw = new CraftingWatcher( this, swh ); - watchers.put( gridNode, iw ); + craftingWatchers.put( gridNode, iw ); swh.updateWatcher( iw ); } @@ -236,21 +241,21 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper updateList = true; if ( machine instanceof ICraftingProvider ) { - providers.add( (ICraftingProvider) machine ); + craftingProviders.add( (ICraftingProvider) machine ); updatePatterns(); } } private void updateCPUClusters() { - cpuClusters.clear(); + craftingCPUClusters.clear(); for (IGridNode cst : grid.getMachines( TileCraftingStorageTile.class )) { TileCraftingStorageTile tile = (TileCraftingStorageTile) cst.getMachine(); CraftingCPUCluster cluster = (CraftingCPUCluster) tile.getCluster(); if ( cluster != null ) { - cpuClusters.add( cluster ); + craftingCPUClusters.add( cluster ); if ( cluster.myLastLink != null ) addLink( (CraftingLink) cluster.myLastLink ); @@ -290,10 +295,10 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper emitableItems.clear(); // update the stuff that was in the list... - sg.postAlterationOfStoredItems( StorageChannel.ITEMS, oldItems.keySet(), new BaseActionSource() ); + storageGrid.postAlterationOfStoredItems( StorageChannel.ITEMS, oldItems.keySet(), new BaseActionSource() ); // re-create list.. - for (ICraftingProvider cp : providers) + for (ICraftingProvider cp : craftingProviders) cp.provideCrafting( this ); HashMap> tmpCraft = new HashMap>(); @@ -320,7 +325,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper for (Entry> e : tmpCraft.entrySet()) craftableItems.put( e.getKey(), ImmutableList.copyOf( e.getValue() ) ); - sg.postAlterationOfStoredItems( StorageChannel.ITEMS, craftableItems.keySet(), new BaseActionSource() ); + storageGrid.postAlterationOfStoredItems( StorageChannel.ITEMS, craftableItems.keySet(), new BaseActionSource() ); } @Override @@ -395,7 +400,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper @Override public IAEStack injectItems(IAEStack input, Actionable type, BaseActionSource src) { - for (CraftingCPUCluster cpu : cpuClusters) + for (CraftingCPUCluster cpu : craftingCPUClusters) input = cpu.injectItems( input, type, src ); return input; @@ -404,7 +409,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper @Override public boolean canAccept(IAEStack input) { - for (CraftingCPUCluster cpu : cpuClusters) + for (CraftingCPUCluster cpu : craftingCPUClusters) if ( cpu.canAccept( input ) ) return true; @@ -426,7 +431,7 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper if ( target == null ) { List validCpusClusters = new ArrayList(); - for (CraftingCPUCluster cpu : cpuClusters) + for (CraftingCPUCluster cpu : craftingCPUClusters) { if ( cpu.isActive() && !cpu.isBusy() && cpu.getAvailableStorage() >= job.getByteTotal() ) { @@ -545,13 +550,13 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper public boolean hasCpu(ICraftingCPU cpu) { - return cpuClusters.contains( cpu ); + return craftingCPUClusters.contains( cpu ); } @Override public boolean isRequesting(IAEItemStack what) { - for (CraftingCPUCluster c : cpuClusters) + for (CraftingCPUCluster c : craftingCPUClusters) if ( c.isMaking( what ) ) return true; return false; From da148ef787229c21e163fdd6280568e477c55ee4 Mon Sep 17 00:00:00 2001 From: thatsIch Date: Sun, 5 Oct 2014 16:15:36 +0200 Subject: [PATCH 4/4] Renames, visibility, other cleanups --- .../appeng/me/cache/CraftingGridCache.java | 249 +++++++++++------- 1 file changed, 153 insertions(+), 96 deletions(-) diff --git a/src/main/java/appeng/me/cache/CraftingGridCache.java b/src/main/java/appeng/me/cache/CraftingGridCache.java index fbd0c172..3615b696 100644 --- a/src/main/java/appeng/me/cache/CraftingGridCache.java +++ b/src/main/java/appeng/me/cache/CraftingGridCache.java @@ -59,15 +59,15 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper private final Map craftingWatchers = new HashMap(); private final IGrid grid; - IStorageGrid storageGrid; - IEnergyGrid energyGrid; + private IStorageGrid storageGrid; + private IEnergyGrid energyGrid; private final Map> craftingMethods = new HashMap>(); private final Map> craftableItems = new HashMap>(); private final Set emitableItems = new HashSet(); private final Map craftingLinks = new HashMap(); - boolean updateList = false; + private boolean updateList = false; private final Multimap interests = HashMultimap.create(); public final GenericInterestManager interestManager = new GenericInterestManager( this.interests ); @@ -140,42 +140,50 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper this.storageGrid.registerCellProvider( this ); } - public void addLink(CraftingLink l) + public void addLink(CraftingLink link) { - if ( l.isStandalone() ) + if ( link.isStandalone() ) + { return; + } - CraftingLinkNexus n = craftingLinks.get( l.getCraftingID() ); - if ( n == null ) - craftingLinks.put( l.getCraftingID(), n = new CraftingLinkNexus( l.getCraftingID() ) ); + CraftingLinkNexus nexus = this.craftingLinks.get( link.getCraftingID() ); + if ( nexus == null ) + { + this.craftingLinks.put( link.getCraftingID(), nexus = new CraftingLinkNexus( link.getCraftingID() ) ); + } - l.setNexus( n ); + link.setNexus( nexus ); } @Override public void onUpdateTick() { - if ( updateList ) + if ( this.updateList ) { - updateList = false; - updateCPUClusters(); + this.updateList = false; + this.updateCPUClusters(); } - Iterator i = craftingLinks.values().iterator(); - while (i.hasNext()) + Iterator craftingLinkIterator = this.craftingLinks.values().iterator(); + while (craftingLinkIterator.hasNext()) { - if ( i.next().isDead( grid, this ) ) - i.remove(); + if ( craftingLinkIterator.next().isDead( this.grid, this ) ) + { + craftingLinkIterator.remove(); + } } - for (CraftingCPUCluster cpu : craftingCPUClusters) - cpu.updateCraftingLogic( grid, energyGrid, this ); + for (CraftingCPUCluster cpu : this.craftingCPUClusters) + { + cpu.updateCraftingLogic( this.grid, this.energyGrid, this ); + } } @MENetworkEventSubscribe public void updateCPUClusters(MENetworkCraftingCpuChange c) { - updateList = true; + this.updateList = true; } @MENetworkEventSubscribe @@ -189,31 +197,34 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper { if ( machine instanceof ICraftingWatcherHost ) { - ICraftingWatcher myWatcher = craftingWatchers.get( machine ); - if ( myWatcher != null ) + ICraftingWatcher craftingWatcher = this.craftingWatchers.get( machine ); + if ( craftingWatcher != null ) { - myWatcher.clear(); - craftingWatchers.remove( machine ); + craftingWatcher.clear(); + this.craftingWatchers.remove( machine ); } } if ( machine instanceof ICraftingRequester ) { - for (CraftingLinkNexus n : craftingLinks.values()) + for (CraftingLinkNexus link : this.craftingLinks.values()) { - if ( n.isMachine( machine ) ) + if ( link.isMachine( machine ) ) { - n.removeNode(); + link.removeNode(); } } } if ( machine instanceof TileCraftingTile ) - updateList = true; + { + this.updateList = true; + } + if ( machine instanceof ICraftingProvider ) { - craftingProviders.remove( machine ); - updatePatterns(); + this.craftingProviders.remove( machine ); + this.updatePatterns(); } } @@ -222,43 +233,51 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper { if ( machine instanceof ICraftingWatcherHost ) { - ICraftingWatcherHost swh = (ICraftingWatcherHost) machine; - CraftingWatcher iw = new CraftingWatcher( this, swh ); - craftingWatchers.put( gridNode, iw ); - swh.updateWatcher( iw ); + ICraftingWatcherHost watcherHost = (ICraftingWatcherHost) machine; + CraftingWatcher watcher = new CraftingWatcher( this, watcherHost ); + craftingWatchers.put( gridNode, watcher ); + watcherHost.updateWatcher( watcher ); } if ( machine instanceof ICraftingRequester ) { - for (ICraftingLink l : ((ICraftingRequester) machine).getRequestedJobs()) + for (ICraftingLink link : ((ICraftingRequester) machine).getRequestedJobs()) { - if ( l instanceof CraftingLink ) - addLink( (CraftingLink) l ); + if ( link instanceof CraftingLink ) + { + this.addLink( (CraftingLink) link ); + } } } if ( machine instanceof TileCraftingTile ) - updateList = true; + { + this.updateList = true; + } + if ( machine instanceof ICraftingProvider ) { - craftingProviders.add( (ICraftingProvider) machine ); - updatePatterns(); + this.craftingProviders.add( (ICraftingProvider) machine ); + this.updatePatterns(); } } private void updateCPUClusters() { - craftingCPUClusters.clear(); - for (IGridNode cst : grid.getMachines( TileCraftingStorageTile.class )) + this.craftingCPUClusters.clear(); + + for (IGridNode cst : this.grid.getMachines( TileCraftingStorageTile.class )) { TileCraftingStorageTile tile = (TileCraftingStorageTile) cst.getMachine(); CraftingCPUCluster cluster = (CraftingCPUCluster) tile.getCluster(); if ( cluster != null ) { - craftingCPUClusters.add( cluster ); + this.craftingCPUClusters.add( cluster ); if ( cluster.myLastLink != null ) + { addLink( (CraftingLink) cluster.myLastLink ); + } } } } @@ -266,45 +285,50 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper @Override public void addCraftingOption(ICraftingMedium medium, ICraftingPatternDetails api) { - List details = craftingMethods.get( api ); + List details = this.craftingMethods.get( api ); if ( details == null ) { details = new ArrayList(); details.add( medium ); - craftingMethods.put( api, details ); + this.craftingMethods.put( api, details ); } else + { details.add( medium ); + } } - static final Comparator comp = new Comparator(){ + static final Comparator comp = new Comparator() + { @Override - public int compare(ICraftingPatternDetails o1, - ICraftingPatternDetails o2) { - return o2.getPriority() - o1.getPriority(); + public int compare(ICraftingPatternDetails firstDetail, ICraftingPatternDetails nextDetail) + { + return nextDetail.getPriority() - firstDetail.getPriority(); } }; private void updatePatterns() { - Map> oldItems = craftableItems; + Map> oldItems = this.craftableItems; // erase list. - craftingMethods.clear(); - craftableItems.clear(); - emitableItems.clear(); + this.craftingMethods.clear(); + this.craftableItems.clear(); + this.emitableItems.clear(); // update the stuff that was in the list... - storageGrid.postAlterationOfStoredItems( StorageChannel.ITEMS, oldItems.keySet(), new BaseActionSource() ); + this.storageGrid.postAlterationOfStoredItems( StorageChannel.ITEMS, oldItems.keySet(), new BaseActionSource() ); // re-create list.. - for (ICraftingProvider cp : craftingProviders) - cp.provideCrafting( this ); + for (ICraftingProvider provider : this.craftingProviders) + { + provider.provideCrafting( this ); + } - HashMap> tmpCraft = new HashMap>(); + Map> tmpCraft = new HashMap>(); // new craftables! - for (ICraftingPatternDetails details : craftingMethods.keySet()) + for (ICraftingPatternDetails details : this.craftingMethods.keySet()) { for (IAEItemStack out : details.getOutputs()) { @@ -315,7 +339,9 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper Set methods = tmpCraft.get( out ); if ( methods == null ) - tmpCraft.put( out, methods = new TreeSet(comp) ); + { + tmpCraft.put( out, methods = new TreeSet( comp ) ); + } methods.add( details ); } @@ -323,9 +349,11 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper // make them immutable for (Entry> e : tmpCraft.entrySet()) - craftableItems.put( e.getKey(), ImmutableList.copyOf( e.getValue() ) ); + { + this.craftableItems.put( e.getKey(), ImmutableList.copyOf( e.getValue() ) ); + } - storageGrid.postAlterationOfStoredItems( StorageChannel.ITEMS, craftableItems.keySet(), new BaseActionSource() ); + this.storageGrid.postAlterationOfStoredItems( StorageChannel.ITEMS, this.craftableItems.keySet(), new BaseActionSource() ); } @Override @@ -348,9 +376,13 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper @Override public List getCellArray(StorageChannel channel) { - ArrayList list = new ArrayList( 1 ); + List list = new ArrayList( 1 ); + if ( channel == StorageChannel.ITEMS ) + { list.add( this ); + } + return list; } @@ -370,11 +402,15 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper public IItemList getAvailableItems(IItemList out) { // add craftable items! - for (IAEItemStack st : craftableItems.keySet()) - out.addCrafting( st ); + for (IAEItemStack stack : this.craftableItems.keySet()) + { + out.addCrafting( stack ); + } - for (IAEItemStack st : emitableItems) + for (IAEItemStack st : this.emitableItems) + { out.addCrafting( st ); + } return out; } @@ -400,8 +436,10 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper @Override public IAEStack injectItems(IAEStack input, Actionable type, BaseActionSource src) { - for (CraftingCPUCluster cpu : craftingCPUClusters) + for (CraftingCPUCluster cpu : this.craftingCPUClusters) + { input = cpu.injectItems( input, type, src ); + } return input; } @@ -409,29 +447,36 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper @Override public boolean canAccept(IAEStack input) { - for (CraftingCPUCluster cpu : craftingCPUClusters) + for (CraftingCPUCluster cpu : this.craftingCPUClusters) + { if ( cpu.canAccept( input ) ) + { return true; + } + } return false; } @Override - public ICraftingLink submitJob(ICraftingJob job, ICraftingRequester requestingMachine, ICraftingCPU target, final boolean prioritizePower, - BaseActionSource src) + public ICraftingLink submitJob(ICraftingJob job, ICraftingRequester requestingMachine, ICraftingCPU target, final boolean prioritizePower, BaseActionSource src) { if ( job.isSimulation() ) + { return null; + } CraftingCPUCluster cpuCluster = null; if ( target instanceof CraftingCPUCluster ) + { cpuCluster = (CraftingCPUCluster) target; + } if ( target == null ) { List validCpusClusters = new ArrayList(); - for (CraftingCPUCluster cpu : craftingCPUClusters) + for (CraftingCPUCluster cpu : this.craftingCPUClusters) { if ( cpu.isActive() && !cpu.isBusy() && cpu.getAvailableStorage() >= job.getByteTotal() ) { @@ -439,29 +484,31 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper } } - Collections.sort( validCpusClusters, new Comparator() { - + Collections.sort( validCpusClusters, new Comparator() + { @Override - public int compare(CraftingCPUCluster o1, CraftingCPUCluster o2) + public int compare(CraftingCPUCluster firstCluster, CraftingCPUCluster nextCluster) { if ( prioritizePower ) { - int a = ItemSorters.compareLong( o2.getCoProcessors(), o1.getCoProcessors() ); - if ( a != 0 ) - return a; - return ItemSorters.compareLong( o2.getAvailableStorage(), o1.getAvailableStorage() ); + int comparison = ItemSorters.compareLong( nextCluster.getCoProcessors(), firstCluster.getCoProcessors() ); + if ( comparison != 0 ) + return comparison; + return ItemSorters.compareLong( nextCluster.getAvailableStorage(), firstCluster.getAvailableStorage() ); } - int a = ItemSorters.compareLong( o1.getCoProcessors(), o2.getCoProcessors() ); - if ( a != 0 ) - return a; - return ItemSorters.compareLong( o1.getAvailableStorage(), o2.getAvailableStorage() ); + int comparison = ItemSorters.compareLong( firstCluster.getCoProcessors(), nextCluster.getCoProcessors() ); + if ( comparison != 0 ) + return comparison; + return ItemSorters.compareLong( firstCluster.getAvailableStorage(), nextCluster.getAvailableStorage() ); } } ); if ( !validCpusClusters.isEmpty() ) + { cpuCluster = validCpusClusters.get( 0 ); + } } if ( cpuCluster != null ) @@ -481,19 +528,19 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper @Override public ImmutableCollection getCraftingFor(IAEItemStack whatToCraft, ICraftingPatternDetails details, int slotIndex, World world) { - ImmutableList res = craftableItems.get( whatToCraft ); + ImmutableList res = this.craftableItems.get( whatToCraft ); if ( res == null ) { if ( details != null && details.isCraftable() ) { - for (IAEItemStack ais : craftableItems.keySet()) + for (IAEItemStack ais : this.craftableItems.keySet()) { if ( ais.getItem() == whatToCraft.getItem() && (!ais.getItem().getHasSubtypes() || ais.getItemDamage() == whatToCraft.getItemDamage()) ) { if ( details.isValidItemForSlot( slotIndex, ais.getItemStack(), world ) ) { - return craftableItems.get( ais ); + return this.craftableItems.get( ais ); } } } @@ -507,12 +554,14 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper public List getMediums(ICraftingPatternDetails key) { - List o = craftingMethods.get( key ); + List mediums = craftingMethods.get( key ); - if ( o == null ) - o = ImmutableList.of(); + if ( mediums == null ) + { + mediums = ImmutableList.of(); + } - return o; + return mediums; } @Override @@ -542,36 +591,44 @@ public class CraftingGridCache implements ICraftingGrid, ICraftingProviderHelper public Future beginCraftingJob(World world, IGrid grid, BaseActionSource actionSrc, IAEItemStack slotItem, ICraftingCallback cb) { if ( world == null || grid == null || actionSrc == null || slotItem == null ) + { throw new RuntimeException( "Invalid Crafting Job Request" ); + } - CraftingJob cj = new CraftingJob( world, grid, actionSrc, slotItem, cb ); - return craftingPool.submit( cj, (ICraftingJob) cj ); + CraftingJob job = new CraftingJob( world, grid, actionSrc, slotItem, cb ); + + return craftingPool.submit( job, (ICraftingJob) job ); } public boolean hasCpu(ICraftingCPU cpu) { - return craftingCPUClusters.contains( cpu ); + return this.craftingCPUClusters.contains( cpu ); } @Override public boolean isRequesting(IAEItemStack what) { - for (CraftingCPUCluster c : craftingCPUClusters) - if ( c.isMaking( what ) ) + for (CraftingCPUCluster cluster : this.craftingCPUClusters) + { + if ( cluster.isMaking( what ) ) + { return true; + } + } + return false; } @Override - public boolean canEmitFor(IAEItemStack what) + public boolean canEmitFor(IAEItemStack someItem) { - return emitableItems.contains( what ); + return this.emitableItems.contains( someItem ); } @Override - public void setEmitable(IAEItemStack what) + public void setEmitable(IAEItemStack someItem) { - emitableItems.add( what.copy() ); + this.emitableItems.add( someItem.copy() ); } }