Hello, I'm a c++ programmer and I have a qestion about a simple code
implementation in Java.
Basicly, I have a set of EventHandlers (just some class). Handlers
could be added/removed/suspended/active...
EventHandler is a class written by someone else and it must not be
modified; this class knows nothing about suspended/active state.
The way I'd do this in c++: make a set of std::pair<EventHandler,
State> or something like this.
here's how I did this in Java and it doesn't work.
class EventHandlerWrapper extends EventHandler {
public enum State {
SUSPENDED,
ACTIVE
}
protected State _state = State.ACTIVE;
public void Suspend(){
_state = State.SUSPENDED;
}
public void Resume(){
_state = State.ACTIVE;
}
}
Now I want to be able to add EventHandler's into the
Set<EventHandlerWrapper>, but it doesn't work. I' messed up with
casting etc.
what's the right way to do this kind of stuff in Java
thanks!!
Oliver Wong - 12 Apr 2006 21:50 GMT
> Hello, I'm a c++ programmer and I have a qestion about a simple code
> implementation in Java.
>
> Basicly, I have a set of EventHandlers (just some class). Handlers
> could be added/removed/suspended/active...
Here, did you really mean "handlers could be
added/removed/suspended/active", or did you mean "events could be
added/removed/suspended/active"?
> EventHandler is a class written by someone else and it must not be
> modified; this class knows nothing about suspended/active state.
[quoted text clipped - 22 lines]
>
> what's the right way to do this kind of stuff in Java
When you say "this kind of stuff", it's not clear what "this" refers to.
Are you asking how to do event handling in Java? If so, usually via the
"observer" or "listener" design pattern. Are you asking how to do something
like std::pair<EventHandler,State>? You should probably look into Java
generics, though they aren't exactly the same thing as C++ Templates. Are
you asking how to add EventHandlers in Set<EventHandlerWrapper>? You can
only do this is EventHandler extends EventHandlerWrapper (or if you can cast
it into something which does extend EventHandlerWrapper).
- Oliver
Vova Reznik - 12 Apr 2006 22:17 GMT
> Hello, I'm a c++ programmer and I have a qestion about a simple code
> implementation in Java.
[quoted text clipped - 25 lines]
> Set<EventHandlerWrapper>, but it doesn't work. I' messed up with
> casting etc.
You cannot add EventHandler if you defined set as <EventHandlerWrapper>.
backwards: add EventHandlerWrapper to the set defined as <EventHandler>
> what's the right way to do this kind of stuff in Java
>
> thanks!!