Avoiding an unchecked cast for cast to a collection of a generic interface in Java for an event publisher
generics, java
Solution
Is the unchecked cast actually safe here?
Quite. Your code will not cause heap pollution because the signature of subcribe ensures that you only put IEventSubscribers of the proper compile time type into the map. It might propagate heap pollution caused by an unsafe unchecked cast elsewhere, but there is little you can do about that.
How can I actually check to see if the items in the subscriber list implement IEventSubscriber?
By casting each item to `IEventSubscriber`. Your code already does this in the following line:
for (IEventSubscriber<T> subscriber: existingSubscribers) {
If `existingSubscribers` contained an object not assignable to `IEventSubscriber`, this line would throw a ClassCastException. Standard practice to avoid a warning when iterating over a list of unknown type parameter is to explicitly cast each item:
List<?> list = ...
for (Object item : list) {
IEventSubscriber<T> subscriber = (IEventSubscriber<T>) item;
}
That code explicitly checks that each item is an `IEventSubscriber`, but can not check that it is an `IEventSubscriber<T>`.
To actually check the type parameter of `IEventSubscriber`, the `IEventSubscriber` needs to help you out. That is due to erasure, specifically, given the declaration
class MyEventSubscriber<T> implements IEventSubscriber<T> { ... }
the following expression will always be true:
new MyEventSubscriber<String>.getClass() == new MyEventSubscriber<Integer>.getClass()
Presuming that (2) involves some nasty reflection, what would you do here?
I'd leave the code as it is. It is quite easy to reason that the cast is correct, and I would not find it worth my time to rewrite it to compile without warnings. If you do wish to rewrite it, the following idea may be of use:
class SubscriberList<E> extends CopyOnWriteArrayList<E> {
final Class<E> eventClass;
public void trigger(Object event) {
E event = eventClass.cast(event);
for (IEventSubscriber<E> subscriber : this) {
subscriber.trigger(event);
}
}
}
and
SubscriberList<?> subscribers = (SubscriberList<?>) subscriptions.get(eventClass);
subscribers.trigger(message);
Problem
I'm trying to create a lightweight, thread-safe in-app publish/subscribe mechanism for an Android app that I'm building. My basic approach is to keep track of a list of `IEventSubscriber<T>` for each event type T and then be able to publish events to subscribing objects by passing along a payload of type T. I use generic method parameters to (I think) ensure that subscriptions are created in a type safe way. Thus, I'm pretty sure that when I obtain the list of subscribers from my subscription map when it comes time to publish an event that I'm OK casting it to a list of `IEventSubscriber<T>`, however, this generates the unchecked cast warning. My questions: - Is the unchecked cast actually safe here? - How can I actually check to see if the items in the subscriber list implement `IEventSubscriber<T>`? - Presuming that (2) involves some nasty reflection, what would you do here? Code (Java 1.6): ``` import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.CopyOnWriteArraySet; public class EventManager { private ConcurrentMap<Class, CopyOnWriteArraySet<IEventSubscriber>> subscriptions = new ConcurrentHashMap<Class, CopyOnWriteArraySet<IEventSubscriber>>(); public <T> boolean subscribe(IEventSubscriber<T> subscriber, Class<T> eventClass) { CopyOnWriteArraySet<IEventSubscriber> existingSubscribers = subscriptions. putIfAbsent(eventClass, new CopyOnWriteArraySet<IEventSubscriber>()); return existingSubscribers.add(subscriber); } public <T> boolean removeSubscription(IEventSubscriber<T> subscriber, Class<T> eventClass) { CopyOnWriteArraySet<IEventSubscriber> existingSubscribers = subscriptions.get(eventClass); return existingSubscribers == null || !existingSubscribers.remove(subscriber); } public <T> void publish(T message, Class<T> eventClass) { @SuppressWarnings("unchecked") CopyOnWriteArraySet<IEventSubscriber<T>> existingSubscribers = (CopyOnWriteArraySet<IEventSubscriber<T>>) subscriptions.get(eventClass); if (existingSubscribers != null) { for (IEventSubscriber<T> subscriber: existingSubscribers) { subscriber.trigger(message); } } } } ```