2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
|
|
|
* Copyright (c) 2013 - 2014, 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>.
|
|
|
|
*/
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
package appeng.me.storage;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
import appeng.api.networking.storage.IStackWatcher;
|
|
|
|
import appeng.api.networking.storage.IStackWatcherHost;
|
|
|
|
import appeng.api.storage.data.IAEStack;
|
|
|
|
import appeng.me.cache.GridStorageCache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maintain my interests, and a global watch list, they should always be fully synchronized.
|
|
|
|
*/
|
|
|
|
public class ItemWatcher implements IStackWatcher
|
|
|
|
{
|
|
|
|
|
|
|
|
class ItemWatcherIterator implements Iterator<IAEStack>
|
|
|
|
{
|
|
|
|
|
|
|
|
final ItemWatcher watcher;
|
|
|
|
final Iterator<IAEStack> interestIterator;
|
|
|
|
IAEStack myLast;
|
|
|
|
|
|
|
|
public ItemWatcherIterator(ItemWatcher parent, Iterator<IAEStack> i) {
|
2014-12-29 15:13:47 +01:00
|
|
|
this.watcher = parent;
|
|
|
|
this.interestIterator = i;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNext()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.interestIterator.hasNext();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IAEStack next()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.myLast = this.interestIterator.next();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
ItemWatcher.this.gsc.interestManager.remove( this.myLast, this.watcher );
|
|
|
|
this.interestIterator.remove();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 00:50:06 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-09-29 09:54:34 +02:00
|
|
|
final GridStorageCache gsc;
|
|
|
|
final IStackWatcherHost myObject;
|
|
|
|
final HashSet<IAEStack> myInterests = new HashSet<IAEStack>();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
public ItemWatcher(GridStorageCache cache, IStackWatcherHost host) {
|
2014-12-29 15:13:47 +01:00
|
|
|
this.gsc = cache;
|
|
|
|
this.myObject = host;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public IStackWatcherHost getHost()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.myObject;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean add(IAEStack e)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
if ( this.myInterests.contains( e ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
return false;
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.myInterests.add( e.copy() ) && this.gsc.interestManager.put( e, this );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean addAll(Collection<? extends IAEStack> c)
|
|
|
|
{
|
|
|
|
boolean didChange = false;
|
|
|
|
|
|
|
|
for (IAEStack o : c)
|
2014-12-29 15:13:47 +01:00
|
|
|
didChange = this.add( o ) || didChange;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
return didChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clear()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
Iterator<IAEStack> i = this.myInterests.iterator();
|
2014-09-24 02:26:27 +02:00
|
|
|
while (i.hasNext())
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.gsc.interestManager.remove( i.next(), this );
|
2014-09-24 02:26:27 +02:00
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean contains(Object o)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.myInterests.contains( o );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean containsAll(Collection<?> c)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.myInterests.containsAll( c );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEmpty()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.myInterests.isEmpty();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<IAEStack> iterator()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return new ItemWatcherIterator( this, this.myInterests.iterator() );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean remove(Object o)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.myInterests.remove( o ) && this.gsc.interestManager.remove( (IAEStack)o, this );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean removeAll(Collection<?> c)
|
|
|
|
{
|
|
|
|
boolean didSomething = false;
|
|
|
|
for (Object o : c)
|
2014-12-29 15:13:47 +01:00
|
|
|
didSomething = this.remove( o ) || didSomething;
|
2014-09-24 02:26:27 +02:00
|
|
|
return didSomething;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean retainAll(Collection<?> c)
|
|
|
|
{
|
|
|
|
boolean changed = false;
|
2014-12-29 15:13:47 +01:00
|
|
|
Iterator<IAEStack> i = this.iterator();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
|
|
|
if ( !c.contains( i.next() ) )
|
|
|
|
{
|
|
|
|
i.remove();
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int size()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.myInterests.size();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object[] toArray()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.myInterests.toArray();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T> T[] toArray(T[] a)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.myInterests.toArray( a );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|