All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.mostlyoriginal.api.event.dispatcher.PollingPooledEventDispatcher Maven / Gradle / Ivy

package net.mostlyoriginal.api.event.dispatcher;

import com.artemis.utils.Bag;
import net.mostlyoriginal.api.event.common.Event;
import net.mostlyoriginal.api.utils.pooling.PoolsCollection;

/**
 * 

Polling event dispatcher that cares about garbage collection by using Object Pool pattern.

*

Note: remember to manually reset event objects OR use Poolable interface.

* * @author Namek */ public class PollingPooledEventDispatcher extends FastEventDispatcher { private final Bag eventQueue = new Bag(); private final PoolsCollection pools = new PoolsCollection(); @Override public void process() { Object[] eventsToDispatch = eventQueue.getData(); for (int i = 0, s = eventQueue.size(); i < s; i++) { Event event = (Event) eventsToDispatch[i]; super.dispatch(event); pools.free(event); } eventQueue.clear(); } public void dispatch(Event event) { throw new UnsupportedOperationException("This dispatcher manages its own events. Use dispatch(Class type) instead!"); } @Override public T dispatch(Class type) { T event = pools.obtain(type); eventQueue.add(event); return event; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy