Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is original class information preserved

 
Tanya Ruttenberg
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20996
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tanya Ruttenberg
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the patterns reference. I am just starting to learn those. I will go check these out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic