wood burning stoves 2.0*
The moose likes Java in General and the fly likes Is original class information preserved Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is original class information preserved" Watch "Is original class information preserved" New topic
Author

Is original class information preserved

Tanya Ruttenberg
Ranch Hand

Joined: Jun 22, 2009
Posts: 40
I have a Table class and several sub-classes, OfficeTable, DeviceTable, SiteCodeTable.

I have an array of these Tables:


Is there any way to know the type of each item in the table?

When I step through the eclipse debugger and inspect tblList, the field containing the data type lists OfficeTable, DeviceTable, etc. Is that just a contrivance of the eclipse debugger, or does this information exist somewhere?

I want to be able to call joinTable(DeviceTable, OfficeTable) with tlbList.get(1) and tblList.get(0), but I get an error stating joinTable(Table, Table) is not defined. I understand why. What I want to know is if there is some way to know that tblList.get(1) is a DeviceTable and get(0) is an OfficeTable.

TDR
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

Tanya Ruttenberg wrote:I have a Table class and several sub-classes, OfficeTable, DeviceTable, SiteCodeTable.

I have an array of these Tables:


Is there any way to know the type of each item in the table?

When I step through the eclipse debugger and inspect tblList, the field containing the data type lists OfficeTable, DeviceTable, etc. Is that just a contrivance of the eclipse debugger, or does this information exist somewhere?

I want to be able to call joinTable(DeviceTable, OfficeTable) with tlbList.get(1) and tblList.get(0), but I get an error stating joinTable(Table, Table) is not defined. I understand why. What I want to know is if there is some way to know that tblList.get(1) is a DeviceTable and get(0) is an OfficeTable.

TDR



You can check the Table object to see if it is a particular type of table using the instanceof operator. And then, if it is, you can cast it to that table type.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8250
    
  23

Tanya Ruttenberg wrote:I want to be able to call joinTable(DeviceTable, OfficeTable) with tlbList.get(1) and tblList.get(0), but I get an error stating joinTable(Table, Table) is not defined. I understand why. What I want to know is if there is some way to know that tblList.get(1) is a DeviceTable and get(0) is an OfficeTable.

As Henry said, instanceof is your man; however, I wonder why you think you need to know this information?

join() is a standard function for a dataset (at least if it does a normal database 'join') and can be emulated with Set.retainAll(Set), so it would seem that your type check is superfluous; but maybe you could explain why you think you need it.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Tanya Ruttenberg
Ranch Hand

Joined: Jun 22, 2009
Posts: 40
I found "instanceof" right after I posted this. Thanks. I would be interested in finding the class name of the object rather than having to test the object for instanceof against every possible data type. I'm working on tbl.getClass() now to see if I can get the result I want.

Here's why I am doing this:

I need to generate a sql statement based on some criteria. If the tables to be joined are DeviceTable and and OfficeTable, the sql syntax for the join is "d.office_id=o.office_id". If the tables to be joined are DeviceTable and SiteCodeTable, the syntax is "d.office_id=scm.office_id and scm.site_code_id = sc.site_code_id". Etc.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8250
    
  23

Tanya Ruttenberg wrote:I need to generate a sql statement based on some criteria. If the tables to be joined are DeviceTable and and OfficeTable, the sql syntax for the join is "d.office_id=o.office_id". If the tables to be joined are DeviceTable and SiteCodeTable, the syntax is "d.office_id=scm.office_id and scm.site_code_id = sc.site_code_id". Etc.

Ah. Then what you probably want is an instanceof on the dataset, not on an element.
However, I also suspect that, rather than dispatch code, you could do the translation polymorphically; maybe with something like a Strategy pattern (or Bridge; I forget).

Whatever, you aren't going to get around the fact that (I suspect) you need something that covers all possible combinations, and throws an error for invalid ones.

Winston
Tanya Ruttenberg
Ranch Hand

Joined: Jun 22, 2009
Posts: 40
Thank you for the patterns reference. I am just starting to learn those. I will go check these out.
 
wood burning stoves
 
subject: Is original class information preserved