org.apache.ibatis.ibator.plugins
Class EqualsHashCodePlugin

java.lang.Object
  extended by org.apache.ibatis.ibator.api.IbatorPluginAdapter
      extended by org.apache.ibatis.ibator.plugins.EqualsHashCodePlugin
All Implemented Interfaces:
IbatorPlugin

public class EqualsHashCodePlugin
extends IbatorPluginAdapter

This plugin adds equals() and hashCode() methods to the generated model classes. It demonstrates the process of adding methods to generated classes

The equals method generated by this class is correct in most cases, but will probably NOT be correct if you have specified a rootClass - because our equals method only checks the fields it knows about.

The hashCode method generated by this class is a very simplistic implementation. A better implementation would rely on the HashCodeUtil from www.javapractices.com - but we do not want to introduce another dependency in this simple plugin.

Author:
Jeff Butler

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.ibatis.ibator.api.IbatorPlugin
IbatorPlugin.ModelClassType
 
Field Summary
 
Fields inherited from class org.apache.ibatis.ibator.api.IbatorPluginAdapter
ibatorContext, properties
 
Constructor Summary
EqualsHashCodePlugin()
           
 
Method Summary
protected  void generateEquals(TopLevelClass topLevelClass, java.util.List<IntrospectedColumn> introspectedColumns, IntrospectedTable introspectedTable)
          Generates an equals method that does a comparison of all fields.
protected  void generateHashCode(TopLevelClass topLevelClass, java.util.List<IntrospectedColumn> introspectedColumns, IntrospectedTable introspectedTable)
          Generates a hashCode method that multiplies the hashCodes of all fields.
 boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable)
          This method is called after the base record class is generated by the ibator supplied JavaModelGenerator.
 boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable)
          This method is called after the primary key class is generated by the ibator supplied JavaModelGenerator.
 boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable)
          This method is called after the record with BLOBs class is generated by the ibator supplied JavaModelGenerator.
 boolean validate(java.util.List<java.lang.String> warnings)
          This plugin is always valid - no properties are required
 
Methods inherited from class org.apache.ibatis.ibator.api.IbatorPluginAdapter
contextGenerateAdditionalJavaFiles, contextGenerateAdditionalJavaFiles, contextGenerateAdditionalXmlFiles, contextGenerateAdditionalXmlFiles, daoCountByExampleMethodGenerated, daoCountByExampleMethodGenerated, daoDeleteByExampleMethodGenerated, daoDeleteByExampleMethodGenerated, daoDeleteByPrimaryKeyMethodGenerated, daoDeleteByPrimaryKeyMethodGenerated, daoImplementationGenerated, daoInsertMethodGenerated, daoInsertMethodGenerated, daoInsertSelectiveMethodGenerated, daoInsertSelectiveMethodGenerated, daoInterfaceGenerated, daoSelectByExampleWithBLOBsMethodGenerated, daoSelectByExampleWithBLOBsMethodGenerated, daoSelectByExampleWithoutBLOBsMethodGenerated, daoSelectByExampleWithoutBLOBsMethodGenerated, daoSelectByPrimaryKeyMethodGenerated, daoSelectByPrimaryKeyMethodGenerated, daoUpdateByExampleSelectiveMethodGenerated, daoUpdateByExampleSelectiveMethodGenerated, daoUpdateByExampleWithBLOBsMethodGenerated, daoUpdateByExampleWithBLOBsMethodGenerated, daoUpdateByExampleWithoutBLOBsMethodGenerated, daoUpdateByExampleWithoutBLOBsMethodGenerated, daoUpdateByPrimaryKeySelectiveMethodGenerated, daoUpdateByPrimaryKeySelectiveMethodGenerated, daoUpdateByPrimaryKeyWithBLOBsMethodGenerated, daoUpdateByPrimaryKeyWithBLOBsMethodGenerated, daoUpdateByPrimaryKeyWithoutBLOBsMethodGenerated, daoUpdateByPrimaryKeyWithoutBLOBsMethodGenerated, getIbatorContext, getProperties, initialized, modelExampleClassGenerated, modelFieldGenerated, modelGetterMethodGenerated, modelSetterMethodGenerated, setIbatorContext, setProperties, sqlMapCountByExampleElementGenerated, sqlMapDeleteByExampleElementGenerated, sqlMapDeleteByPrimaryKeyElementGenerated, sqlMapDocumentGenerated, sqlMapExampleWhereClauseElementGenerated, sqlMapGenerated, sqlMapInsertElementGenerated, sqlMapInsertSelectiveElementGenerated, sqlMapResultMapWithBLOBsElementGenerated, sqlMapResultMapWithoutBLOBsElementGenerated, sqlMapSelectByExampleWithBLOBsElementGenerated, sqlMapSelectByExampleWithoutBLOBsElementGenerated, sqlMapSelectByPrimaryKeyElementGenerated, sqlMapUpdateByExampleSelectiveElementGenerated, sqlMapUpdateByExampleWithBLOBsElementGenerated, sqlMapUpdateByExampleWithoutBLOBsElementGenerated, sqlMapUpdateByPrimaryKeySelectiveElementGenerated, sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated, sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EqualsHashCodePlugin

public EqualsHashCodePlugin()
Method Detail

validate

public boolean validate(java.util.List<java.lang.String> warnings)
This plugin is always valid - no properties are required

Parameters:
warnings - add strings to this list to specify warnings. For example, if the plugin is invalid, you should specify why. Warnings are reported to users after the completion of the ibator run.
Returns:
true if the plugin is in a valid state. Invalid plugins will not be called

modelBaseRecordClassGenerated

public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
                                             IntrospectedTable introspectedTable)
Description copied from interface: IbatorPlugin
This method is called after the base record class is generated by the ibator supplied JavaModelGenerator. This method will only be called if the table rules call for generation of a base record class.

This method is only guaranteed to be called by the ibator supplied Java model generators. Other user supplied generators may, or may not, call this method.

Specified by:
modelBaseRecordClassGenerated in interface IbatorPlugin
Overrides:
modelBaseRecordClassGenerated in class IbatorPluginAdapter
Parameters:
topLevelClass - the generated base record class
introspectedTable - ibator's class containing information about the table as introspected from the database
Returns:
true if the class should be generated, false if the generated class should be ignored. In the case of multiple plugins, the first plugin returning false will disable the calling of further plugins.

modelPrimaryKeyClassGenerated

public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
                                             IntrospectedTable introspectedTable)
Description copied from interface: IbatorPlugin
This method is called after the primary key class is generated by the ibator supplied JavaModelGenerator. This method will only be called if the table rules call for generation of a primary key class.

This method is only guaranteed to be called by the ibator supplied Java model generators. Other user supplied generators may, or may not, call this method.

Specified by:
modelPrimaryKeyClassGenerated in interface IbatorPlugin
Overrides:
modelPrimaryKeyClassGenerated in class IbatorPluginAdapter
Parameters:
topLevelClass - the generated primary key class
introspectedTable - ibator's class containing information about the table as introspected from the database
Returns:
true if the class should be generated, false if the generated class should be ignored. In the case of multiple plugins, the first plugin returning false will disable the calling of further plugins.

modelRecordWithBLOBsClassGenerated

public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass,
                                                  IntrospectedTable introspectedTable)
Description copied from interface: IbatorPlugin
This method is called after the record with BLOBs class is generated by the ibator supplied JavaModelGenerator. This method will only be called if the table rules call for generation of a record with BLOBs class.

This method is only guaranteed to be called by the ibator supplied Java model generators. Other user supplied generators may, or may not, call this method.

Specified by:
modelRecordWithBLOBsClassGenerated in interface IbatorPlugin
Overrides:
modelRecordWithBLOBsClassGenerated in class IbatorPluginAdapter
Parameters:
topLevelClass - the generated record with BLOBs class
introspectedTable - ibator's class containing information about the table as introspected from the database
Returns:
true if the class should be generated, false if the generated class should be ignored. In the case of multiple plugins, the first plugin returning false will disable the calling of further plugins.

generateEquals

protected void generateEquals(TopLevelClass topLevelClass,
                              java.util.List<IntrospectedColumn> introspectedColumns,
                              IntrospectedTable introspectedTable)
Generates an equals method that does a comparison of all fields.

The generated equals method will be correct unless:

Parameters:
topLevelClass - the class to which the method will be added
introspectedColumns - column definitions of this class and any superclass of this class
introspectedTable - the table corresponding to this class

generateHashCode

protected void generateHashCode(TopLevelClass topLevelClass,
                                java.util.List<IntrospectedColumn> introspectedColumns,
                                IntrospectedTable introspectedTable)
Generates a hashCode method that multiplies the hashCodes of all fields.

Note that this is a very simplistic implementation of hashCode.

Parameters:
topLevelClass - the class to which the method will be added
introspectedColumns - column definitions of this class and any superclass of this class
introspectedTable - the table corresponding to this class