Package org.biojava.utils
Class ObjectUtil
java.lang.Object
org.biojava.utils.ObjectUtil
utility methods for implementing the equals() and hashCode() methods of Objects.
All credit for this class goes to Mark Davis (Java Report 5(1), 46; Java Report 5(4), 60).
All equals() methods in this class take the two fields to compare for equality as arguments and return whether they are equal. Consequently, the equals() method of class AClass should be implemented (taking advantage of the equals() methods in this class) as follows:
public boolean equals(Object o) { if (o == this) return true; // if this class AClass is a direct sub-class of Object: if (o == null) return false; if (!o.getClass().equals(this.getClass())) return false; // else if this class AClass extends another class than Object: //if (!super.equals(o)) return false; AClass that = (AClass) o; // only compare fields of this class AClass (not of super-classes): //if (!ObjectUtil.equals(this.field, that.field)) return false; // this and that are identical if we made it 'til here return true; }
All hashCode() methods in this class take the current hashCode value as the first argument and the field to take into account as the second argument and return the newly calculated hashCode value (which now includes the influence of the given field). Consequently, the hashCode() method of class AClass should be implemented (taking advantage of the hashCode() methods in this class) as follows:
public int hashCode() { // if this class AClass is a direct sub-class of Object: int hc = 0; // else if this class AClass extends another class than Object: //int hc = super.hashCode(); // only take into account fields of this class AClass (not of super-class): //hc = ObjectUtil.hashCode(hc, field); return hc; }
- Author:
- Gerald Loeffler
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
the current hashCode is always first multiplied with this prime before the hashCode value for a particular field is added. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
equals
(boolean[] a1, boolean[] a2) static boolean
equals
(boolean b1, boolean b2) static boolean
equals
(double[] a1, double[] a2) static boolean
equals
(double d1, double d2) static boolean
equals
(float[] a1, float[] a2) static boolean
equals
(float f1, float f2) static boolean
equals
(int[] a1, int[] a2) static boolean
equals
(int i1, int i2) static boolean
equals
(long[] a1, long[] a2) static boolean
equals
(long l1, long l2) static boolean
static boolean
static int
hashCode
(int currentHashCodeValue, boolean b) static int
hashCode
(int currentHashCodeValue, boolean[] a) static int
hashCode
(int currentHashCodeValue, double d) static int
hashCode
(int currentHashCodeValue, double[] a) static int
hashCode
(int currentHashCodeValue, float f) static int
hashCode
(int currentHashCodeValue, float[] a) static int
hashCode
(int currentHashCodeValue, int i) static int
hashCode
(int currentHashCodeValue, int[] a) static int
hashCode
(int currentHashCodeValue, long l) static int
hashCode
(int currentHashCodeValue, long[] a) static int
static int
-
Field Details
-
PRIME
the current hashCode is always first multiplied with this prime before the hashCode value for a particular field is added.- See Also:
-
-
Constructor Details
-
ObjectUtil
public ObjectUtil()
-
-
Method Details
-
equals
-
equals
-
equals
-
equals
-
equals
-
equals
-
equals
-
equals
-
equals
-
equals
-
equals
-
equals
-
hashCode
-
hashCode
-
hashCode
-
hashCode
-
hashCode
-
hashCode
-
hashCode
-
hashCode
-
hashCode
-
hashCode
-
hashCode
-
hashCode
-