views:

195

answers:

7

Hi there,

I'm looking for an solution to generate a checksum for any type of Java object, which remains the same for every exection of an application which produces the same object.

I tried it with Object.hashCode(), but as I read in the api

....This integer need not remain consistent from one execution of an application to another execution of the same application.

thank you, best regard alex

+4  A: 

I think you should look at serialization. Serialization mechanism needs to solve similar problem, so you can look how it's implemented.

But if you describe the problem you're trying to solve you'll probably get more precise solution.

Roman
+3  A: 

If you control the source, you can implement hashCode() so it will be consistent from one execution to another.

Seffi
+3  A: 

Do you want to be able to do this for all Java objects?

In that case hashCode() doesn't work.

For some classes hashCode() has a stricter definition which guarantees equality across executions. For example String has a well-defined hashCode implementation. Similarly List and Set have well-defined values, provided all objects that they contain also have well-defined values (note that the general Collection.hashCode() does not require the value to be well-defined).

For other classes you will have to use reflection recursively with some well-defined formula to build a checksum.

Joachim Sauer
A: 

Hashcode is OK. Either given class overrides equals and also, as contract demands, hashcode. By contract, if equals returns true hashcode must be the same.
Or class doesn't override equals. In this case different executions of your application cannot produce same object, so there is no problem.
The only problem is that some classes (even from Java API) break contract for equals.

Tadeusz Kopec
+2  A: 

I had similar problem (generating good hashcode for XML files) and I found out that the best solution is to use MD5 through MessageDigest or in case you need something faster: Fast MD5. Please notice that even if Object.hashCode would be the same every time it is anyway too short (only 32 bits) to ensure high uniqueness. I think 64 bits is a minimum to compute good hash code. Please be aware that MD5 generates 128 bits long hash code, which should is even more that needed in this situation.

Of course to use MessageDigest you need serialize (in your case marshall) the object first.

koppernickus
thank you all for your answers. I calculate a MD5 from the marshalled request.
Alex
+1  A: 

The Apache commons lang library provides a HashCodeBuilder class which helps building a hash code that fills your requirements from the class properties.

Example:

   public int checksum() {
     // you pick a hard-coded, randomly chosen, non-zero, odd number
     // ideally different for each class
     return new HashCodeBuilder(17, 37).
       append(property1).
       append(property2).
       append(property3).
       toHashCode();
   }

See Commons Lang API

FRotthowe
+1  A: 

If you're f you're using Eclipse IDE then it has actions (under Source menu) to generate hashcode and equals functions. It allows you to choose the attributes of the class you want in the hashcode. This is similar to using the HashCodeBuilder approach that has already been suggested.

Alternatively you could stream the object to a byte array and generate an MD5 of that.

pauli