/** * Looks for an events pool for the specified event type. * * @param eventClass * @return */ @SuppressWarnings({"rawtypes", "unchecked"}) private <T extends EntityEvent> EventsPool<T> getEventsPool(final Class<T> eventClass) { int count = m_eventsPool.size(); for (int i = 0; i < count; ++i) { EventsPool pool = m_eventsPool.get(i); if (pool.equals(eventClass)) { // pool found return (EventsPool<T>) pool; } } // no pool found return null; }
/** * Transmits an entity event to the specific listeners. * * <p>CAUTION: single entity can only receive up to MAX_EVENTS_COUNT per frame. Once that number * is reached, no new events will be transmitted to the entity and this method will return null * pointers. * * @param event * @throws EntityEventException when the allowed number of sent events was exceeded */ public final <T extends EntityEvent> T sendEvent(Class<T> eventClass) throws EntityEventException { EventsPool<T> pool = getEventsPool(eventClass); if (pool == null) { // Unregistered event type - this is acceptable return null; } T event = pool.newEvent(); if (event == null) { throw new EntityEventException("Amount of allowed sent events exceeded"); } return event; }
/** * Registers an event type. * * @param eventClass * @param factory factory that will create the events */ @SuppressWarnings({"rawtypes", "unchecked"}) public final <T extends EntityEvent> void registerEvent( Class<T> eventClass, EventFactory<T> factory) { // check if the event isn't already registered. // If it is - replace the old definition int count = m_eventsPool.size(); for (int i = 0; i < count; ++i) { EventsPool pool = m_eventsPool.get(i); if (pool.equals(eventClass)) { pool.setFactory(factory); return; } } // this is a new event typ EventsPool<T> newPool = new EventsPool<T>(eventClass, factory); m_eventsPool.add(newPool); }