public final class EventUtils
extends java.lang.Object
Event message tool class. Used for getting the delegate object witch send messages, or registering message listener.
public class EventUtilsTest { public static interface TestListener { void method(); } public static class TestListenerImplOne implements TestListener { public boolean invoked = false; public void method() { invoked = true; System.out.println("TestListenerImplOne.method() invoked"); } } public static class TestListenerImplTwo implements TestListener { public boolean invoked = false; public void method() { invoked = true; System.out.println("TestListenerImplTwo.method() invoked"); } } public static void main(String[] args) { TestListenerImplOne listener1 = new TestListenerImplOne(); EventUtils.registerEventListener(TestListener.class, listener1); TestListenerImplTwo listener2 = new TestListenerImplTwo(); EventUtils.registerEventListener(TestListener.class, listener2); TestListener sender = EventUtils.getDelegate(TestListener.class); sender.method(); while(!(listener1.invoked && listener2.invoked)) { Thread.yield(); } //The application will output: //TestListenerImplTwo.method() invoked //TestListenerImplOne.method() invoked //Or //TestListenerImplOne.method() invoked //TestListenerImplTwo.method() invoked /// //The order and time of calling listener1 and listener2 are indeterminated. EventUtils.dipose(); } }
Constructor and Description |
---|
EventUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
dipose()
Destroy the thread pool which sends messages.
|
static <T> T |
getDelegate(java.lang.Class<T> eventType)
Gets a delegate object used for sending messages.
|
static void |
init()
Initialize the thread pool which sends messages.
|
static <T> void |
registerEventListener(java.lang.Class<T> eventType, T listener)
Register a message listener.
|
static <T> void |
removeListener(java.lang.Class<T> eventType, T listener) |
public static <T> void registerEventListener(java.lang.Class<T> eventType, T listener)
Register a message listener.
eventType
- Register event listener interface types. This parameter must be a interface or else no operation will be performed.listener
- The receive message instance.public static <T> void removeListener(java.lang.Class<T> eventType, T listener)
public static <T> T getDelegate(java.lang.Class<T> eventType)
Gets a delegate object used for sending messages. This function requires a instance which has a parameter interface and returns a instance of the same type. This function can send messages by directly call the corresponding method. Only call the methods with its return value void, or the action will be indeterminated.
eventType
- The message interface type to get, this parameter must be a interface.public static void dipose()
Destroy the thread pool which sends messages.
public static void init()
Initialize the thread pool which sends messages. This method don't need to be called by default. Call this method is needed only when you want to use it after dipose()
has been called.