Replaced Watcher using Collection with a more fitting interface (#2693)
Replaced the watchers for energy, storage and crafting with a more fitting interface compared to a common collection. Fixes #229
This commit is contained in:
@@ -20,50 +20,98 @@ package appeng.me.energy;
|
||||
|
||||
|
||||
import appeng.api.networking.energy.IEnergyWatcher;
|
||||
import appeng.util.ItemSorters;
|
||||
|
||||
|
||||
public class EnergyThreshold implements Comparable<EnergyThreshold>
|
||||
{
|
||||
|
||||
private final double Limit;
|
||||
private final double threshold;
|
||||
private final IEnergyWatcher watcher;
|
||||
private final int hash;
|
||||
private final int watcherHash;
|
||||
|
||||
public EnergyThreshold( final double lim, final IEnergyWatcher wat )
|
||||
public EnergyThreshold( final double lim, final IEnergyWatcher watcher )
|
||||
{
|
||||
this.Limit = lim;
|
||||
this.watcher = wat;
|
||||
this.threshold = lim;
|
||||
this.watcher = watcher;
|
||||
this.watcherHash = watcher.hashCode();
|
||||
}
|
||||
|
||||
if( this.getWatcher() != null )
|
||||
/**
|
||||
* Special constructor to allow querying a for a subset of thresholds.
|
||||
*
|
||||
* @param lim
|
||||
* @param bound
|
||||
*/
|
||||
public EnergyThreshold( final double lim, final int bound )
|
||||
{
|
||||
this.threshold = lim;
|
||||
this.watcher = null;
|
||||
this.watcherHash = bound;
|
||||
}
|
||||
|
||||
public IEnergyWatcher getEnergyWatcher()
|
||||
{
|
||||
return watcher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo( EnergyThreshold o )
|
||||
{
|
||||
int a = Double.compare( this.threshold, o.threshold );
|
||||
|
||||
if( a == 0 )
|
||||
{
|
||||
this.hash = this.getWatcher().hashCode() ^ ( (Double) lim ).hashCode();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.hash = ( (Double) lim ).hashCode();
|
||||
return Integer.compare( this.watcherHash, o.watcherHash );
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return this.hash;
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits( threshold );
|
||||
result = prime * result + (int) ( temp ^ ( temp >>> 32 ) );
|
||||
result = prime * result + ( ( watcher == null ) ? 0 : watcher.hashCode() );
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo( final EnergyThreshold o )
|
||||
public boolean equals( Object obj )
|
||||
{
|
||||
return ItemSorters.compareDouble( this.getLimit(), o.getLimit() );
|
||||
}
|
||||
if( this == obj )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if( obj == null )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if( getClass() != obj.getClass() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
double getLimit()
|
||||
{
|
||||
return this.Limit;
|
||||
}
|
||||
EnergyThreshold other = (EnergyThreshold) obj;
|
||||
if( Double.doubleToLongBits( threshold ) != Double.doubleToLongBits( other.threshold ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public IEnergyWatcher getWatcher()
|
||||
{
|
||||
return this.watcher;
|
||||
if( watcher == null )
|
||||
{
|
||||
if( other.watcher != null )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if( !watcher.equals( other.watcher ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,9 +19,9 @@
|
||||
package appeng.me.energy;
|
||||
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import appeng.api.networking.energy.IEnergyWatcher;
|
||||
import appeng.api.networking.energy.IEnergyWatcherHost;
|
||||
@@ -35,170 +35,57 @@ public class EnergyWatcher implements IEnergyWatcher
|
||||
{
|
||||
|
||||
private final EnergyGridCache gsc;
|
||||
private final IEnergyWatcherHost myObject;
|
||||
private final HashSet<EnergyThreshold> myInterests = new HashSet<EnergyThreshold>();
|
||||
private final IEnergyWatcherHost watcherHost;
|
||||
private final Set<EnergyThreshold> myInterests = new HashSet<EnergyThreshold>();
|
||||
|
||||
public EnergyWatcher( final EnergyGridCache cache, final IEnergyWatcherHost host )
|
||||
{
|
||||
this.gsc = cache;
|
||||
this.myObject = host;
|
||||
this.watcherHost = host;
|
||||
}
|
||||
|
||||
public void post( final EnergyGridCache energyGridCache )
|
||||
{
|
||||
this.myObject.onThresholdPass( energyGridCache );
|
||||
this.watcherHost.onThresholdPass( energyGridCache );
|
||||
}
|
||||
|
||||
public IEnergyWatcherHost getHost()
|
||||
{
|
||||
return this.myObject;
|
||||
return this.watcherHost;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size()
|
||||
public boolean add( final double amount )
|
||||
{
|
||||
return this.myInterests.size();
|
||||
}
|
||||
final EnergyThreshold eh = new EnergyThreshold( amount, this );
|
||||
|
||||
@Override
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return this.myInterests.isEmpty();
|
||||
}
|
||||
if( this.myInterests.contains( eh ) )
|
||||
|
||||
@Override
|
||||
public boolean contains( final Object o )
|
||||
{
|
||||
return this.myInterests.contains( o );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Double> iterator()
|
||||
{
|
||||
return new EnergyWatcherIterator( this, this.myInterests.iterator() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray()
|
||||
{
|
||||
return this.myInterests.toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray( final T[] a )
|
||||
{
|
||||
return this.myInterests.toArray( a );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add( final Double e )
|
||||
{
|
||||
if( this.myInterests.contains( e ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
final EnergyThreshold eh = new EnergyThreshold( e, this );
|
||||
return this.gsc.getInterests().add( eh ) && this.myInterests.add( eh );
|
||||
return this.gsc.registerEnergyInterest( eh ) && this.myInterests.add( eh );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove( final Object o )
|
||||
public boolean remove( final double amount )
|
||||
{
|
||||
final EnergyThreshold eh = new EnergyThreshold( (Double) o, this );
|
||||
return this.myInterests.remove( eh ) && this.gsc.getInterests().remove( eh );
|
||||
final EnergyThreshold eh = new EnergyThreshold( amount, this );
|
||||
|
||||
return this.myInterests.remove( eh ) && this.gsc.unregisterEnergyInterest( eh );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll( final Collection<?> c )
|
||||
public void reset()
|
||||
{
|
||||
return this.myInterests.containsAll( c );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll( final Collection<? extends Double> c )
|
||||
{
|
||||
boolean didChange = false;
|
||||
|
||||
for( final Double o : c )
|
||||
for( Iterator<EnergyThreshold> iterator = myInterests.iterator(); iterator.hasNext(); )
|
||||
{
|
||||
didChange = this.add( o ) || didChange;
|
||||
}
|
||||
final EnergyThreshold threshold = iterator.next();
|
||||
|
||||
return didChange;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll( final Collection<?> c )
|
||||
{
|
||||
boolean didSomething = false;
|
||||
for( final Object o : c )
|
||||
{
|
||||
didSomething = this.remove( o ) || didSomething;
|
||||
}
|
||||
return didSomething;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll( final Collection<?> c )
|
||||
{
|
||||
boolean changed = false;
|
||||
final Iterator<Double> i = this.iterator();
|
||||
|
||||
while( i.hasNext() )
|
||||
{
|
||||
if( !c.contains( i.next() ) )
|
||||
{
|
||||
i.remove();
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear()
|
||||
{
|
||||
final Iterator<EnergyThreshold> i = this.myInterests.iterator();
|
||||
while( i.hasNext() )
|
||||
{
|
||||
this.gsc.getInterests().remove( i.next() );
|
||||
i.remove();
|
||||
this.gsc.unregisterEnergyInterest( threshold );
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
|
||||
private class EnergyWatcherIterator implements Iterator<Double>
|
||||
{
|
||||
|
||||
private final EnergyWatcher watcher;
|
||||
private final Iterator<EnergyThreshold> interestIterator;
|
||||
private EnergyThreshold myLast;
|
||||
|
||||
public EnergyWatcherIterator( final EnergyWatcher parent, final Iterator<EnergyThreshold> i )
|
||||
{
|
||||
this.watcher = parent;
|
||||
this.interestIterator = i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
{
|
||||
return this.interestIterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double next()
|
||||
{
|
||||
this.myLast = this.interestIterator.next();
|
||||
return this.myLast.getLimit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove()
|
||||
{
|
||||
EnergyWatcher.this.gsc.getInterests().remove( this.myLast );
|
||||
this.interestIterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user