A Technology Blog About Code Development, Architecture, Operating System, Hardware, Tips and Tutorials for Developers.

Saturday, October 22, 2016

hashcode() Best Practice

9:38:00 PM Posted by Satish No comments
A good hash function tends to produce unequal hash codes for unequal objects. Ideally, a hash function should distribute any reasonable collection of unequal instances uniformly across all possible hash values. Achieving this would be ideal, but can be complex. Luckily the following rules made this easy:
1. Store some constant non zero value, say, 17, in an int variable called result.
2. For each significant field f in your object (each field taken into account by the
equals method), do the following:
             a. Compute an int hash code c for the field:
                          i. If the field is a boolean, compute (f ? 1 : 0).
                          ii. If the field is a byte, char, short, or int, compute (int) f.
                          iii. If the field is a long, compute (int) (f ^ (f >>> 32)).
                          iv. If the field is a float, compute Float.floatToIntBits(f).
                          v. If the field is a double, compute Double.doubleToLongBits(f), and then hash the resulting long as in step 2.a.iii.
                          vi. If the field is an object reference and this class’s equals method, compares the field by recursively invoking equals, recursively invoke hashCode on the field. If a more complex comparison is required, compute a “canonical representation” for this field and invoke hashCode on the canonical representation. If the value of the field is null, return 0 (or some other constant, but 0 is traditional).
                          vii. If the field is an array, treat it as if each element were a separate field. That is, compute a hash code for each significant element by applying these rules recursively, and combine these values per step 2.b. If every element in an array field is significant, you can use one of the Arrays.hashCode methods added in release 1.5.
             b. Combine the hash code c computed in step 2.a into result as follows:
                          result = 31 * result + c;
3. Return result.
4. Write unit tests to verify if equal instances have unequal hash codes, figure out why and fix the problem.

Wednesday, October 19, 2016

ReflectionUtils.doWithFields()

6:47:00 PM Posted by Satish No comments
Spring has a nice Utility class ReflectionUtils that does just that: it defines a method to loop over all fields of all super classes with a callback: ReflectionUtils.doWithFields()

Documentation:
Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.

Parameters:
- clazz - the target class to analyze
- fc - the callback to invoke for each field
- ff - the filter that determines the fields to apply the callback to

Sample Code:


Output:
Found field private int java.util.ArrayList.size in type class java.util.ArrayList

Wednesday, October 5, 2016

Custom Maven Plugin

7:30:00 PM Posted by Satish , No comments
When you write a custom plugin, you are going to be writing a series of Mojos (goals). Every Mojo is a single Java class which contains a series of annotations that tell Maven how to generate the Plugin descriptor. Before you can start writing Mojo classes, you will need to create Maven project with the appropriate packaging and POM.

To create a plugin project, you should use the Maven Archetype plugin. The following command-line will create a plugin with a groupId of org.tat.api and the artifactId of webapp-optimizer:

pom.xml
The most important element in a plugin project’s POM is the packaging element which has a value of maven-plugin. The other important piece of a plugin project’s POM is the dependency on the Maven Plugin API. This project depends on version 2.0 of the maven-plugin-api and it also adds in JUnit as a test-scoped dependency.

The Mojo implementation shown in the following example implements the Mojo interface by extending the org.apache.maven.plugin.AbstractMojo class. Before we dive into the code for this Mojo, let’s take some time to explore the methods on the Mojo interface. Mojo provides the following methods:

void setLog( org.apache.maven.monitor.logging.Log log )
When Maven loads and executes a Mojo, it is going to call the setLog() method and supply the Mojo instance with a suitable logging destination to be used in your custom plugin.

protected Log getLog()
Maven is going to call setLog() before your Mojo is executed, and your Mojo can retrieve the logging object by calling getLog().

void execute() throws org.apache.maven.plugin.MojoExecutionException
This method is called by Maven when it is time to execute your goal.


You can run the plug-in using the following command. The commands is as per the groupId:artifactId:version:goal format.

 When you run this command-line you should see output that contains the output of the echo goal with the default message: "Hello Maven World…". If you want to customize the message, you can pass the value of the message parameter with the following command-line: