|
|
@ -0,0 +1,52 @@ |
|
|
|
package com.iflytop.uf; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
public class UfEventBus { |
|
|
|
// 事件回调 |
|
|
|
public interface EventCallback { |
|
|
|
void callback( Object ... args ); |
|
|
|
} |
|
|
|
|
|
|
|
// singleton instance |
|
|
|
private static final UfEventBus instance = new UfEventBus(); |
|
|
|
// 事件处理回调 |
|
|
|
private final Map<String, List<EventCallback>> callbacks; |
|
|
|
|
|
|
|
// get instance |
|
|
|
public static UfEventBus getInstance() { |
|
|
|
return UfEventBus.instance; |
|
|
|
} |
|
|
|
|
|
|
|
// con |
|
|
|
private UfEventBus() { |
|
|
|
this.callbacks = new HashMap<String, List<EventCallback>>(); |
|
|
|
} |
|
|
|
|
|
|
|
// on |
|
|
|
public void on( String name, EventCallback callback ) { |
|
|
|
if ( !this.callbacks.containsKey(name) ) { |
|
|
|
this.callbacks.put(name, new ArrayList<EventCallback>()); |
|
|
|
} |
|
|
|
this.callbacks.get(name).add(callback); |
|
|
|
} |
|
|
|
|
|
|
|
// off |
|
|
|
public void off( String name, EventCallback callback ) { |
|
|
|
if ( !this.callbacks.containsKey(name) ) { |
|
|
|
return ; |
|
|
|
} |
|
|
|
this.callbacks.get(name).remove(callback); |
|
|
|
} |
|
|
|
|
|
|
|
// emit |
|
|
|
public void emit( String name, Object ... args ) { |
|
|
|
if ( !this.callbacks.containsKey(name) ) { |
|
|
|
return ; |
|
|
|
} |
|
|
|
for ( EventCallback callback : this.callbacks.get(name) ) { |
|
|
|
callback.callback( args ); |
|
|
|
} |
|
|
|
} |
|
|
|
} |