-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2be8041
commit 73aeb67
Showing
4 changed files
with
80 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters