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.
Summary: | Generate toString(), hashCode(), compareTo(), equals() methods using Apache commons framework calls | ||
---|---|---|---|
Product: | java | Reporter: | tugando <tugando> |
Component: | Editor | Assignee: | Dusan Balek <dbalek> |
Status: | NEW --- | ||
Severity: | blocker | CC: | calvin, markiewb |
Priority: | P3 | ||
Version: | 6.x | ||
Hardware: | All | ||
OS: | All | ||
Issue Type: | ENHANCEMENT | Exception Reporter: |
Description
tugando
2007-02-23 15:06:36 UTC
Generate code menu (alt-insert or in editor context menu) is not enough? 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. 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 + "]"; } 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); } 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); } } |