0

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.

Community
  • 1
  • 1
Alex Spurling
  • 54,094
  • 23
  • 70
  • 76

2 Answers2

3

Currently it looks to me like the map is substituting for polymorphism and in fact corresponds to a type check thus:

if (event instanceof EventA) {
}
else if (event instanceof EventB) {
}
// etc...

Since you're using the type of the event to determine the corresponding event processor, and then call that with the event, why not use the visitor or double-dispatch pattern thus:

event.getEventProcessor().processEvent(event);

such that each event understand what event processor corresponds to it via polymorphism. The above can obviously be collapsed to simply:

event.process();

So it's much less complex and you don't have to populate your map for each new event type. I note that you're implementing the strategy pattern, but this currently looks largely static and tied per type. I would expect the strategy pattern to substitute different implementations on something other than a type (e.g. some config)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

Yes, add

@SuppressWarnings("unchecked")

to the method.

The problem is that the compiler cannot determine the operation is safe.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Sorry 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. – Alex Spurling Dec 14 '12 at 14:01
  • It doesn't in this case because you are trying to do something which the compiler cannot check for. I don't believe there is a better, more scalable way to design what you are doing. Note: even the collections libraries do not compile without these warnings. – Peter Lawrey Dec 14 '12 at 14:46