Possible Duplicate:
How to suppress Java compiler warnings for specific functions
I would like to use a Map to implement the Strategy pattern. However, I cannot find a way of doing this in a way that plays nicely with generics. Is it possible to remove the compile warning from the following code without altering the functionality?
import java.util.HashMap;
import java.util.Map;
interface Event
{
}
interface EventProcessor<T extends Event>
{
public void handleEvent(T event);
}
class EventA implements Event
{
}
class EventB implements Event
{
}
class ProcessorA implements EventProcessor<EventA>
{
@Override
public void handleEvent(final EventA event)
{
System.out.println("Handling event type A");
}
}
class ProcessorB implements EventProcessor<EventB>
{
@Override
public void handleEvent(final EventB event)
{
System.out.println("Handling event type B");
}
}
class GenericEventProcessor
{
Map<Class<? extends Event>, EventProcessor> map = new HashMap<Class<? extends Event>, EventProcessor>();
public GenericEventProcessor()
{
map.put(EventA.class, new ProcessorA());
map.put(EventB.class, new ProcessorB());
}
public void processEvent(Event event)
{
EventProcessor eventProcessor = map.get(event.getClass());
//Java Warning: GenericEventProcessorTest.java uses unchecked or unsafe operations.
eventProcessor.handleEvent(event);
}
}
public class GenericEventProcessorTest
{
public static void main(String[] args)
{
EventA eventA = new EventA();
EventB eventB = new EventB();
GenericEventProcessor eventProcessor = new GenericEventProcessor();
eventProcessor.processEvent(eventA);
eventProcessor.processEvent(eventB);
}
}
Edit: I should have said, without using SuppressWarnings! That normally tells me there is a problem with my design and I'd like to know whether that's the case here.