aspose file tools*
The moose likes Beginning Java and the fly likes A more effcient way to work with overloaded functions with enums as arguments than an interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A more effcient way to work with overloaded functions with enums as arguments than an interface?" Watch "A more effcient way to work with overloaded functions with enums as arguments than an interface?" New topic
Author

A more effcient way to work with overloaded functions with enums as arguments than an interface?

Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 206
At one point, I needed to convert between different units. These might be units of weight, volume, length, etc. I created a bunch of enums, i.e.,



I then created a Convert class and overloaded the methods based on the enum, i.e.,



I then called convert( type, value ) wherever I needed to do the conversion.

I am now finding I am writing a lot of custom text fields, one for each class of enum. I was hoping that since all the functions except convert are the same and convert is overloaded, I might be able to extend a generic class. I haven't been able to get that to work.

If I try something like:


I get



I can create an interface (see below), but most of the code in the custom text fields is identical (everything but the calls to Convert). Is there a way to extend a class when I have a set of overloaded functions like above? Or a better implementation than just an interface, so I don't duplicate so much code?


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
In your Java installation folder, there is a file called src.zip. Unzip it, and explore it till you find the TimeUnit enum. Inspect its convert method. If you click on the word TimeUnit, you can work out from the API documentation it is in the concurrent folder in the util folder in the java folder. that has methods allowing you to convert times from milliseconds to hours, etc. Is that the sort of thing you are looking for here, converting feet to metres or pounds to kilograms? Can you use similar techniques?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8419
    
  23

Jon Swanson wrote:I can create an interface (see below), but most of the code in the custom text fields is identical (everything but the calls to Convert). Is there a way to extend a class when I have a set of overloaded functions like above? Or a better implementation than just an interface, so I don't duplicate so much code?

Yes.

I hate to say, but you were on the right lines at the start with your enums; you just didn't take it a stage further and link it properly with your other good idea: the interface. And now you've started overthinking...

Go back to your Weight enum (BTW, don't use all-caps for class names). Why not just add a convertTo() method and a little "knowledge" to it?
Then it might look something like:and now
Weight.KILOGRAMS.convertTo(Weight.POUNDS, 3.0)
will return
6.6138670242179628

And now you can take it a stage further. Enums can implement interfaces, so what about:and then, your Weight definition becomes:HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
That looks very similar to TimeUnit, only in TimeUnit the numbers include 1000 1000000 1000000000 etc.
Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 206
Thanks all, I could tell I was going about it wrong. The example was really helpful.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A more effcient way to work with overloaded functions with enums as arguments than an interface?