Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

java question,

Thread view: 
__PPS__ - 12 Apr 2006 21:45 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...
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!!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.