java question,

P

__PPS__

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!!
 
O

Oliver Wong

__PPS__ said:
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.

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

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
 
V

Vova Reznik

__PPS__ said:
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.

You cannot add EventHandler if you defined set as <EventHandlerWrapper>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top