title: Java generics event system unchecked call
body: <p>I'm writing an event system for a game. Here's an example of the API for subscribing to a channel.</p>
  
  <pre><code>Events.subscribe(Channel.COLLISION, new EventHandler&lt;CollisionEvent&gt;() {
      @Override
      public void handle(CollisionEvent event) {
      }
  });
  </code></pre>
  
  <p>and publishing</p>
  
  <pre><code>Events.publish(Channel.COLLISION, new CollisionEvent(data));
  </code></pre>
  
  <p>My problem arises when passing objects to the publish method. I'd been getting a warning from my IDE about an unchecked call in the publish method. I'm trying to find a way to check to make sure the type of the object is compatible with the <code>EventHandler</code>'s generic type. How can I ensure that <code>event</code> is of the right type for the <code>handler</code> (in the excerpt below)?</p>
  
  <h3> Events.java </h3>
  
  <pre><code>private static Map&lt;Channel, ArrayList&lt;EventHandler&gt;&gt; eventHandlers
          = new HashMap&lt;Channel, ArrayList&lt;EventHandler&gt;&gt;();
  
  public static void publish(EventType eventType, GameEvent event) {
      ArrayList&lt;EventHandler&gt; handlers = eventHandlers.get(eventType);
      if(handlers != null) {
          for (EventHandler handler : handlers) {
              handler.handle(event); // unchecked call warning occurs here
          }
      }
  }
  </code></pre>
  
  <h3> EventHandler.java</h3>
  
  <pre><code>public abstract class EventHandler&lt;T extends GameEvent&gt; {
      public abstract void handle(T event);
  }
  </code></pre>
  
   <hr>