monniasza.collects.alloc.Allocator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of multimachinebuilder Show documentation
Show all versions of multimachinebuilder Show documentation
Dependency for the MultiMachineBuilder, a voxel game about building an industrial empire in a finite world.
THIS RELEASE IS NOT PLAYABLE. To play the game, donwload from >ITCH.IO LINK HERE< or >GH releases link here<
/**
*
*/
package monniasza.collects.alloc;
import java.util.Iterator;
import java.util.NoSuchElementException;
import com.google.common.collect.Iterators;
import mmb.NN;
import mmb.Nil;
/**
* @author oskar
* @param type of allocated objects
* An allocator offers fast, reliable and durable allocation of object IDs. The allocations may not be changed except for removing and replacing them.
*/
public interface Allocator extends Iterable{
/**
* Allocates a slot in the allocator
* @param data object to be allocated
* @return allocated index
*/
public int allocate(T data);
/**
* Destroys the allocation at given index and calls the listeners
* @param id index of allocation to be destroyed
*/
public void destroy(int id);
/**
* Gets the object at given index
* @param id index to get from
* @return object at given index, or null if absent
*/
@Nil public T get(int id);
/**
* @param id index to check
* @return is given index currently allocateed?
*/
public boolean isAllocated(int id);
/**
* @return number of allocation slots (not allocated values)
*/
public int size();
/**
* The iterator's remove() method can be used to destroy allocation slots
* @return an iterator over valid objects
*/
@Override
public Iterator iterator();
//Global listeners
/**
* Adds an allocation listener
* @param listener listener to be added
*/
public void addAllocationListener(AllocationListener listener);
/**
* Removes an allocation listener
* @param listener listener to be removed
*/
public void removeAllocationListener(AllocationListener listener);
/**
* Add an allocation listener to a specific position
* @param listener
* @param index index to listen to
* @throws NoSuchElementException when given index is absent
*/
public void addSpecificAllocationListener(AllocationListener listener, int index);
/**
* Add an allocation listener to a specific position
* @param listener
* @param index index to listen to
* @throws NoSuchElementException when given index is absent
*/
public void removeSpecificAllocationListener(AllocationListener listener, int index);
/**
* @return read-only allocator which mirrors this allocator.
* The returned allocator does not allow new allocations and changes to the exception handler
*/
@NN public default Allocator readonly(){
final Allocator alloc = this;
return new Allocator() {
@Override
public int allocate(T data) {
throw new UnsupportedOperationException();
}
@Override
public void destroy(int id) {
throw new UnsupportedOperationException();
}
@Override
public T get(int id) {
return alloc.get(id);
}
@Override
public boolean isAllocated(int id) {
return alloc.isAllocated(id);
}
@Override
public void addAllocationListener(AllocationListener listener) {
alloc.addAllocationListener(listener);
}
@Override
public void removeAllocationListener(AllocationListener listener) {
alloc.removeAllocationListener(listener);
}
@Override
public void addSpecificAllocationListener(AllocationListener listener, int index) {
alloc.addSpecificAllocationListener(listener, index);
}
@Override
public void removeSpecificAllocationListener(AllocationListener listener, int index) {
alloc.removeSpecificAllocationListener(listener, index);
}
@Override
public Allocator readonly(){return this;}
@Override
public int size() {
return alloc.size();
}
@SuppressWarnings("null")
@Override
public @NN Iterator iterator() {
return Iterators.unmodifiableIterator(alloc.iterator());
}
};
}
}