• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?


 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks very similar to TimeUnit, only in TimeUnit the numbers include 1000 1000000 1000000000 etc.
 
Jon Swanson
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all, I could tell I was going about it wrong. The example was really helpful.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
Create symphonies in seed and soil. For this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic