: eval 'exec perl -w -S `echo $0 | sed "s/.*\///" ` ${1+"$@"}' if 0; @a=@ARGV; @flags=grep(/^-/,@a); @other=(); @other=grep(!/^-/,@a); if ( grep( /^-h(elp)?$/,@flags) || scalar(@other) != 1) { $scriptName = $0; $scriptName =~ s%^.*/%%; print <<"END"; Usage: $scriptName [-h -help] Class Print out a prototype of a new java class that implements equals() and hashCode(); END exit;} $rN="\n"; $rN="\n" if ( ! -d "/var" ); $Class=$a[0]; print <<"END"; public class $Class { /* Overrides Object.equals */ \@Override public boolean equals (Object object) { // short circuit for efficiency if (this == object ) { return true; } // Test that belongs to same class. (instanceof lacks symmetry.) if (object==null || getClass()!=object.getClass()) { return false; } // Insure contract that if a.equals(b) then a.hashCode()==b.hashCode(); if (this.hashCode()!=object.hashCode()) { return false; } // Keep next test if super overrides Object.equals() if ($Class.class.getSuperclass()!=Object.class // have a super && !super.equals(object)) {return false;} // super overrides equals() // Test equality of members $Class other = ($Class) object; // if (!this._member.equals(other._member)) return false; // if (this._int != other._int) return false; // if (!java.util.Arrays.equals(this._array,other._array)) return false; return true; } /* Overrides Object.hashCode() because $Class overrides Object.equals(). Preserves the following rule:
$Class a,b;
if (a.equals(b)) assert(a.hashCode()==b.hashCode());
if (a.hashCode()!=b.hashCode()) assert(!a.equals(b));
*/
\@Override public int hashCode() {
return 1; // XXX
// A constant avoids breaking equals(), but makes very slow hash tables.
// If $Class goes in hash tables, calculate an int from variable members
// return _member1.hashCode() ^ _member2.hashCode(); // for example
// return _member1.hashCode() + 17*_member2.hashCode(); // for example
// Return hashCode of a few members that change most often.
// Cache value if object is immutable, or if slow to calculate.
// Different instances can never be equal with the default for Object:
// return System.identityHashCode(this);
}
}
END