Nagabhushan S N
Nagabhushan S N

Reputation: 7267

How to generate java's hashcode method in intellij with its members sorted?

I want to generate the the java equals() and hashcode() method in Intellij. I want it to generate the way how eclipse generates. I have created a custom template to do this. But, in the hashcode method generated by intellij, the members are in the order they are listed, not alphabetically. But eclipse sorts them alphabetically when generating equals and hashcode method. I need to achieve this in Intellij. I couldn't find any sort function to use. Any help or pointers will be greatly appreciated. Thank you

Upvotes: 1

Views: 516

Answers (2)

Nagabhushan S N
Nagabhushan S N

Reputation: 7267

Based on @CrazyCoder's suggestion, I developed a macro for intellij.

#macro(sort $array)
  #set($size=$array.size())
  #set($index1=0)
  #foreach($element1 in $array)
    #set($index2=0)
    #set($minElement=$element1)
    #set($minElementIndex=$index1)
    #foreach($element2 in $array)
      #if($index2>$index1)
        #if($minElement.name.compareTo($element2.name)>0)
          #set($minElement=$element2)
          #set($minElementIndex=$index2)
        #end
      #end
      #set($index2=$index2+1)
    #end
    #set($temp=$array.get($index1))
    #set($junk=$array.set($index1, $minElement))
    #set($junk=$array.set($minElementIndex, $temp))
    #set($index1=$index1+1)
  #end
#end

Works good.

Upvotes: 2

CrazyCoder
CrazyCoder

Reputation: 402015

This request was already submitted for IntelliJ IDEA and it was declined with the following comment:

We assume that you have ordered your members in a meaningful order within the file, and that preserving this order would be more meaningful than alphabetic sorting.

Upvotes: 3

Related Questions