View Javadoc

1   /* ====================================================================
2    *   Copyright 2003-2005 Fabrizio Giustina.
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
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"; //$NON-NLS-1$
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"; //$NON-NLS-1$
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          // be shure they are odd numbers
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("); //$NON-NLS-1$
96          buffer.append(initial);
97          buffer.append(", "); //$NON-NLS-1$
98          buffer.append(multiplier);
99          buffer.append(")\n"); //$NON-NLS-1$
100 
101         if (CCPluginPreferences.getPreferences().appendSuperToHashcode())
102         {
103             buffer.append(".appendSuper(super.hashCode())\n"); //$NON-NLS-1$
104         }
105 
106         buffer.append(buildAppenderList(type));
107 
108         buffer.append(".toHashCode();\n}\n"); //$NON-NLS-1$
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"; //$NON-NLS-1$ //$NON-NLS-2$
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"; //$NON-NLS-1$
143     }
144 
145 }