Getting a set of subscribers from a subscriberMap In my Java event bus project, I have a private generic method that retrieves a Set&lt;Subscriber&lt;E&gt;&gt; from a private Map&lt;Class&lt;? extends Event&gt;, Set&lt;Subscriber&lt;? extends Event&gt;&gt;&gt; (the subscriberMap). 

/**
 * A map of Event types and Subscriber Sets.
 * 
 * That is, the Set of Subscribers set to an Event type
 * is assumed to be used for that Event type.
 */
private final Map&lt;Class&lt;? extends Event&gt;, Set&lt;Subscriber&lt;? extends Event&gt;&gt;&gt; subscriberMap = new ConcurrentHashMap&lt;&gt;();

private &lt;E extends Event&gt; void checkSubscriberMap(Class&lt;E&gt; type)
{
    if (!subscriberMap.containsKey(type))
    {
        subscriberMap.put(type, new HashSet&lt;&gt;());
    }
}

/**
 * Gets a Set of Subscribers by an Event.
 *
 * @param event the event
 * @param &lt;E&gt; the type of event
 * @return a Set of Subscribers
 */
@SuppressWarnings("unchecked")
private &lt;E extends Event&gt; Set&lt;Subscriber&lt;E&gt;&gt; getSubscribers(E event)
{
    return (Set&lt;Subscriber&lt;E&gt;&gt;) (Object) getSubscribers(event.getClass());
}

/**
 * Gets a Set of Subscribers by an Event type.
 *
 * @param type the event class
 * @param &lt;E&gt; the type of event
 * @return a Set of Subscribers
 */
@SuppressWarnings("unchecked")
private &lt;E extends Event&gt; Set&lt;Subscriber&lt;E&gt;&gt; getSubscribers(Class&lt;E&gt; type)
{
    checkSubscriberMap(type); // checks if subscriberMap.get(type) is null

    return (Set&lt;Subscriber&lt;E&gt;&gt;) (Object) subscriberMap.get(type);
}


Assuming nobody uses reflection to modify subscriberMap, the public methods that modify subscriberMap only work if the Subscriber&lt;? extends Event&gt; is the same type as the Class&lt;? extends Event&gt;. Therefore, I can be sure that getSubscribers() will always return a Set of the same type that you give it.

/**
 * Registers a single event handler under an explicit Event type.
 * Returns true if the handler was added.
 *
 * @param subscriber the Subscriber
 * @param type the type of Event
 */
public &lt;E extends Event&gt; boolean register(Subscriber&lt;E&gt; subscriber, Class&lt;E&gt; type)
{
    if(type == Event.class)
    {
        throw new IllegalArgumentException("The provided type is not a subclass of Event.");
    }

    return getSubscribers(type).add(subscriber);
}


However, it's bothering me how getSubscribers() looks. Why does it look I'm doing a hacky workaround? Is it necessary that I have to cast subscriberMap.get(type) twice? Is this the right way to go about what I'm trying to do?
private &lt;E extends Event&gt; void checkSubscriberMap(Class&lt;E&gt; type)


This method checks nothing. It ensures that there's a non-null value. Or it fixes or prepares something.

Note that you're using both contains and get every time. I guess, a method like

private &lt;E extends Event&gt; Set&lt;E&gt; safeGet(Class&lt;E&gt; type) {
   Set&lt;E&gt; result =
           (Set&lt;Subscriber&lt;E&gt;&gt;) (Object) subscriberMap.get(type);
   if (result == null) {
        result = new HashSet&lt;&gt;();
        subscriberMap.put(type, result);
   }
   return result;
}


would be better.

Consider using Guava's Multimap as it's been solved there.



@SuppressWarnings("unchecked")
private &lt;E extends Event&gt; Set&lt;Subscriber&lt;E&gt;&gt; getSubscribers(E event)
{
    return (Set&lt;Subscriber&lt;E&gt;&gt;) (Object) getSubscribers(event.getClass());
}


I'm afraid, that's right. The ugly double cast is needed as there's no way to tell the compiler that it works. Maybe the intermediate cast could be (non-generic) (Set).




  Assuming nobody uses reflection


If somebody does, that's not your fault.

What you did is similar in idea to the MutableClassToInstanceMap, except that you need Set&lt;Subscriber&lt;E&gt;&gt; instead of E itself and therefore you can't use type.cast(object) and have to resolve to an unchecked cast.