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 org.eclipse.jdt.core.IMethod;
20  import org.eclipse.jdt.core.IType;
21  import org.eclipse.jdt.core.JavaModelException;
22  
23  
24  /***
25   * Generator for equals(Object) methods.
26   * @author fgiust
27   * @version $Revision: 1.6 $ ($Author: fgiust $)
28   */
29  public final class EqualsGenerator extends Generator
30  {
31  
32      /***
33       * class name for the Equals builder.
34       */
35      private static final String BUILDER_CLASS = "org.apache.commons.lang.builder.EqualsBuilder"; //$NON-NLS-1$
36  
37      /***
38       * singleton for EqualsGenerator.
39       */
40      private static Generator instance = new EqualsGenerator();
41  
42      /***
43       * use getInstance() to obtain an instance of EqualsGenerator.
44       */
45      private EqualsGenerator()
46      {
47      }
48  
49      /***
50       * returns the EqualsGenerator instance.
51       * @return instance of EqualsGenerator
52       */
53      public static Generator getInstance()
54      {
55          return instance;
56      }
57  
58      /***
59       * @see net.sf.commonclipse.Generator#getMethodName()
60       */
61      protected String getMethodName()
62      {
63          return "equals"; //$NON-NLS-1$
64      }
65  
66      /***
67       * @see net.sf.commonclipse.Generator#createMethod(org.eclipse.jdt.core.IType)
68       */
69      protected String createMethod(IType type) throws JavaModelException
70      {
71  
72          StringBuffer buffer = new StringBuffer();
73  
74          buffer.append(getJavadoc());
75  
76          String className = type.getElementName();
77  
78          buffer.append("public boolean equals("); //$NON-NLS-1$
79  
80          if (CCPluginPreferences.getPreferences().useFinalParameters())
81          {
82              buffer.append("final "); //$NON-NLS-1$
83          }
84  
85          buffer.append("Object object) {\n"); //$NON-NLS-1$
86  
87          if (CCPluginPreferences.getPreferences().addInstanceCheckToEquals())
88          {
89              buffer.append("if (object == this) {\nreturn true;\n}\n"); //$NON-NLS-1$
90          }
91  
92          buffer.append("if ( !(object instanceof "); //$NON-NLS-1$
93          buffer.append(className);
94          buffer.append(") ) {\nreturn false;\n}\n"); //$NON-NLS-1$
95          buffer.append(className);
96          buffer.append(" rhs = ("); //$NON-NLS-1$
97          buffer.append(className);
98          buffer.append(") object;\nreturn new EqualsBuilder()\n"); //$NON-NLS-1$
99  
100         if (CCPluginPreferences.getPreferences().appendSuperToEquals())
101         {
102             buffer.append(".appendSuper(super.equals(object))\n"); //$NON-NLS-1$
103         }
104 
105         buffer.append(buildAppenderList(type));
106 
107         buffer.append(".isEquals();\n}\n"); //$NON-NLS-1$
108         return buffer.toString();
109     }
110 
111     /***
112      * @see net.sf.commonclipse.Generator#getFieldAppender(java.lang.String, java.lang.String)
113      */
114     protected String getFieldAppender(String fieldName, String accessor)
115     {
116         return ".append(this." + fieldName + ", rhs." + fieldName + ")\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
117     }
118 
119     /***
120      * @see net.sf.commonclipse.Generator#getExistingMethod(org.eclipse.jdt.core.IType)
121      */
122     protected IMethod getExistingMethod(IType type)
123     {
124         return type.getMethod(getMethodName(), new String[]{"QObject;"}); //$NON-NLS-1$
125     }
126 
127     /***
128      * @see net.sf.commonclipse.Generator#addImports(org.eclipse.jdt.core.IType)
129      */
130     protected void addImports(IType type) throws JavaModelException
131     {
132         type.getCompilationUnit().createImport(BUILDER_CLASS, null, null);
133     }
134 
135     /***
136      * Generates the method javadoc.
137      * @return String javadoc
138      */
139     private String getJavadoc()
140     {
141         return "/**\n * @see java.lang.Object#equals(Object)\n */\n"; //$NON-NLS-1$
142     }
143 
144 }