aspose file tools*
The moose likes Java in General and the fly likes Using a Queue to hold a variety of objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using a Queue to hold a variety of objects" Watch "Using a Queue to hold a variety of objects" New topic
Author

Using a Queue to hold a variety of objects

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I created my own generic Queue to hold a wide range of objects. I use it as both a standard queue and a priority queue. With methods "enter" and "priorityEnter" depending on which one it is. When I use it as a priority queue I need to do ordered inserts of two diffrent kind of objects.

Data (abstract superclass)
-Rooms extends Data
-People extends Data
-Events extends Data (abstract superclass)
-Event1 extends Events
-Event2 extends Events
-Event3 extends Events

*People contains method getPriority()
*Events contains method getTime()

People and Events need to be stored in the priority queue sorting Poeple by a variable called priority, and Events by a variable called time. I can easily use instance of to determine if i'm working with a People object and perform the desired cast and perform my ordered insert. But I can only go as far as using instanceof to determine if it's an Events object, than it can't find the method getTime() in Events to do the ordered insert. Since there are diffrent kinds of events I don't want to re-write the code 3 times using the appropriate cast for each Event object. What can I do?



So I know that the parameter i'm working with is an instanceof Events ( could be any one of them ). The getTime() method is inside the Events class, and should be inherited by all of the classes that extend it. How do I access it? Or do I have to re-write the code for every class that extends is and go instanceof Event1, instanceof Event2 etc...?

So since Events is abstract, and the method getTime cannot be instantiated except only from one of Events subclasses. How can I write the code once, but cast it to the specific event and perform the ordered insert? If this is possible I can eliminate the duplicate code I wrote for "People", because I perform a "People" cast, but other than that the code is identical.
[ January 25, 2008: Message edited by: John Lockheart ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using a Queue to hold a variety of objects