This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Mapping a method data of a custom class to an Enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Mapping a method data of a custom class to an Enum" Watch "Mapping a method data of a custom class to an Enum" New topic
Author

Mapping a method data of a custom class to an Enum

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi,

I am trying to write an Enum that will hold the column names of an Excel sheet. I am able to associate the column number and column name with the Enum values.

The data for the columns is take from a custom class object. The class has getter and setter method to access its data holding fields.

So if I like to print a column data, I check the column number from the Enum and then call the corresponding setter method in my custom class.
Say if I am printing column 0, then I call a setter method named getProduct() and print it's data.

I wish to make this check done by the Enum itself. i.e. If I call a generalized method named getData() on a particular Enum (say PRODUCT) then it should in turn call the getProduct() method and return its value.

Is there a way I can achieve it.

I am pasting a similar code I wrote to visualize my need.




See commented lines in the below Tester class


Thanks much!
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

I don't know whether it is a good idea, but you could code your enum similar to this:
Then use it in the following way: colEnum.printField(student));.

You can declare an abstract method to do whatever you need and implement it differently for individual enums. But so tight coupling between the enum and the functionality does not look right to me. Though there are many ways to twist this basic scheme around, I still usually prefer the good old switch statement in cases similar to this. Good thing about the scheme I've shown is that the compiler forces you to redefine the abstract method for any of your enums. In a switch statement, that is generally detected only at runtime.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Using an abstract method that you override in each of the constants, like Martin's code shows, is the usual way to do this. Instead of a method that prints data you can of course also use a method that returns a property of the object. The return type will have to be a common super type of all property types though. The marks would need to be returned as a String, or the return type would need to be Object to allow both Strings and Integers to be returned.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Martin - That's a great idea... I was exactly looking for that..
Rob - Thank you... luckily I had all but one returning String... and one was returning Set<String>. I parsed that in the Enum itself.

The resulting code that prints the Excel became very little and cute Thanks again...

The code that prints Excel -



This is the resultant Enum -

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

The only thing I would change in that code is the getTopic method:
Both your implementation and mine use an Iterator over topicHierarchySet (yours when creating the ArrayList), but my implementation a) doesn't iterate over the entire set but only the necessary part, and b) doesn't create the List object. Both implementations work just fine, mine is slightly better in both memory and time.

And I would probably store the return value of ResultColumn.values() in a ResultColumn[] instead of calling the method twice, but that too is only to improve performance slightly.


Oh, and a for not using the return value of order() as the column number. I don't know if you did this on purpose, but order() should only be used in data structures like EnumSet and EnumMap, definitely not for custom code. The reason is simple - if you add an enum constant in the middle, the return value of order() changes, and that can lead to unexpected results.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Thanks Rob... I changed them...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mapping a method data of a custom class to an Enum
 
Similar Threads
Need help on saving child object
add property of type enum to class
Arrays in Hashtable
Why my table's columName invisiable?
struts + hibernate confused on what type of mapping to use