Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  compareTo is final in Enum...  (Read 378 times)
0 Members and 1 Guest are viewing this topic.
Offline 2NickPick

JGO n00b
*

Posts: 9



« on: 2011-02-25 10:10:26 »

OK, so I have an enum with about 15 types in it, representing elements such as water, grass, fire and such. I made the enum implement Comparable<E> and rewrote the compareTo method. BUT apparently compareTo is final and I am not allowed to override it. The final version of compareTo compares them based on their ordering, but there is no logical way to order the types so that the relationships are modeled correctly. For example, the ordering

WATER FIRE GRASS GROUND...

water is strong to fire, fire is strong to grass and grass is strong to ground. BUT water is also strong to ground, and grass is strong to water.....

the obvious option is to write a different method called compareTypes( Type ) or something to that effect but it feels wrong. It feels broken... I really wanted a compareTo method...
Offline pjt33

JGO Strike Force
***

Posts: 913
Medals: 17



« Reply #1 on: 2011-02-25 11:02:43 »

It sounds like you were planning on breaking the contract of Comparable anyway. It's intended for total orderings.
Offline 2NickPick

JGO n00b
*

Posts: 9



« Reply #2 on: 2011-02-25 11:23:23 »

Ah, I've just read through the Comparable interface's API and it somewhat makes sense now. So what I really want is my own original method compareTypes because compareTo is designed so that it sets the elements in a specific order for sorting maps and the like. I get it. I feel better about writing my own method now. Thanks.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.087 seconds with 20 queries.