Skip to content

Commit

Permalink
refactoring of EventPool
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasHefti committed Nov 7, 2016
1 parent 2be8041 commit 73aeb67
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 49 deletions.
40 changes: 22 additions & 18 deletions src/main/java/com/inari/commons/event/EventDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,29 @@
*/
public final class EventDispatcher implements IEventDispatcher {

private final EventPool eventPool;
private final IEventLog eventLog;
private final DynArray<EventPool<?>> eventPools = new DynArray<EventPool<?>>();
private final DynArray<List<?>> listeners = new DynArray<List<?>>();

public EventDispatcher() {
eventLog = null;
eventPool = null;
}

public EventDispatcher( IEventLog eventLog ) {
this.eventLog = eventLog;
eventPool = null;
}

public EventDispatcher( IEventLog eventLog, EventPool eventPool ) {
this.eventLog = eventLog;
this.eventPool = eventPool;
public final void registerEventPool( final EventTypeKey eventType, final EventPool<?> pool ) {
eventPools.set( eventType.index(), pool );
}

public final void unregisterEventPool( final EventTypeKey eventType ) {
eventPools.remove( eventType.index() );
}

public EventDispatcher( EventPool eventPool ) {
this.eventLog = null;
this.eventPool = eventPool;
@SuppressWarnings( "unchecked" )
public final <E extends Event<?>> EventPool<E> getEventPool( final EventTypeKey eventType ) {
return (EventPool<E>) eventPools.get( eventType.index() );
}

/* (non-Javadoc)
Expand Down Expand Up @@ -89,9 +90,7 @@ public final <L> void notify( final Event<L> event ) {
event.notify( listener );
}

if ( eventPool != null ) {
eventPool.restoreEvent( event );
}
restoreEvent( event );
}

/* (non-Javadoc)
Expand All @@ -108,10 +107,10 @@ public final <L extends AspectedEventListener> void notify( final AspectedEvent<
}
}

if ( eventPool != null ) {
eventPool.restoreEvent( event );
}
restoreEvent( event );
}



/* (non-Javadoc)
* @see com.inari.commons.event.IEventDispatcher#notify(com.inari.commons.event.PredicatedEvent<L>)
Expand All @@ -135,9 +134,7 @@ public final <L extends PredicatedEventListener> void notify( final PredicatedEv
}
}

if ( eventPool != null ) {
eventPool.restoreEvent( event );
}
restoreEvent( event );
}

@Override
Expand All @@ -162,5 +159,12 @@ private final <L> List<L> getListenersOfType( final Indexed indexed, final boole

return listenersOfType;
}

@SuppressWarnings( "unchecked" )
private final void restoreEvent( final Event<?> event ) {
if ( eventPools.contains( event.index() ) ) {
( (EventPool<Event<?>>) eventPools.get( event.index() ) ).restore( event );
}
}

}
50 changes: 19 additions & 31 deletions src/main/java/com/inari/commons/event/EventPool.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,42 @@
package com.inari.commons.event;

import java.util.Stack;
import java.util.ArrayDeque;

import com.inari.commons.event.Event.EventTypeKey;
import com.inari.commons.lang.list.DynArray;

public final class EventPool {
public abstract class EventPool<E extends Event<?>> {

private final DynArray<Stack<Event<?>>> eventPooling = new DynArray<Stack<Event<?>>>();
private final ArrayDeque<E> eventPooling = new ArrayDeque<E>();

public final void populate( EventBuilder eventBuilder, int number ) {
public final void populate( int number ) {
if ( number <= 0 ) {
return;
}

for ( int i = 0; i < number; i++ ) {
restoreEvent( eventBuilder.createEvent() );
eventPooling.add( create() );
}
}

final void restoreEvent( Event<?> event ) {
Stack<Event<?>> eventStack = eventPooling.get( event.index() );
if ( eventStack == null ) {
eventStack = new Stack<Event<?>>();
eventPooling.set( event.index(), eventStack );
public final E get() {
if ( eventPooling.isEmpty() ) {
populate( 10 );
}

eventStack.push( event );
return eventPooling.pop();
}

public final Event<?> getEvent( EventTypeKey type ) {
Stack<Event<?>> eventStack = eventPooling.get( type.index() );
if ( eventStack.isEmpty() ) {
throw new IllegalStateException( "No Event of type: " + type + " available" );
}

return eventStack.pop();
public final void restore( E event ) {
clearEvent( event );
eventPooling.push( event );
}

public final Event<?> getEvent( EventTypeKey type, EventBuilder eventBuilder ) {
Stack<Event<?>> eventStack = eventPooling.get( type.index() );
if ( eventStack.isEmpty() ) {
populate( eventBuilder, 10 );
}

return eventStack.pop();
public final void clear() {
eventPooling.clear();
}

protected abstract E create();

protected abstract void clearEvent( E event );

public static interface EventBuilder {
Event<?> createEvent();
}


}
21 changes: 21 additions & 0 deletions src/main/java/com/inari/commons/event/IEventDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@
*
*/
public interface IEventDispatcher {

/** Use this to register a EventPool for specified type of Event(s).
* Once a EventPool is registered for a type of Event, the events of that types gets pooled.
*
* @param eventType the type of the Event
* @param pool the EventPool instance that handles the pooling
*/
void registerEventPool( final EventTypeKey eventType, final EventPool<?> pool );

/** use this to stop pooling of Event of a specified type
*
* @param eventType the type of the Event
*/
void unregisterEventPool( final EventTypeKey eventType );

/** Use this to get a registered EventPool for a specified type of Event.
*
* @param eventType the type of the Event
* @return The registered instance of EventPool for specified type of Event. Or null of there is none registered.
*/
<E extends Event<?>> EventPool<E> getEventPool( final EventTypeKey eventType );

/** Register a Listener L to listen to specified type of {@link Event}.
*
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/inari/commons/GeomUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,23 @@ public void testIntersectionAA() {
assertEquals( "10", String.valueOf( GeomUtils.intersection( 0, 10, -10, 30 ) ) );
assertEquals( "5", String.valueOf( GeomUtils.intersection( 0, 10, -10, 15 ) ) );
}

@Test
public void testIntersectionWithResult() {
Rectangle r1 = new Rectangle( 0,0,10,10 );
Rectangle r2 = new Rectangle( 10,0,10,10 );
Rectangle r3 = new Rectangle( 20,0,10,10 );

Rectangle intersection = new Rectangle();

GeomUtils.intersection( r1, r2, intersection );
assertEquals( "[x=10,y=0,width=0,height=10]", intersection.toString() );
GeomUtils.intersection( r2, r1, intersection );
assertEquals( "[x=10,y=0,width=0,height=10]", intersection.toString() );
GeomUtils.intersection( r2, r3, intersection );
assertEquals( "[x=20,y=0,width=0,height=10]", intersection.toString() );


}

}

0 comments on commit 73aeb67

Please sign in to comment.