Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.70 KB | None | 0 0
  1. /*
  2.  * Created on Oct 24, 2010
  3.  */
  4.  
  5. package engine.rts;
  6.  
  7. import java.lang.reflect.Method;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.NoSuchElementException;
  13.  
  14. import craterstudio.data.tuples.Pair;
  15.  
  16. public class Binding
  17. {
  18.    final Object                              source;
  19.    private final Map<String, List<Callback>> eventToCallbacks;
  20.  
  21.    public Binding(Object source)
  22.    {
  23.       this.source = source;
  24.       this.eventToCallbacks = new HashMap<String, List<Callback>>();
  25.    }
  26.  
  27.    public void listen(String event, Object target)
  28.    {
  29.       Method method = findMethodForEvent(this.source, target, event);
  30.  
  31.       List<Callback> callbacks = this.eventToCallbacks.get(event);
  32.       if (callbacks == null)
  33.          this.eventToCallbacks.put(event, callbacks = new ArrayList<Callback>());
  34.       callbacks.add(new Callback(target, method));
  35.    }
  36.  
  37.    public void discard(String event, Object target)
  38.    {
  39.       List<Callback> callbacks = this.eventToCallbacks.get(event);
  40.       if (callbacks == null)
  41.          return;
  42.       Callback match = null;
  43.       for (Callback callback : callbacks)
  44.          if (callback.target == target)
  45.             match = callback;
  46.       callbacks.remove(match);
  47.  
  48.       if (callbacks.isEmpty())
  49.          this.eventToCallbacks.remove(event);
  50.    }
  51.  
  52.    public void fire(String event)
  53.    {
  54.       List<Callback> callbacks = this.eventToCallbacks.get(event);
  55.       if (callbacks != null)
  56.          for (Callback callback : callbacks)
  57.             callback.call();
  58.    }
  59.  
  60.    // callback
  61.  
  62.    private class Callback
  63.    {
  64.       final Object target;
  65.       final Method method;
  66.  
  67.       public Callback(Object target, Method method)
  68.       {
  69.          this.target = target;
  70.          this.method = method;
  71.       }
  72.  
  73.       public void call()
  74.       {
  75.          try
  76.          {
  77.             this.method.invoke(this.target, new Object[] { Binding.this.source });
  78.          }
  79.          catch (Exception exc)
  80.          {
  81.             throw new RuntimeException(exc);
  82.          }
  83.       }
  84.    }
  85.  
  86.    // utility methods
  87.  
  88.    private static Method findMethodForEvent(Object source, Object target, String event)
  89.    {
  90.       Pair<String, Class< ? >> key = new Pair<String, Class< ? >>(event, target.getClass());
  91.       Method found = method_cache.get(key);
  92.       if (found != null)
  93.          return found;
  94.  
  95.       found = findMethodByName(source, target, fromEventToMethodname(source, event));
  96.       method_cache.put(key, found);
  97.       return found;
  98.    }
  99.  
  100.    private static String fromEventToMethodname(Object source, String event)
  101.    {
  102.       String typeName = source.getClass().getSimpleName();
  103.       String capitalizedEvent = event.substring(0, 1).toUpperCase() + event.substring(1);
  104.       return "on" + typeName + capitalizedEvent;
  105.    }
  106.  
  107.    private static Method findMethodByName(Object source, Object target, String methodName)
  108.    {
  109.       for (Method method : target.getClass().getDeclaredMethods())
  110.       {
  111.          if (!method.getName().equals(methodName))
  112.             continue;
  113.  
  114.          Class< ? >[] types = method.getParameterTypes();
  115.          if (types.length != 1)
  116.             throw new IllegalStateException("event handler must have 1 parameter");
  117.          if (types[0] != source.getClass())
  118.             throw new IllegalStateException("event handler must have 1 parameter type equal to the source type");
  119.  
  120.          method.setAccessible(true);
  121.          return method;
  122.       }
  123.       throw new NoSuchElementException(target.getClass().getSimpleName() + "." + methodName + "()");
  124.    }
  125.  
  126.    private static Map<Pair<String, Class< ? >>, Method> method_cache = new HashMap<Pair<String, Class< ? >>, Method>();
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement