1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.commonclipse;
18
19 import java.util.Random;
20
21 import org.eclipse.jdt.core.IMethod;
22 import org.eclipse.jdt.core.IType;
23 import org.eclipse.jdt.core.JavaModelException;
24
25
26 /***
27 * Generator for hashcode() methods.
28 * @author fgiust
29 * @version $Revision: 1.5 $ ($Author: fgiust $)
30 */
31 public final class HashcodeGenerator extends Generator
32 {
33
34 /***
35 * class name for the HashCode builder.
36 */
37 private static final String BUILDER_CLASS = "org.apache.commons.lang.builder.HashCodeBuilder";
38
39 /***
40 * singleton for HashcodeGenerator.
41 */
42 private static Generator instance = new HashcodeGenerator();
43
44 /***
45 * Random used for hashCode.
46 */
47 private Random random = new Random();
48
49 /***
50 * use getInstance() to obtain an instance of HashcodeGenerator.
51 */
52 private HashcodeGenerator()
53 {
54 }
55
56 /***
57 * returns the HashcodeGenerator instance.
58 * @return instance of HashcodeGenerator
59 */
60 public static Generator getInstance()
61 {
62 return instance;
63 }
64
65 /***
66 * @see net.sf.commonclipse.Generator#getMethodName()
67 */
68 protected String getMethodName()
69 {
70 return "hashCode";
71 }
72
73 /***
74 * @see net.sf.commonclipse.Generator#createMethod(org.eclipse.jdt.core.IType)
75 */
76 protected String createMethod(IType type) throws JavaModelException
77 {
78 int initial = this.random.nextInt();
79 int multiplier = this.random.nextInt();
80
81
82 if (initial % 2 == 0)
83 {
84 initial++;
85 }
86 if (multiplier % 2 == 0)
87 {
88 multiplier++;
89 }
90
91 StringBuffer buffer = new StringBuffer();
92
93 buffer.append(getJavadoc());
94
95 buffer.append("public int hashCode()\n{\nreturn new HashCodeBuilder(");
96 buffer.append(initial);
97 buffer.append(", ");
98 buffer.append(multiplier);
99 buffer.append(")\n");
100
101 if (CCPluginPreferences.getPreferences().appendSuperToHashcode())
102 {
103 buffer.append(".appendSuper(super.hashCode())\n");
104 }
105
106 buffer.append(buildAppenderList(type));
107
108 buffer.append(".toHashCode();\n}\n");
109 return buffer.toString();
110 }
111
112 /***
113 * @see net.sf.commonclipse.Generator#getFieldAppender(java.lang.String, java.lang.String)
114 */
115 protected String getFieldAppender(String fieldName, String accessor)
116 {
117 return ".append(this." + fieldName + ")\n";
118 }
119
120 /***
121 * @see net.sf.commonclipse.Generator#getExistingMethod(org.eclipse.jdt.core.IType)
122 */
123 protected IMethod getExistingMethod(IType type)
124 {
125 return type.getMethod(getMethodName(), new String[0]);
126 }
127
128 /***
129 * @see net.sf.commonclipse.Generator#addImports(org.eclipse.jdt.core.IType)
130 */
131 protected void addImports(IType type) throws JavaModelException
132 {
133 type.getCompilationUnit().createImport(BUILDER_CLASS, null, null);
134 }
135
136 /***
137 * Generates the method javadoc.
138 * @return String javadoc
139 */
140 private String getJavadoc()
141 {
142 return "/**\n * @see java.lang.Object#hashCode()\n */\n";
143 }
144
145 }