This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 96449 - Generate toString(), hashCode(), compareTo(), equals() methods using Apache commons framework calls
Summary: Generate toString(), hashCode(), compareTo(), equals() methods using Apache c...
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Editor (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker with 3 votes (vote)
Assignee: Dusan Balek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-02-23 15:06 UTC by tugando
Modified: 2015-07-26 12:03 UTC (History)
2 users (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description tugando 2007-02-23 15:06:36 UTC
Add functionality that generates toString(), hashCode(), compareTo(), and
equals() method using Jakarta commons lang library.  Similar to what is provided
by the Commonclipe plugin for Eclipse.
Comment 1 Jiri Prox 2007-02-23 15:11:39 UTC
Generate code menu (alt-insert or in editor context menu) is not enough?
Comment 2 tugando 2007-02-23 15:35:31 UTC
Having the IDE generate the toString() method, for a JavaBean object, and not
have to write the output code for each property yourself, is one of the
enhancements that I mean.
Comment 3 calvin 2009-04-28 11:46:43 UTC
A proposed format for a generated toString() method can be:

Original class:

public class Bean {

   private String field1;
   private String field2;

}

Tostring method:
(Its a good practice to keep the output on one line. And use nested toString() methods on sub objects.

public String toString(){
   return "Bean[" + 
      "field1=" + field1 +
      ",field2=" + field2 +
      "]";
}



Comment 4 calvin 2009-04-28 11:54:21 UTC
A proposed generation for compareters can be:

public class Bean {

   private String field1;
   private String field2;

}

Add a compareter for field1 causes this public innner class for others to use on a list of Beans.
Use nested compareters on sub-objects or the compareto (then that object chooses a default comparetor)
Use normal datatype comparison on strings, int's etc.

public static final Comparator FIELD1_ORDER = new Comparator(){
   public int compare(Object o1, Object o2) {
      Bean compare1 = (Bean) o1;
      Bean compare2 = (Bean) o2;
      return compare1.field1.compareTo(compare2.field1);
   }
};

Adding a compareTo method and compariable interface is a matter of choise of the default comparetor to use in the bean.

public int compareTo(Object o){
   return FIELD1_ORDER.compare(this,o);
}
Comment 5 calvin 2009-05-04 11:22:02 UTC
If we use generics in comparison genration the result can be:

public class Bean implements Comparable<Bean>{
...

public static final Comparator<Bean> FIELD1_ORDER = new Comparator<Bean>(){
   public int compare(Bean compare1, Bean compare2) {
      return compare1.field1.compareTo(compare2.field1);
   }
};

public int compareTo(Bean o){
   return FIELD1_ORDER.compare(this,o);
}
}